Skip to content

Commit 5a25405

Browse files
committed
Add a benchmark for log showing the difference of switching between log_write_impl and log_write_impl_va ( #140 ).
1 parent 0524afc commit 5a25405

File tree

3 files changed

+250
-0
lines changed

3 files changed

+250
-0
lines changed

source/benchmarks/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ endif()
5555

5656
include(CTest)
5757

58+
add_subdirectory(log_bench)
5859
add_subdirectory(metacall_py_c_api_bench)
5960
add_subdirectory(metacall_py_call_bench)
6061
add_subdirectory(metacall_py_init_bench)
Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
#
2+
# Executable name and options
3+
#
4+
5+
# Target name
6+
set(target log-bench)
7+
message(STATUS "Benchmark ${target}")
8+
9+
#
10+
# Compiler warnings
11+
#
12+
13+
include(Warnings)
14+
15+
#
16+
# Compiler security
17+
#
18+
19+
include(SecurityFlags)
20+
21+
#
22+
# Sources
23+
#
24+
25+
set(include_path "${CMAKE_CURRENT_SOURCE_DIR}/include/${target}")
26+
set(source_path "${CMAKE_CURRENT_SOURCE_DIR}/source")
27+
28+
set(sources
29+
${source_path}/log_bench.cpp
30+
)
31+
32+
# Group source files
33+
set(header_group "Header Files (API)")
34+
set(source_group "Source Files")
35+
source_group_by_path(${include_path} "\\\\.h$|\\\\.hpp$"
36+
${header_group} ${headers})
37+
source_group_by_path(${source_path} "\\\\.cpp$|\\\\.c$|\\\\.h$|\\\\.hpp$"
38+
${source_group} ${sources})
39+
40+
#
41+
# Create executable
42+
#
43+
44+
# Build executable
45+
add_executable(${target}
46+
${sources}
47+
)
48+
49+
# Create namespaced alias
50+
add_executable(${META_PROJECT_NAME}::${target} ALIAS ${target})
51+
52+
#
53+
# Project options
54+
#
55+
56+
set_target_properties(${target}
57+
PROPERTIES
58+
${DEFAULT_PROJECT_OPTIONS}
59+
FOLDER "${IDE_FOLDER}"
60+
)
61+
62+
#
63+
# Include directories
64+
#
65+
66+
target_include_directories(${target}
67+
PRIVATE
68+
${DEFAULT_INCLUDE_DIRECTORIES}
69+
${PROJECT_BINARY_DIR}/source/include
70+
)
71+
72+
#
73+
# Libraries
74+
#
75+
76+
target_link_libraries(${target}
77+
PRIVATE
78+
${DEFAULT_LIBRARIES}
79+
80+
GBench
81+
82+
${META_PROJECT_NAME}::version
83+
${META_PROJECT_NAME}::preprocessor
84+
${META_PROJECT_NAME}::format
85+
${META_PROJECT_NAME}::threading
86+
${META_PROJECT_NAME}::log
87+
)
88+
89+
#
90+
# Compile definitions
91+
#
92+
93+
target_compile_definitions(${target}
94+
PRIVATE
95+
${DEFAULT_COMPILE_DEFINITIONS}
96+
)
97+
98+
#
99+
# Compile options
100+
#
101+
102+
target_compile_options(${target}
103+
PRIVATE
104+
${DEFAULT_COMPILE_OPTIONS}
105+
)
106+
107+
#
108+
# Linker options
109+
#
110+
111+
target_link_libraries(${target}
112+
PRIVATE
113+
${DEFAULT_LINKER_OPTIONS}
114+
)
115+
116+
#
117+
# Define test
118+
#
119+
120+
add_test(NAME ${target}
121+
COMMAND $<TARGET_FILE:${target}>
122+
)
123+
124+
#
125+
# Define dependencies
126+
#
127+
128+
add_dependencies(${target}
129+
log
130+
)
131+
132+
#
133+
# Define test properties
134+
#
135+
136+
set_property(TEST ${target}
137+
PROPERTY LABELS ${target}
138+
)
139+
140+
include(TestEnvironmentVariables)
141+
142+
test_environment_variables(${target}
143+
""
144+
${TESTS_ENVIRONMENT_VARIABLES}
145+
)
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
/*
2+
* MetaCall Library by Parra Studios
3+
* A library for providing a foreign function interface calls.
4+
*
5+
* Copyright (C) 2016 - 2021 Vicente Eduardo Ferrer Garcia <[email protected]>
6+
*
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
*
19+
*/
20+
21+
#include <benchmark/benchmark.h>
22+
23+
#include <log/log.h>
24+
#include <metacall/metacall_loaders.h>
25+
26+
static int stream_write(void *, const char *, const size_t)
27+
{
28+
// Disable stream write so we do not count stdout on the benchmark
29+
return 0;
30+
}
31+
32+
static int stream_flush(void *)
33+
{
34+
// Disable stream flush so we do not count stdout on the benchmark
35+
return 0;
36+
}
37+
38+
class log_bench : public benchmark::Fixture
39+
{
40+
public:
41+
void SetUp(benchmark::State &state)
42+
{
43+
if (log_configure("metacall",
44+
log_policy_format_text(),
45+
log_policy_schedule_sync(),
46+
log_policy_storage_sequential(),
47+
log_policy_stream_custom(NULL, &stream_write, &stream_flush)) != 0)
48+
{
49+
state.SkipWithError("Error creating the log");
50+
}
51+
}
52+
53+
void TearDown(benchmark::State &)
54+
{
55+
}
56+
};
57+
58+
BENCHMARK_DEFINE_F(log_bench, call_macro)
59+
(benchmark::State &state)
60+
{
61+
const int64_t call_count = 10000;
62+
63+
for (auto _ : state)
64+
{
65+
for (int64_t it = 0; it < call_count; ++it)
66+
{
67+
benchmark::DoNotOptimize(log_write("metacall", LOG_LEVEL_ERROR, "Message"));
68+
}
69+
}
70+
71+
state.SetLabel("Log Benchmark - Call Macro");
72+
state.SetItemsProcessed(call_count);
73+
}
74+
75+
BENCHMARK_REGISTER_F(log_bench, call_macro)
76+
->Threads(1)
77+
->Unit(benchmark::kMillisecond)
78+
->Iterations(1)
79+
->Repetitions(3);
80+
81+
BENCHMARK_DEFINE_F(log_bench, call_va)
82+
(benchmark::State &state)
83+
{
84+
const int64_t call_count = 10000;
85+
86+
for (auto _ : state)
87+
{
88+
for (int64_t it = 0; it < call_count; ++it)
89+
{
90+
benchmark::DoNotOptimize(log_write_impl_va("metacall", LOG_PREPROCESSOR_LINE, log_record_function(), __FILE__, LOG_LEVEL_ERROR, "Message"));
91+
}
92+
}
93+
94+
state.SetLabel("Log Benchmark - Call Variadic");
95+
state.SetItemsProcessed(call_count);
96+
}
97+
98+
BENCHMARK_REGISTER_F(log_bench, call_va)
99+
->Threads(1)
100+
->Unit(benchmark::kMillisecond)
101+
->Iterations(1)
102+
->Repetitions(3);
103+
104+
BENCHMARK_MAIN();

0 commit comments

Comments
 (0)