1- #pragma once
2-
3- #include < iostream>
4- #include < atomic>
5- #include < thread>
6- #include < string>
7- #include < memory>
8- #include < mutex>
9- #include < utility>
10-
11- #include " gtest/gtest.h"
12-
13- #include " capo/stopwatch.hpp"
14-
15- #include " thread_pool.h"
16-
17- namespace ipc_ut {
18-
19- template <typename Dur>
20- struct unit ;
21-
22- template <> struct unit <std::chrono::nanoseconds> {
23- constexpr static char const * str () noexcept {
24- return " ns" ;
25- }
26- };
27-
28- template <> struct unit <std::chrono::microseconds> {
29- constexpr static char const * str () noexcept {
30- return " us" ;
31- }
32- };
33-
34- template <> struct unit <std::chrono::milliseconds> {
35- constexpr static char const * str () noexcept {
36- return " ms" ;
37- }
38- };
39-
40- template <> struct unit <std::chrono::seconds> {
41- constexpr static char const * str () noexcept {
42- return " sec" ;
43- }
44- };
45-
46- struct test_stopwatch {
47- capo::stopwatch<> sw_;
48- std::atomic_flag started_ = ATOMIC_FLAG_INIT;
49-
50- void start () {
51- if (!started_.test_and_set ()) {
52- sw_.start ();
53- }
54- }
55-
56- template <typename ToDur = std::chrono::nanoseconds>
57- void print_elapsed (int N, int Loops, char const * message = " " ) {
58- auto ts = sw_.elapsed <ToDur>();
59- std::cout << " [" << N << " , \t " << Loops << " ] " << message << " \t "
60- << (double (ts) / double (Loops)) << " " << unit<ToDur>::str () << std::endl;
61- }
62-
63- template <int Factor, typename ToDur = std::chrono::nanoseconds>
64- void print_elapsed (int N, int M, int Loops, char const * message = " " ) {
65- auto ts = sw_.elapsed <ToDur>();
66- std::cout << " [" << N << " -" << M << " , \t " << Loops << " ] " << message << " \t "
67- << (double (ts) / double (Factor ? (Loops * Factor) : (Loops * N))) << " " << unit<ToDur>::str () << std::endl;
68- }
69-
70- template <typename ToDur = std::chrono::nanoseconds>
71- void print_elapsed (int N, int M, int Loops, char const * message = " " ) {
72- print_elapsed<0 , ToDur>(N, M, Loops, message);
73- }
74- };
75-
76- inline static thread_pool & sender () {
77- static thread_pool pool;
78- return pool;
79- }
80-
81- inline static thread_pool & reader () {
82- static thread_pool pool;
83- return pool;
84- }
85-
86- } // namespace ipc_ut
1+ #pragma once
2+
3+ #include < iostream>
4+ #include < atomic>
5+ #include < thread>
6+ #include < string>
7+ #include < memory>
8+ #include < mutex>
9+ #include < utility>
10+
11+ #include " gtest/gtest.h"
12+
13+ #include " capo/stopwatch.hpp"
14+
15+ #include " thread_pool.h"
16+
17+ #include " libipc/platform/detail.h"
18+ #ifdef IPC_OS_LINUX_
19+ #include < fcntl.h> // ::open
20+ #endif
21+
22+ namespace ipc_ut {
23+
24+ template <typename Dur>
25+ struct unit ;
26+
27+ template <> struct unit <std::chrono::nanoseconds> {
28+ constexpr static char const * str () noexcept {
29+ return " ns" ;
30+ }
31+ };
32+
33+ template <> struct unit <std::chrono::microseconds> {
34+ constexpr static char const * str () noexcept {
35+ return " us" ;
36+ }
37+ };
38+
39+ template <> struct unit <std::chrono::milliseconds> {
40+ constexpr static char const * str () noexcept {
41+ return " ms" ;
42+ }
43+ };
44+
45+ template <> struct unit <std::chrono::seconds> {
46+ constexpr static char const * str () noexcept {
47+ return " sec" ;
48+ }
49+ };
50+
51+ struct test_stopwatch {
52+ capo::stopwatch<> sw_;
53+ std::atomic_flag started_ = ATOMIC_FLAG_INIT;
54+
55+ void start () {
56+ if (!started_.test_and_set ()) {
57+ sw_.start ();
58+ }
59+ }
60+
61+ template <typename ToDur = std::chrono::nanoseconds>
62+ void print_elapsed (int N, int Loops, char const * message = " " ) {
63+ auto ts = sw_.elapsed <ToDur>();
64+ std::cout << " [" << N << " , \t " << Loops << " ] " << message << " \t "
65+ << (double (ts) / double (Loops)) << " " << unit<ToDur>::str () << std::endl;
66+ }
67+
68+ template <int Factor, typename ToDur = std::chrono::nanoseconds>
69+ void print_elapsed (int N, int M, int Loops, char const * message = " " ) {
70+ auto ts = sw_.elapsed <ToDur>();
71+ std::cout << " [" << N << " -" << M << " , \t " << Loops << " ] " << message << " \t "
72+ << (double (ts) / double (Factor ? (Loops * Factor) : (Loops * N))) << " " << unit<ToDur>::str () << std::endl;
73+ }
74+
75+ template <typename ToDur = std::chrono::nanoseconds>
76+ void print_elapsed (int N, int M, int Loops, char const * message = " " ) {
77+ print_elapsed<0 , ToDur>(N, M, Loops, message);
78+ }
79+ };
80+
81+ inline static thread_pool & sender () {
82+ static thread_pool pool;
83+ return pool;
84+ }
85+
86+ inline static thread_pool & reader () {
87+ static thread_pool pool;
88+ return pool;
89+ }
90+
91+ #ifdef IPC_OS_LINUX_
92+ inline bool check_exist (char const *name) noexcept {
93+ int fd = ::open ((std::string{" /dev/shm/__IPC_SHM__" } + name).c_str (), O_RDONLY);
94+ if (fd == -1 ) {
95+ return false ;
96+ }
97+ ::close (fd);
98+ return true ;
99+ }
100+ #endif
101+
102+ inline bool expect_exist (char const *name, bool expected) noexcept {
103+ #ifdef IPC_OS_LINUX_
104+ return ipc_ut::check_exist (name) == expected;
105+ #else
106+ return true ;
107+ #endif
108+ }
109+
110+ } // namespace ipc_ut
0 commit comments