66
77#include " BenchMark.h"
88
9- namespace {
9+ extern std:: size_t g_work_load_scale;
1010
11- static const char * LONG_STR = " Lorem ipsum" ;
12- // dolor sit amet, consectetur adipiscing elit, sed do"
13- // "do aeiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis"
14- // "nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure"
15- // "dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Except"
16- // "eur ssint occaecat cupidatat nnon proident, sunt in culpa qui officia deserunt mollit anim id"
17- // "Lorem ipsum dolor sit amet laboris nisi ut aliquip ex ea commodo";
18- }
19-
20- // Pre-created string to isolate call overhead
21- argStr_t g_longStr (LONG_STR);
22-
23- extern decltype (&rtl_bench::getMessage) getMessagePtr;
24- extern decltype (&rtl_bench::sendMessage) sendMessagePtr;
25- extern decltype (&rtl_bench::Node::getMessage) getMessageNodePtr;
26- extern decltype (&rtl_bench::Node::sendMessage) sendMessageNodePtr;
27-
28- namespace rtl_bench
11+ namespace
2912{
30- static Node* node = new Node();
31-
32- void BenchMark::directCall_noReturn (benchmark::State& state)
33- {
34- for (auto _ : state)
35- {
36- sendMessagePtr (g_longStr);
37- benchmark::DoNotOptimize (g_msg);
38- }
39- }
40-
41-
42- void BenchMark::stdFunctionCall_noReturn (benchmark::State& state)
43- {
44- static std::function sendMsg = [](argStr_t& pMsg) {
45- sendMessagePtr (pMsg);
46- };
47-
48- for (auto _ : state)
49- {
50- sendMsg (g_longStr);
51- benchmark::DoNotOptimize (g_msg);
52- }
53- }
54-
55- void BenchMark::stdFunctionMethodCall_noReturn (benchmark::State& state)
13+ static void work_load (bm::argStr_t& pMsg)
5614 {
57- static std::function sendMsg = [=](argStr_t& pMsg) {
58- (node->*sendMessageNodePtr)(pMsg);
59- };
60-
61- for (auto _ : state)
15+ bm::g_msg = std::string ();
16+ for (int i = 0 ; i < g_work_load_scale; ++i)
6217 {
63- sendMsg (g_longStr);
64- benchmark::DoNotOptimize (g_msg);
65- }
66- }
67-
68-
69- void BenchMark::directCall_withReturn (benchmark::State& state)
70- {
71- static auto _ = []() {
72- std::cout << " --------------------------------------------------"
73- " -----------------------------------------------" << std::endl;
74- return 0 ;
75- }();
76-
77- for (auto _ : state)
78- {
79- benchmark::DoNotOptimize (getMessage (g_longStr));
80- }
81- }
82-
83-
84- void BenchMark::stdFunctionCall_withReturn (benchmark::State& state)
85- {
86- static std::function getMsg = [](argStr_t& pMsg) {
87- return getMessagePtr (pMsg);
88- };
89-
90- for (auto _ : state)
91- {
92- benchmark::DoNotOptimize (getMsg (g_longStr));
93- }
94- }
95-
96-
97- void BenchMark::stdFunctionMethodCall_withReturn (benchmark::State& state)
98- {
99- static std::function getMsg = [=](argStr_t& pMsg) {
100- return (node->*getMessageNodePtr)(pMsg);
101- };
102-
103- for (auto _ : state)
104- {
105- benchmark::DoNotOptimize (getMsg (g_longStr));
18+ bm::g_msg->append (pMsg);
10619 }
10720 }
10821}
10922
11023
111- namespace rtl_bench
24+ namespace bm
11225{
113- void BenchMark::reflectedCall_noReturn (benchmark::State& state)
26+ NOINLINE void sendMessage (argStr_t pMsg)
11427 {
115- static rtl::Function sendMsg = cxx_mirror ().getFunction (" sendMessage" ).value ();
116- static auto _ = []() {
117- auto err = sendMsg.bind ().call (g_longStr).err ;
118- if (err != rtl::error::None) {
119- std::cout << " [rtl:0] err: " << rtl::to_string (err)<<" \n " ;
120- }
121- return 0 ;
122- }();
123-
124- for (auto _ : state)
125- {
126- benchmark::DoNotOptimize (sendMsg.bind ().call (g_longStr));
127- }
28+ volatile auto * p = &pMsg;
29+ static_cast <void >(p);
30+ work_load (pMsg);
12831 }
12932
13033
131- void BenchMark::reflectedMethodCall_noReturn (benchmark::State& state )
34+ NOINLINE retStr_t getMessage (argStr_t pMsg )
13235 {
133- static rtl::Record rNode = cxx_mirror ().getRecord (" Node" ).value ();
134- static rtl::Method sendMsg = rNode.getMethod (" sendMessage" ).value ();
135- static rtl::RObject robj = rNode.create <rtl::alloc::Heap>().rObject ;
136- static auto _ = []() {
137- auto err = sendMsg.bind (robj).call (g_longStr).err ;
138- if (err != rtl::error::None) {
139- std::cout << " [rtl:1] err: " << rtl::to_string (err) << " \n " ;
140- }
141- return 0 ;
142- }();
143-
144- for (auto _ : state)
145- {
146- benchmark::DoNotOptimize (sendMsg.bind (robj).call (g_longStr));
147- }
36+ volatile auto * p = &pMsg;
37+ static_cast <void >(p);
38+ work_load (pMsg);
39+ return bm::retStr_t (bm::g_msg->c_str ());
14840 }
14941
15042
151- void BenchMark::reflectedCall_withReturn (benchmark::State& state)
43+ NOINLINE void Node::sendMessage (argStr_t pMsg)
15244 {
153- static rtl::Function getMsg = cxx_mirror ().getFunction (" getMessage" ).value ();
154- static auto _ = []() {
155- auto err = getMsg.bind ().call (g_longStr).err ;
156- if (err != rtl::error::None) {
157- std::cout << " [rtl:2] err: " << rtl::to_string (err) << " \n " ;
158- }
159- return 0 ;
160- }();
161-
162- for (auto _ : state)
163- {
164- benchmark::DoNotOptimize (getMsg.bind ().call (g_longStr));
165- }
45+ volatile auto * p = &pMsg;
46+ static_cast <void >(p);
47+ work_load (pMsg);
16648 }
16749
16850
169- void BenchMark::reflectedMethodCall_withReturn (benchmark::State& state )
51+ NOINLINE retStr_t Node::getMessage (argStr_t pMsg )
17052 {
171- static rtl::Record rNode = cxx_mirror ().getRecord (" Node" ).value ();
172- static rtl::Method getMsg = rNode.getMethod (" getMessage" ).value ();
173- static rtl::RObject robj = rNode.create <rtl::alloc::Heap>().rObject ;
174- static auto _ = []() {
175- auto err = getMsg.bind (robj).call (g_longStr).err ;
176- if (err != rtl::error::None) {
177- std::cout << " [rtl:3] err: " << rtl::to_string (err) << " \n " ;
178- }
179- return 0 ;
180- }();
181-
182- for (auto _ : state)
183- {
184- benchmark::DoNotOptimize (getMsg.bind (robj).call (g_longStr));
185- }
53+ volatile auto * p = &pMsg;
54+ static_cast <void >(p);
55+ work_load (pMsg);
56+ return bm::retStr_t (bm::g_msg->c_str ());
18657 }
18758}
0 commit comments