Skip to content

Commit

Permalink
fix(benchmark): avoid compiler optimizations in benchmarks loops.
Browse files Browse the repository at this point in the history
Signed-off-by: Federico Di Pierro <[email protected]>
  • Loading branch information
FedeDP authored and poiana committed Sep 26, 2024
1 parent 0eb7be3 commit 73bcad2
Showing 1 changed file with 20 additions and 15 deletions.
35 changes: 20 additions & 15 deletions benchmark/libsinsp/utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,47 +20,52 @@ limitations under the License.
#include <benchmark/benchmark.h>

static void BM_sinsp_split(benchmark::State& state) {
std::string str = "hello,world,";
for(auto _ : state) {
sinsp_split(str, ',');
std::string str = "hello,world,";
benchmark::DoNotOptimize(sinsp_split(str, ','));
}
}
BENCHMARK(BM_sinsp_split);

static void BM_sinsp_concatenate_paths_relative_path(benchmark::State& state) {
std::string path1 = "/tmp/";
std::string path2 = "foo/bar";
for(auto _ : state) {
sinsp_utils::concatenate_paths(path1, path2);
std::string path1 = "/tmp/";
std::string path2 = "foo/bar";
benchmark::DoNotOptimize(sinsp_utils::concatenate_paths(path1, path2));
}
}
BENCHMARK(BM_sinsp_concatenate_paths_relative_path);

static void BM_sinsp_concatenate_paths_empty_path(benchmark::State& state) {
std::string path1 = "/tmp/";
std::string path2 = "";
for(auto _ : state) {
sinsp_utils::concatenate_paths(path1, path2);
std::string path1 = "/tmp/";
std::string path2 = "";
benchmark::DoNotOptimize(sinsp_utils::concatenate_paths(path1, path2));
}
}
BENCHMARK(BM_sinsp_concatenate_paths_empty_path);

static void BM_sinsp_concatenate_paths_absolute_path(benchmark::State& state) {
std::string path1 = "/tmp/";
std::string path2 = "/foo/bar";
for(auto _ : state) {
sinsp_utils::concatenate_paths(path1, path2);
std::string path1 = "/tmp/";
std::string path2 = "/foo/bar";
benchmark::DoNotOptimize(sinsp_utils::concatenate_paths(path1, path2));
}
}
BENCHMARK(BM_sinsp_concatenate_paths_absolute_path);

static void BM_sinsp_split_container_image(benchmark::State& state) {
std::string container_image =
"localhost:12345/library/"
"busybox:1.27.2@sha256:da39a3ee5e6b4b0d3255bfef95601890afd80709";
std::string hostname, port, name, tag, digest;
for(auto _ : state) {
std::string container_image =
"localhost:12345/library/"
"busybox:1.27.2@sha256:da39a3ee5e6b4b0d3255bfef95601890afd80709";
std::string hostname, port, name, tag, digest;
sinsp_utils::split_container_image(container_image, hostname, port, name, tag, digest);
benchmark::DoNotOptimize(hostname);
benchmark::DoNotOptimize(port);
benchmark::DoNotOptimize(name);
benchmark::DoNotOptimize(tag);
benchmark::DoNotOptimize(digest);
}
}
BENCHMARK(BM_sinsp_split_container_image);

0 comments on commit 73bcad2

Please sign in to comment.