Advertisement
homer512

Sensor merge- bench

Apr 17th, 2024
635
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.41 KB | Source Code | 0 0
  1. #include <chrono>
  2. #include <queue>
  3. #include <random>
  4. #include <string>
  5.  
  6.  
  7. namespace bench {
  8.  
  9. using clock_t = std::chrono::system_clock;
  10.  
  11. struct SensorSample
  12. {
  13.     unsigned sensor_id;
  14.     clock_t::time_point timestamp;
  15.     std::string payload;
  16. };
  17.  
  18. struct SensorReaderMockup
  19. {
  20.     unsigned sensor_id;
  21.     clock_t::duration interval;
  22.     clock_t::time_point last_sample;
  23.  
  24.     SensorSample next()
  25.     {
  26.         SensorSample rtrn {
  27.                 sensor_id, last_sample,
  28.                 "some payload longer than SSO" };
  29.         last_sample += interval;
  30.         return rtrn;
  31.     }
  32. };
  33.  
  34. struct SampleCompare
  35. {
  36.     bool operator()(const SensorSample& left,
  37.                     const SensorSample& right) const noexcept
  38.     { return left.timestamp > right.timestamp; }
  39. };
  40.  
  41. using min_heap_t = std::priority_queue<
  42.         SensorSample, std::vector<SensorSample>, SampleCompare>;
  43.  
  44.  
  45. void run_heap(SensorReaderMockup* sensor_states, unsigned sensors,
  46.               int events)
  47. {
  48.     min_heap_t heap;
  49.     /*
  50.      * Read first sample of each sensor
  51.      */
  52.     for(std::size_t i = 0; i < sensors; ++i)
  53.         heap.push(sensor_states[i].next());
  54.     for(int i = 0; i < events; ++i) {
  55.         /*
  56.          * Retrieve oldest sample for transfer to output
  57.          */
  58.         SensorSample oldest = std::move(heap.top());
  59.         heap.pop();
  60.         /*
  61.          * Read next sample from this particular sensor.
  62.          * Real code would obviously have to check whether sensor is at
  63.          * end-of-file
  64.          */
  65.         heap.push(sensor_states[oldest.sensor_id].next());
  66.     }
  67. }
  68.  
  69. /**
  70.  * Initializes sensors with pseudo-random sampling periods
  71.  */
  72. std::vector<SensorReaderMockup> init_state(unsigned sensors)
  73. {
  74.     std::vector<SensorReaderMockup> rtrn;
  75.     rtrn.reserve(sensors);
  76.     std::default_random_engine rng;
  77.     std::uniform_int_distribution<int> distr {1, 100};
  78.     for(unsigned i = 0; i < sensors; ++i) {
  79.         clock_t::duration interval {distr(rng)};
  80.         clock_t::time_point last_sample = clock_t::time_point{} + interval;
  81.         rtrn.push_back(SensorReaderMockup{i, interval, last_sample});
  82.     }
  83.     return rtrn;
  84. }
  85.  
  86. } // namespace bench
  87.  
  88.  
  89. int main()
  90. {
  91.     using namespace bench;
  92.     unsigned sensors = 60'000;
  93.    int events = 100'000'000;
  94.    std::vector<SensorReaderMockup> sensor_states = init_state(sensors);
  95.    run_heap(sensor_states.data(), sensors, events);
  96. }
  97.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement