test-bench.C 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. // (c) Daniel Llorens - 2017
  2. // This library is free software; you can redistribute it and/or modify it under
  3. // the terms of the GNU Lesser General Public License as published by the Free
  4. // Software Foundation; either version 3 of the License, or (at your option) any
  5. // later version.
  6. /// @file bench.H
  7. /// @brief Test the benchmarking microlibrary.
  8. #include <string>
  9. #include <iostream>
  10. #include <iomanip>
  11. #include <limits>
  12. #include <chrono>
  13. #include <thread>
  14. #include "ra/operators.H"
  15. #include "ra/io.H"
  16. #include "ra/test.H"
  17. #include "ra/bench.H"
  18. using std::cout; using std::endl;
  19. int main()
  20. {
  21. TestRecorder tr;
  22. tr.section("straight");
  23. {
  24. auto f = [](auto && a, auto && b)
  25. {
  26. std::this_thread::sleep_for(std::chrono::nanoseconds(1));
  27. return a+b;
  28. };
  29. auto b = Benchmark {/* repeats */ 100, /* runs */ 10};
  30. auto vala = b.run(f, 1, 2);
  31. cout << "empty: " << vala.empty() << endl;
  32. b.report(std::cout, vala, 1e-9);
  33. auto valb = b.run(f, ra::iota(3), ra::iota(10, 3));
  34. b.report(std::cout, valb, 1e-9);
  35. }
  36. tr.section("fixture");
  37. {
  38. auto g = [](auto && repeat, auto && a, auto && b)
  39. {
  40. /* do stuff */
  41. repeat([&]() { std::this_thread::sleep_for(std::chrono::nanoseconds(1));
  42. return a+b; });
  43. /* do stuff */
  44. };
  45. auto b = Benchmark {/* repeats */ 100, /* runs */ 10};
  46. auto vala = b.run_f(g, 1, 2);
  47. cout << "empty: " << vala.empty() << endl;
  48. b.report(std::cout, vala, 1e-9);
  49. auto valb = b.run_f(g, ra::iota(3), ra::iota(10, 3));
  50. b.report(std::cout, valb, 1e-9);
  51. }
  52. return tr.summary();
  53. };