Skip to content

Commit 287d534

Browse files
authored
Fix benchmarks, add benchmark_Random (#12616)
* Fix benchmarks, add benchmark_Random * Add distribution test * remove iterations
1 parent 245506c commit 287d534

File tree

6 files changed

+114
-6
lines changed

6 files changed

+114
-6
lines changed

tools/benchmark/CMakeLists.txt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,10 @@ if(TS_USE_HWLOC)
2828
endif()
2929

3030
add_executable(benchmark_ProxyAllocator benchmark_ProxyAllocator.cc)
31-
target_link_libraries(benchmark_ProxyAllocator PRIVATE Catch2::Catch2 ts::tscore ts::inkevent libswoc::libswoc)
31+
target_link_libraries(benchmark_ProxyAllocator PRIVATE Catch2::Catch2WithMain ts::tscore ts::inkevent libswoc::libswoc)
3232

3333
add_executable(benchmark_SharedMutex benchmark_SharedMutex.cc)
3434
target_link_libraries(benchmark_SharedMutex PRIVATE Catch2::Catch2 ts::tscore libswoc::libswoc)
35+
36+
add_executable(benchmark_Random benchmark_Random.cc)
37+
target_link_libraries(benchmark_Random PRIVATE Catch2::Catch2WithMain ts::tscore)

tools/benchmark/benchmark_EventSystem.cc

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#include <catch2/reporters/catch_reporter_registrars.hpp>
2727
#include <catch2/interfaces/catch_interfaces_config.hpp>
2828
#include <catch2/catch_session.hpp>
29+
#include <catch2/benchmark/catch_benchmark.hpp>
2930

3031
#include "iocore/eventsystem/Continuation.h"
3132
#include "iocore/eventsystem/EventSystem.h"
@@ -81,8 +82,8 @@ TEST_CASE("event process benchmark", "")
8182
};
8283
}
8384

84-
struct EventProcessorListener : Catch::TestEventListenerBase {
85-
using TestEventListenerBase::TestEventListenerBase;
85+
struct EventProcessorListener : Catch::EventListenerBase {
86+
using EventListenerBase::EventListenerBase;
8687

8788
void
8889
testRunStarting(Catch::TestRunInfo const & /* testRunInfo ATS_UNUSED */) override
@@ -108,7 +109,7 @@ main(int argc, char *argv[])
108109
{
109110
Catch::Session session;
110111

111-
using namespace Catch::clara;
112+
using namespace Catch::Clara;
112113

113114
auto cli = session.cli() | Opt(nevents, "n")["--ts-nevents"]("number of events (default: 1)\n") |
114115
Opt(nthreads, "n")["--ts-nthreads"]("number of ethreads (default: 1)\n");

tools/benchmark/benchmark_FreeList.cc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323

2424
#include <catch2/catch_test_macros.hpp>
2525
#include <catch2/catch_session.hpp>
26+
#include <catch2/benchmark/catch_benchmark.hpp>
2627

2728
#include "tscore/ink_hw.h"
2829
#include "tscore/ink_thread.h"
@@ -183,7 +184,7 @@ main(int argc, char *argv[])
183184
{
184185
Catch::Session session;
185186

186-
using namespace Catch::clara;
187+
using namespace Catch::Clara;
187188

188189
bool opt_enable_hugepage = false;
189190

tools/benchmark/benchmark_ProxyAllocator.cc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ limitations under the License.
2323

2424
#define CATCH_CONFIG_ENABLE_BENCHMARKING
2525
#include <catch2/catch_test_macros.hpp>
26+
#include <catch2/benchmark/catch_benchmark.hpp>
2627

2728
#include "iocore/eventsystem/Thread.h"
2829
#include "tscore/Allocator.h"
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
2+
/** @file
3+
4+
Simple benchmark for ProxyAllocator
5+
6+
@section license License
7+
8+
Licensed to the Apache Software Foundation (ASF) under one
9+
or more contributor license agreements. See the NOTICE file
10+
distributed with this work for additional information
11+
regarding copyright ownership. The ASF licenses this file
12+
to you under the Apache License, Version 2.0 (the
13+
"License"); you may not use this file except in compliance
14+
with the License. You may obtain a copy of the License at
15+
16+
http://www.apache.org/licenses/LICENSE-2.0
17+
18+
Unless required by applicable law or agreed to in writing, software
19+
distributed under the License is distributed on an "AS IS" BASIS,
20+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
21+
See the License for the specific language governing permissions and
22+
limitations under the License.
23+
*/
24+
25+
#include "tscore/ink_rand.h"
26+
#include <cmath>
27+
#include <limits>
28+
#define CATCH_CONFIG_ENABLE_BENCHMARKING
29+
#include <catch2/catch_test_macros.hpp>
30+
#include <catch2/benchmark/catch_benchmark.hpp>
31+
#include <catch2/generators/catch_generators_random.hpp>
32+
33+
#include "tscore/Random.h"
34+
35+
TEST_CASE("BenchRandom", "[bench][random]")
36+
{
37+
InkRand gen(42);
38+
ts::Random::seed(13);
39+
40+
BENCHMARK("IncRand")
41+
{
42+
return gen.random();
43+
};
44+
45+
BENCHMARK("ts::Random")
46+
{
47+
return ts::Random::random();
48+
};
49+
50+
std::mt19937_64 mt;
51+
BENCHMARK("std::mt19937_64")
52+
{
53+
return mt();
54+
};
55+
56+
std::ranlux48_base rb;
57+
BENCHMARK("std::ranlux48_base")
58+
{
59+
return rb();
60+
};
61+
62+
std::ranlux24_base rb24;
63+
BENCHMARK("std::ranlux24_base")
64+
{
65+
return rb24();
66+
};
67+
68+
std::uniform_int_distribution<uint64_t> mtdist{0, UINT64_MAX};
69+
70+
BENCHMARK("std::uniform_int_distribution")
71+
{
72+
return mtdist(mt);
73+
};
74+
}
75+
76+
TEST_CASE("RandomDistribution", "[random][distribution]")
77+
{
78+
auto g = Catch::Generators::random(std::numeric_limits<uint64_t>::min(), std::numeric_limits<uint64_t>::max());
79+
InkRand gen(g.get());
80+
int iterations = 1000000;
81+
constexpr int buckets = 100;
82+
int counts[buckets] = {0};
83+
84+
for (int i = 0; i < iterations; i++) {
85+
counts[gen.random() % buckets]++;
86+
}
87+
88+
double expected = static_cast<double>(iterations) / static_cast<double>(buckets);
89+
90+
double min = std::numeric_limits<double>::max();
91+
double max = std::numeric_limits<double>::min();
92+
93+
for (int count : counts) {
94+
double ratio = static_cast<double>(count) / expected;
95+
96+
min = std::min(ratio, min);
97+
max = std::max(ratio, max);
98+
}
99+
REQUIRE(0.95 < min);
100+
REQUIRE(max < 1.05);
101+
}

tools/benchmark/benchmark_SharedMutex.cc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828

2929
#include <catch2/catch_test_macros.hpp>
3030
#include <catch2/catch_session.hpp>
31+
#include <catch2/benchmark/catch_benchmark.hpp>
3132

3233
#include "tsutil/Bravo.h"
3334

@@ -113,7 +114,7 @@ main(int argc, char *argv[])
113114
{
114115
Catch::Session session;
115116

116-
using namespace Catch::clara;
117+
using namespace Catch::Clara;
117118

118119
// clang-format off
119120
auto cli = session.cli() |

0 commit comments

Comments
 (0)