datanode_base.cpp 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. #include "core/datanode_base.h"
  2. #include <boost/asio/awaitable.hpp>
  3. #include <boost/asio/co_spawn.hpp>
  4. #include <boost/asio/detached.hpp>
  5. #include <cassert>
  6. #include <benchmark/benchmark.h>
  7. using boost::asio::awaitable;
  8. using boost::asio::co_spawn;
  9. using boost::asio::detached;
  10. using namespace sophiar;
  11. static void BM_timestamp(benchmark::State &state) {
  12. for (auto _: state) {
  13. benchmark::DoNotOptimize(sophiar::current_timestamp());
  14. }
  15. }
  16. BENCHMARK(BM_timestamp);
  17. static void BM_datanode_chain(benchmark::State &state) {
  18. struct test_type : public datanode_base<high_freq_tag> {
  19. uint64_t cnt = 0;
  20. awaitable<bool> exec() {
  21. auto input_data = get_input_data(0);
  22. auto output_data = data_packet::new_instance();
  23. output_data->copy_content(*input_data);
  24. (*output_data)[0] += 1.0;
  25. output_data->timestamp = current_timestamp();
  26. cnt = (*output_data)[0];
  27. set_output_data(0, output_data);
  28. co_return true;
  29. }
  30. };
  31. co_spawn(high_freq_context, [&]() -> awaitable<void> {
  32. auto total = state.range(0);
  33. auto pool = new test_type[total];
  34. for (size_t i = 0; i < total; ++i) {
  35. auto &node = pool[i];
  36. co_await node.init();
  37. node.set_trigger_mode(test_type::trigger_mode_type::INPUT);
  38. node.set_trigger_input_mask(0b1);
  39. if (i != 0) {
  40. test_type::connect(pool[i - 1], 0, node, 0);
  41. }
  42. co_await node.start();
  43. }
  44. auto test_data = test_type::data_packet::new_instance();
  45. test_data->refresh();
  46. for (auto _: state) {
  47. pool[0].update_input_data(0, test_data);
  48. assert(pool[total - 1].cnt == total);
  49. }
  50. }, detached);
  51. high_freq_context.run();
  52. }
  53. BENCHMARK(BM_datanode_chain)->RangeMultiplier(10)->Range(1, 1e6);
  54. BENCHMARK_MAIN();