summaryrefslogtreecommitdiffstats
path: root/libtransport/src/test/test_event_thread.cc
blob: e66b49f10ac9faeb2c11bf78ef8199c6004bf0c8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
/*
 * Copyright (c) 2021 Cisco and/or its affiliates.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at:
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <gtest/gtest.h>
#include <hicn/transport/utils/event_thread.h>

#include <cmath>

namespace utils {

namespace {

class EventThreadTest : public ::testing::Test {
 protected:
  EventThreadTest() : event_thread_() {
    // You can do set-up work for each test here.
  }

  virtual ~EventThreadTest() {
    // You can do clean-up work that doesn't throw exceptions here.
  }

  // If the constructor and destructor are not enough for setting up
  // and cleaning up each test, you can define the following methods:

  virtual void SetUp() {
    // Code here will be called immediately after the constructor (right
    // before each test).
  }

  virtual void TearDown() {
    // Code here will be called immediately after each test (right
    // before the destructor).
  }

  utils::EventThread event_thread_;
};

double average(const unsigned long samples[], int size) {
  double sum = 0;

  for (int i = 0; i < size; i++) {
    sum += samples[i];
  }

  return sum / size;
}

double stdDeviation(const unsigned long samples[], int size) {
  double avg = average(samples, size);
  double var = 0;

  for (int i = 0; i < size; i++) {
    var += (samples[i] - avg) * (samples[i] - avg);
  }

  return sqrt(var / size);
}

}  // namespace

TEST_F(EventThreadTest, SchedulingDelay) {
  using namespace std::chrono;
  const size_t size = 1000000;
  std::vector<unsigned long> samples(size);

  for (unsigned int i = 0; i < size; i++) {
    auto t0 = steady_clock::now();
    event_thread_.add([t0, &samples, i]() {
      auto t1 = steady_clock::now();
      samples[i] = duration_cast<nanoseconds>(t1 - t0).count();
    });
  }

  event_thread_.stop();

  auto avg = average(&samples[0], size);
  auto sd = stdDeviation(&samples[0], size);
  (void)sd;

  // Expect average to be less that 1 ms
  EXPECT_LT(avg, 1000000);
}

}  // namespace utils

int main(int argc, char **argv) {
#if 0
  ::testing::InitGoogleTest(&argc, argv);
  return RUN_ALL_TESTS();
#else
  return 0;
#endif
}