Skip to content

Commit

Permalink
chore: add more benchmarks
Browse files Browse the repository at this point in the history
Add more benchmarks.
  • Loading branch information
melg8 committed Dec 17, 2024
1 parent 23b5a29 commit 646f25e
Show file tree
Hide file tree
Showing 4 changed files with 417 additions and 0 deletions.
5 changes: 5 additions & 0 deletions sources/swarm/library/sources/crypt/blowfish_cipher.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// SPDX-FileCopyrightText: © 2024 Melg Eight <[email protected]>
//
// SPDX-License-Identifier: MIT

#include <blowfish_cipher.h>
12 changes: 12 additions & 0 deletions sources/swarm/library/sources/crypt/blowfish_cipher.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// SPDX-FileCopyrightText: © 2024 Melg Eight <[email protected]>
//
// SPDX-License-Identifier: MIT

#ifndef BLOWFISH_CIPHER_H
#define BLOWFISH_CIPHER_H

#include <openssl/blowfish.h>

namespace swarm {} // namespace swarm

#endif // BLOWFISH_CIPHER_H
84 changes: 84 additions & 0 deletions sources/swarm/tests/benchmark_tests/sources/crypt/bench.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
#include <random>
#include <unordered_set>

[[nodiscard]] inline std::vector<int> RandN(int n) noexcept {
if (n <= 0) {
return std::vector<int>{};
}
std::random_device rd;
std::mt19937 gen(rd());
std::uniform_int_distribution<int> distribution;
std::vector<int> result;
result.reserve(n);
for (int i = 0; i < n; ++i) {
result.push_back(distribution(gen));
}
return result;
}

static void RandNBench(benchmark::State& state) {
// Code inside this loop is measured repeatedly
for (auto _ : state) {
const auto result = RandN(10'000);
if (result.size() != 10'000) {
abort();
}
benchmark::DoNotOptimize(result);
}
}
// Register the function as a benchmark
BENCHMARK(RandNBench);

[[nodiscard]] inline std::vector<int> RandN1(int n) noexcept {
if (n <= 0) {
return std::vector<int>{};
}
std::random_device rd;
std::mt19937 gen(rd());
std::uniform_int_distribution<int> distribution;
std::vector<int> result(n, 0);
for (int i = 0; i < n; ++i) {
result[i] = distribution(gen);
}
return result;
}

static void RandN1Bench(benchmark::State& state) {
// Code inside this loop is measured repeatedly
for (auto _ : state) {
const auto result = RandN1(10'000);
if (result.size() != 10'000) {
abort();
}
benchmark::DoNotOptimize(result);
}
}
// Register the function as a benchmark
BENCHMARK(RandN1Bench);

[[nodiscard]] inline std::vector<int> UniqRandN1(int n) noexcept {
if (n <= 0) {
return std::vector<int>{};
}
std::random_device rd;
std::mt19937 gen(rd());
std::uniform_int_distribution<int> distribution;
std::unordered_set<int> unique;
while (unique.size() < n) {
unique.insert(distribution(gen));
}
return std::vector<int>{unique.begin(), unique.end()};
}

static void UniqRandN1Bench(benchmark::State& state) {
// Code inside this loop is measured repeatedly
for (auto _ : state) {
const auto result = UniqRandN1(10'000);
if (result.size() != 10'000) {
abort();
}
benchmark::DoNotOptimize(result);
}
}
// Register the function as a benchmark
BENCHMARK(UniqRandN1Bench);
Loading

0 comments on commit 646f25e

Please sign in to comment.