Skip to content

Commit b19a629

Browse files
committed
add text file fixture
1 parent 4849f66 commit b19a629

1 file changed

Lines changed: 211 additions & 16 deletions

File tree

  • samples/benchmarks/00_fileiobenchmark

samples/benchmarks/00_fileiobenchmark/main.cpp

Lines changed: 211 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -36,38 +36,49 @@
3636
#define GET_PROCESS_ID() GetCurrentProcessId()
3737
#define GET_THREAD_ID() GetCurrentThreadId()
3838

39+
#define CLI_SPRINTF(_s, _sz, _f, ...) sprintf_s(_s, _TRUNCATE, _f, ##__VA_ARGS__)
40+
#define CLI_VSPRINTF(_s, _sz, _f, _a) vsnprintf_s(_s, _TRUNCATE, _f, _a)
41+
3942
#else
4043

4144
#include <pthread.h>
4245

4346
#define GET_PROCESS_ID() getpid()
4447
#define GET_THREAD_ID() pthread_self()
4548

49+
#define CLI_SPRINTF(_s, _sz, _f, ...) snprintf(_s, _sz, _f, ##__VA_ARGS__)
50+
#define CLI_VSPRINTF(_s, _sz, _f, _a) vsnprintf(_s, _sz, _f, _a)
51+
4652
#endif
4753

4854
using test_clock = std::chrono::steady_clock;
4955

5056
struct ofstream_fixture : public benchmark::Fixture
5157
{
5258
std::ofstream trace;
59+
uint64_t fixture_processId;
60+
uint64_t fixture_threadId;
5361
test_clock::time_point global_start;
5462
test_clock::time_point start;
5563
test_clock::time_point end;
5664

65+
static const size_t BUFFER_SIZE = 16 * 1024;
66+
char buffer[BUFFER_SIZE];
67+
5768
virtual void SetUp(benchmark::State& state) override {
5869
global_start = test_clock::now();
5970

60-
trace.open( "dummy_trace_ofstream.json", std::ios::out );
61-
trace << "[\n";
71+
trace.open( "dummy_trace_ofstream.json", std::ios::out | std::ios::binary );
72+
trace << "[ \n";
6273

6374
start = test_clock::now();
6475

65-
uint64_t processId = GET_PROCESS_ID();
66-
uint64_t threadId = GET_THREAD_ID();
76+
fixture_processId = GET_PROCESS_ID();
77+
fixture_threadId = GET_THREAD_ID();
6778
std::string processName = "dummy_process";
6879
trace
69-
<< "{\"ph\":\"M\", \"name\":\"process_name\", \"pid\":" << processId
70-
<< ", \"tid\":" << threadId
80+
<< "{\"ph\":\"M\", \"name\":\"process_name\", \"pid\":" << fixture_processId
81+
<< ", \"tid\":" << fixture_threadId
7182
<< ", \"args\":{\"name\":\"" << processName
7283
<< "\"}},\n";
7384

@@ -82,7 +93,7 @@ BENCHMARK_DEFINE_F(ofstream_fixture, ChromeCallLogging_WithTimes_WithArgs)(bench
8293
{
8394
while(state.KeepRunning()) {
8495
std::string name;
85-
name += __FUNCTION__;
96+
name += "test_function";
8697

8798
std::ostringstream args;
8899

@@ -110,7 +121,7 @@ BENCHMARK_REGISTER_F(ofstream_fixture, ChromeCallLogging_WithTimes_WithArgs);
110121
BENCHMARK_DEFINE_F(ofstream_fixture, ChromeCallLogging_WithTimes_WithArgs_init)(benchmark::State& state)
111122
{
112123
while(state.KeepRunning()) {
113-
std::string name(__FUNCTION__);
124+
std::string name("test_function");
114125

115126
std::ostringstream args;
116127

@@ -138,7 +149,7 @@ BENCHMARK_REGISTER_F(ofstream_fixture, ChromeCallLogging_WithTimes_WithArgs_init
138149
BENCHMARK_DEFINE_F(ofstream_fixture, ChromeCallLogging_WithTimes_WithArgs_nospaces)(benchmark::State& state)
139150
{
140151
while(state.KeepRunning()) {
141-
std::string name(__FUNCTION__);
152+
std::string name("test_function");
142153

143154
std::ostringstream args;
144155

@@ -163,27 +174,124 @@ BENCHMARK_DEFINE_F(ofstream_fixture, ChromeCallLogging_WithTimes_WithArgs_nospac
163174
}
164175
BENCHMARK_REGISTER_F(ofstream_fixture, ChromeCallLogging_WithTimes_WithArgs_nospaces);
165176

177+
BENCHMARK_DEFINE_F(ofstream_fixture, ChromeCallLogging_WithTimes_WithArgs_nospaces_fixturepid)(benchmark::State& state)
178+
{
179+
while(state.KeepRunning()) {
180+
std::string name("test_function");
181+
182+
std::ostringstream args;
183+
184+
uint64_t threadId = GET_THREAD_ID();
185+
186+
using us = std::chrono::microseconds;
187+
uint64_t usStart =
188+
std::chrono::duration_cast<us>(start - global_start).count();
189+
uint64_t usDelta =
190+
std::chrono::duration_cast<us>(end - start).count();
191+
192+
trace
193+
<< "{\"ph\":\"X\",\"pid\":" << fixture_processId
194+
<< ",\"tid\":" << threadId
195+
<< ",\"name\":\"" << name
196+
<< "\",\"ts\":" << usStart
197+
<< ",\"dur\":" << usDelta
198+
<< args.str()
199+
<< "},\n";
200+
}
201+
}
202+
BENCHMARK_REGISTER_F(ofstream_fixture, ChromeCallLogging_WithTimes_WithArgs_nospaces_fixturepid);
203+
204+
struct ofstream_text_fixture : public benchmark::Fixture
205+
{
206+
std::ofstream trace;
207+
uint64_t fixture_processId;
208+
uint64_t fixture_threadId;
209+
test_clock::time_point global_start;
210+
test_clock::time_point start;
211+
test_clock::time_point end;
212+
213+
static const size_t BUFFER_SIZE = 16 * 1024;
214+
char buffer[BUFFER_SIZE];
215+
216+
virtual void SetUp(benchmark::State& state) override {
217+
global_start = test_clock::now();
218+
219+
trace.open( "dummy_trace_ofstream.json", std::ios::out );
220+
trace << "[ \n";
221+
222+
start = test_clock::now();
223+
224+
fixture_processId = GET_PROCESS_ID();
225+
fixture_threadId = GET_THREAD_ID();
226+
std::string processName = "dummy_process";
227+
trace
228+
<< "{\"ph\":\"M\", \"name\":\"process_name\", \"pid\":" << fixture_processId
229+
<< ", \"tid\":" << fixture_threadId
230+
<< ", \"args\":{\"name\":\"" << processName
231+
<< "\"}},\n";
232+
233+
end = test_clock::now();
234+
}
235+
virtual void TearDown(benchmark::State& state) override {
236+
trace.close();
237+
}
238+
};
239+
240+
BENCHMARK_DEFINE_F(ofstream_text_fixture, ChromeCallLogging_WithTimes_WithArgs)(benchmark::State& state)
241+
{
242+
while(state.KeepRunning()) {
243+
std::string name;
244+
name += "test_function";
245+
246+
std::ostringstream args;
247+
248+
uint64_t processId = GET_PROCESS_ID();
249+
uint64_t threadId = GET_THREAD_ID();
250+
251+
using us = std::chrono::microseconds;
252+
uint64_t usStart =
253+
std::chrono::duration_cast<us>(start - global_start).count();
254+
uint64_t usDelta =
255+
std::chrono::duration_cast<us>(end - start).count();
256+
257+
trace
258+
<< "{\"ph\":\"X\", \"pid\":" << processId
259+
<< ", \"tid\":" << threadId
260+
<< ", \"name\":\"" << name
261+
<< "\", \"ts\":" << usStart
262+
<< ", \"dur\":" << usDelta
263+
<< args.str()
264+
<< "},\n";
265+
}
266+
}
267+
BENCHMARK_REGISTER_F(ofstream_text_fixture, ChromeCallLogging_WithTimes_WithArgs);
268+
166269
struct FILE_fixture : public benchmark::Fixture
167270
{
168271
FILE* trace;
272+
uint64_t fixture_processId;
273+
uint64_t fixture_threadId;
169274
test_clock::time_point global_start;
170275
test_clock::time_point start;
171276
test_clock::time_point end;
172277

278+
static const size_t BUFFER_SIZE = 16 * 1024;
279+
char buffer[BUFFER_SIZE];
280+
173281
virtual void SetUp(benchmark::State& state) override {
174282
global_start = test_clock::now();
175283

176284
trace = fopen( "dummy_trace_FILE.json", "wb" );
177-
fprintf(trace, "[\n");
285+
fprintf(trace, "[ \n");
178286

179287
start = test_clock::now();
180288

181-
uint64_t processId = GET_PROCESS_ID();
182-
uint64_t threadId = GET_THREAD_ID();
289+
fixture_processId = GET_PROCESS_ID();
290+
fixture_threadId = GET_THREAD_ID();
183291
std::string processName = "dummy_process";
184292
fprintf(trace, "{\"ph\":\"M\", \"name\":\"process_name\", \"pid\":%" PRIu64 ", \"tid\":%" PRIu64 ", \"args\":{\"name\":\"%s\"}},\n",
185-
processId,
186-
threadId,
293+
fixture_processId,
294+
fixture_threadId,
187295
processName.c_str() );
188296

189297
end = test_clock::now();
@@ -197,7 +305,7 @@ BENCHMARK_DEFINE_F(FILE_fixture, ChromeCallLogging_WithTimes_WithArgs)(benchmark
197305
{
198306
while(state.KeepRunning()) {
199307
std::string name;
200-
name += __FUNCTION__;
308+
name += "test_function";
201309

202310
std::ostringstream args;
203311

@@ -225,7 +333,7 @@ BENCHMARK_DEFINE_F(FILE_fixture, ChromeCallLogging_WithTimes_WithArgs_nospaces)(
225333
{
226334
while(state.KeepRunning()) {
227335
std::string name;
228-
name += __FUNCTION__;
336+
name += "test_function";
229337

230338
std::ostringstream args;
231339

@@ -249,5 +357,92 @@ BENCHMARK_DEFINE_F(FILE_fixture, ChromeCallLogging_WithTimes_WithArgs_nospaces)(
249357
}
250358
BENCHMARK_REGISTER_F(FILE_fixture, ChromeCallLogging_WithTimes_WithArgs_nospaces);
251359

360+
BENCHMARK_DEFINE_F(FILE_fixture, ChromeCallLogging_WithTimes_WithArgs_nospaces_fixturepid)(benchmark::State& state)
361+
{
362+
while(state.KeepRunning()) {
363+
std::string name;
364+
name += "test_function";
365+
366+
std::ostringstream args;
367+
368+
uint64_t threadId = GET_THREAD_ID();
369+
370+
using us = std::chrono::microseconds;
371+
uint64_t usStart =
372+
std::chrono::duration_cast<us>(start - global_start).count();
373+
uint64_t usDelta =
374+
std::chrono::duration_cast<us>(end - start).count();
375+
376+
fprintf(trace, "{\"ph\":\"X\",\"pid\":%" PRIu64 ",\"tid\":%" PRIu64 ",\"name\":\"%s\",\"ts\":%" PRIu64 ",\"dur\":%" PRIu64 "%s},\n",
377+
fixture_processId,
378+
threadId,
379+
name.c_str(),
380+
usStart,
381+
usDelta,
382+
args.str().c_str() );
383+
}
384+
}
385+
BENCHMARK_REGISTER_F(FILE_fixture, ChromeCallLogging_WithTimes_WithArgs_nospaces_fixturepid);
386+
387+
BENCHMARK_DEFINE_F(FILE_fixture, ChromeCallLogging_WithTimes_WithArgs_nospaces_sprintf_fprintf)(benchmark::State& state)
388+
{
389+
while(state.KeepRunning()) {
390+
std::string name;
391+
name += "test_function";
392+
393+
std::ostringstream args;
394+
395+
uint64_t processId = GET_PROCESS_ID();
396+
uint64_t threadId = GET_THREAD_ID();
397+
398+
using us = std::chrono::microseconds;
399+
uint64_t usStart =
400+
std::chrono::duration_cast<us>(start - global_start).count();
401+
uint64_t usDelta =
402+
std::chrono::duration_cast<us>(end - start).count();
403+
404+
int size = CLI_SPRINTF(buffer, BUFFER_SIZE,
405+
"{\"ph\":\"X\",\"pid\":%" PRIu64 ",\"tid\":%" PRIu64 ",\"name\":\"%s\",\"ts\":%" PRIu64 ",\"dur\":%" PRIu64 "%s},\n",
406+
processId,
407+
threadId,
408+
name.c_str(),
409+
usStart,
410+
usDelta,
411+
args.str().c_str() );
412+
fprintf(trace, "%s", buffer);
413+
}
414+
}
415+
BENCHMARK_REGISTER_F(FILE_fixture, ChromeCallLogging_WithTimes_WithArgs_nospaces_sprintf_fprintf);
416+
417+
BENCHMARK_DEFINE_F(FILE_fixture, ChromeCallLogging_WithTimes_WithArgs_nospaces_sprintf_fwrite)(benchmark::State& state)
418+
{
419+
while(state.KeepRunning()) {
420+
std::string name;
421+
name += "test_function";
422+
423+
std::ostringstream args;
424+
425+
uint64_t processId = GET_PROCESS_ID();
426+
uint64_t threadId = GET_THREAD_ID();
427+
428+
using us = std::chrono::microseconds;
429+
uint64_t usStart =
430+
std::chrono::duration_cast<us>(start - global_start).count();
431+
uint64_t usDelta =
432+
std::chrono::duration_cast<us>(end - start).count();
433+
434+
int size = CLI_SPRINTF(buffer, BUFFER_SIZE,
435+
"{\"ph\":\"X\",\"pid\":%" PRIu64 ",\"tid\":%" PRIu64 ",\"name\":\"%s\",\"ts\":%" PRIu64 ",\"dur\":%" PRIu64 "%s},\n",
436+
processId,
437+
threadId,
438+
name.c_str(),
439+
usStart,
440+
usDelta,
441+
args.str().c_str() );
442+
fwrite(buffer, size, 1, trace);
443+
}
444+
}
445+
BENCHMARK_REGISTER_F(FILE_fixture, ChromeCallLogging_WithTimes_WithArgs_nospaces_sprintf_fwrite);
446+
252447

253448
BENCHMARK_MAIN();

0 commit comments

Comments
 (0)