forked from winsoft666/cpp-ipc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.h
executable file
·86 lines (68 loc) · 2.2 KB
/
test.h
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
#pragma once
#include <iostream>
#include <atomic>
#include <thread>
#include <string>
#include <memory>
#include <mutex>
#include <utility>
#include "gtest/gtest.h"
#include "capo/stopwatch.hpp"
#include "thread_pool.h"
namespace ipc_ut {
template <typename Dur>
struct unit;
template <> struct unit<std::chrono::nanoseconds> {
constexpr static char const * str() noexcept {
return "ns";
}
};
template <> struct unit<std::chrono::microseconds> {
constexpr static char const * str() noexcept {
return "us";
}
};
template <> struct unit<std::chrono::milliseconds> {
constexpr static char const * str() noexcept {
return "ms";
}
};
template <> struct unit<std::chrono::seconds> {
constexpr static char const * str() noexcept {
return "sec";
}
};
struct test_stopwatch {
capo::stopwatch<> sw_;
std::atomic_flag started_ = ATOMIC_FLAG_INIT;
void start() {
if (!started_.test_and_set()) {
sw_.start();
}
}
template <typename ToDur = std::chrono::nanoseconds>
void print_elapsed(int N, int Loops, char const * message = "") {
auto ts = sw_.elapsed<ToDur>();
std::cout << "[" << N << ", \t" << Loops << "] " << message << "\t"
<< (double(ts) / double(Loops)) << " " << unit<ToDur>::str() << std::endl;
}
template <int Factor, typename ToDur = std::chrono::nanoseconds>
void print_elapsed(int N, int M, int Loops, char const * message = "") {
auto ts = sw_.elapsed<ToDur>();
std::cout << "[" << N << "-" << M << ", \t" << Loops << "] " << message << "\t"
<< (double(ts) / double(Factor ? (Loops * Factor) : (Loops * N))) << " " << unit<ToDur>::str() << std::endl;
}
template <typename ToDur = std::chrono::nanoseconds>
void print_elapsed(int N, int M, int Loops, char const * message = "") {
print_elapsed<0, ToDur>(N, M, Loops, message);
}
};
inline static thread_pool & sender() {
static thread_pool pool;
return pool;
}
inline static thread_pool & reader() {
static thread_pool pool;
return pool;
}
} // namespace ipc_ut