This is an automated email from the ASF dual-hosted git repository.

cmcfarlen pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/trafficserver.git


The following commit(s) were added to refs/heads/master by this push:
     new 287d534e67 Fix benchmarks, add benchmark_Random (#12616)
287d534e67 is described below

commit 287d534e67c690434f5d0fa0bfa9200cb8d5953b
Author: Chris McFarlen <[email protected]>
AuthorDate: Tue Oct 28 20:03:46 2025 -0400

    Fix benchmarks, add benchmark_Random (#12616)
    
    * Fix benchmarks, add benchmark_Random
    
    * Add distribution test
    
    * remove iterations
---
 tools/benchmark/CMakeLists.txt              |   5 +-
 tools/benchmark/benchmark_EventSystem.cc    |   7 +-
 tools/benchmark/benchmark_FreeList.cc       |   3 +-
 tools/benchmark/benchmark_ProxyAllocator.cc |   1 +
 tools/benchmark/benchmark_Random.cc         | 101 ++++++++++++++++++++++++++++
 tools/benchmark/benchmark_SharedMutex.cc    |   3 +-
 6 files changed, 114 insertions(+), 6 deletions(-)

diff --git a/tools/benchmark/CMakeLists.txt b/tools/benchmark/CMakeLists.txt
index 74bf072e08..6a062bfaad 100644
--- a/tools/benchmark/CMakeLists.txt
+++ b/tools/benchmark/CMakeLists.txt
@@ -28,7 +28,10 @@ if(TS_USE_HWLOC)
 endif()
 
 add_executable(benchmark_ProxyAllocator benchmark_ProxyAllocator.cc)
-target_link_libraries(benchmark_ProxyAllocator PRIVATE Catch2::Catch2 
ts::tscore ts::inkevent libswoc::libswoc)
+target_link_libraries(benchmark_ProxyAllocator PRIVATE Catch2::Catch2WithMain 
ts::tscore ts::inkevent libswoc::libswoc)
 
 add_executable(benchmark_SharedMutex benchmark_SharedMutex.cc)
 target_link_libraries(benchmark_SharedMutex PRIVATE Catch2::Catch2 ts::tscore 
libswoc::libswoc)
+
+add_executable(benchmark_Random benchmark_Random.cc)
+target_link_libraries(benchmark_Random PRIVATE Catch2::Catch2WithMain 
ts::tscore)
diff --git a/tools/benchmark/benchmark_EventSystem.cc 
b/tools/benchmark/benchmark_EventSystem.cc
index 4410d16691..faa759c216 100644
--- a/tools/benchmark/benchmark_EventSystem.cc
+++ b/tools/benchmark/benchmark_EventSystem.cc
@@ -26,6 +26,7 @@
 #include <catch2/reporters/catch_reporter_registrars.hpp>
 #include <catch2/interfaces/catch_interfaces_config.hpp>
 #include <catch2/catch_session.hpp>
+#include <catch2/benchmark/catch_benchmark.hpp>
 
 #include "iocore/eventsystem/Continuation.h"
 #include "iocore/eventsystem/EventSystem.h"
@@ -81,8 +82,8 @@ TEST_CASE("event process benchmark", "")
   };
 }
 
-struct EventProcessorListener : Catch::TestEventListenerBase {
-  using TestEventListenerBase::TestEventListenerBase;
+struct EventProcessorListener : Catch::EventListenerBase {
+  using EventListenerBase::EventListenerBase;
 
   void
   testRunStarting(Catch::TestRunInfo const & /* testRunInfo ATS_UNUSED */) 
override
@@ -108,7 +109,7 @@ main(int argc, char *argv[])
 {
   Catch::Session session;
 
-  using namespace Catch::clara;
+  using namespace Catch::Clara;
 
   auto cli = session.cli() | Opt(nevents, "n")["--ts-nevents"]("number of 
events (default: 1)\n") |
              Opt(nthreads, "n")["--ts-nthreads"]("number of ethreads (default: 
1)\n");
diff --git a/tools/benchmark/benchmark_FreeList.cc 
b/tools/benchmark/benchmark_FreeList.cc
index 29c3b46d8d..88eae093e3 100644
--- a/tools/benchmark/benchmark_FreeList.cc
+++ b/tools/benchmark/benchmark_FreeList.cc
@@ -23,6 +23,7 @@
 
 #include <catch2/catch_test_macros.hpp>
 #include <catch2/catch_session.hpp>
+#include <catch2/benchmark/catch_benchmark.hpp>
 
 #include "tscore/ink_hw.h"
 #include "tscore/ink_thread.h"
@@ -183,7 +184,7 @@ main(int argc, char *argv[])
 {
   Catch::Session session;
 
-  using namespace Catch::clara;
+  using namespace Catch::Clara;
 
   bool opt_enable_hugepage = false;
 
diff --git a/tools/benchmark/benchmark_ProxyAllocator.cc 
b/tools/benchmark/benchmark_ProxyAllocator.cc
index bed12f4729..232b559a5f 100644
--- a/tools/benchmark/benchmark_ProxyAllocator.cc
+++ b/tools/benchmark/benchmark_ProxyAllocator.cc
@@ -23,6 +23,7 @@ limitations under the License.
 
 #define CATCH_CONFIG_ENABLE_BENCHMARKING
 #include <catch2/catch_test_macros.hpp>
+#include <catch2/benchmark/catch_benchmark.hpp>
 
 #include "iocore/eventsystem/Thread.h"
 #include "tscore/Allocator.h"
diff --git a/tools/benchmark/benchmark_Random.cc 
b/tools/benchmark/benchmark_Random.cc
new file mode 100644
index 0000000000..d1a7e7f762
--- /dev/null
+++ b/tools/benchmark/benchmark_Random.cc
@@ -0,0 +1,101 @@
+
+/** @file
+
+Simple benchmark for ProxyAllocator
+
+@section license License
+
+Licensed to the Apache Software Foundation (ASF) under one
+or more contributor license agreements.  See the NOTICE file
+distributed with this work for additional information
+regarding copyright ownership.  The ASF licenses this file
+to you under the Apache License, Version 2.0 (the
+"License"); you may not use this file except in compliance
+with the License.  You may obtain a copy of the License at
+
+    http://www.apache.org/licenses/LICENSE-2.0
+
+Unless required by applicable law or agreed to in writing, software
+distributed under the License is distributed on an "AS IS" BASIS,
+WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+See the License for the specific language governing permissions and
+limitations under the License.
+*/
+
+#include "tscore/ink_rand.h"
+#include <cmath>
+#include <limits>
+#define CATCH_CONFIG_ENABLE_BENCHMARKING
+#include <catch2/catch_test_macros.hpp>
+#include <catch2/benchmark/catch_benchmark.hpp>
+#include <catch2/generators/catch_generators_random.hpp>
+
+#include "tscore/Random.h"
+
+TEST_CASE("BenchRandom", "[bench][random]")
+{
+  InkRand gen(42);
+  ts::Random::seed(13);
+
+  BENCHMARK("IncRand")
+  {
+    return gen.random();
+  };
+
+  BENCHMARK("ts::Random")
+  {
+    return ts::Random::random();
+  };
+
+  std::mt19937_64 mt;
+  BENCHMARK("std::mt19937_64")
+  {
+    return mt();
+  };
+
+  std::ranlux48_base rb;
+  BENCHMARK("std::ranlux48_base")
+  {
+    return rb();
+  };
+
+  std::ranlux24_base rb24;
+  BENCHMARK("std::ranlux24_base")
+  {
+    return rb24();
+  };
+
+  std::uniform_int_distribution<uint64_t> mtdist{0, UINT64_MAX};
+
+  BENCHMARK("std::uniform_int_distribution")
+  {
+    return mtdist(mt);
+  };
+}
+
+TEST_CASE("RandomDistribution", "[random][distribution]")
+{
+  auto          g = 
Catch::Generators::random(std::numeric_limits<uint64_t>::min(), 
std::numeric_limits<uint64_t>::max());
+  InkRand       gen(g.get());
+  int           iterations      = 1000000;
+  constexpr int buckets         = 100;
+  int           counts[buckets] = {0};
+
+  for (int i = 0; i < iterations; i++) {
+    counts[gen.random() % buckets]++;
+  }
+
+  double expected = static_cast<double>(iterations) / 
static_cast<double>(buckets);
+
+  double min = std::numeric_limits<double>::max();
+  double max = std::numeric_limits<double>::min();
+
+  for (int count : counts) {
+    double ratio = static_cast<double>(count) / expected;
+
+    min = std::min(ratio, min);
+    max = std::max(ratio, max);
+  }
+  REQUIRE(0.95 < min);
+  REQUIRE(max < 1.05);
+}
diff --git a/tools/benchmark/benchmark_SharedMutex.cc 
b/tools/benchmark/benchmark_SharedMutex.cc
index e6b0f14cf2..33895c71c7 100644
--- a/tools/benchmark/benchmark_SharedMutex.cc
+++ b/tools/benchmark/benchmark_SharedMutex.cc
@@ -28,6 +28,7 @@
 
 #include <catch2/catch_test_macros.hpp>
 #include <catch2/catch_session.hpp>
+#include <catch2/benchmark/catch_benchmark.hpp>
 
 #include "tsutil/Bravo.h"
 
@@ -113,7 +114,7 @@ main(int argc, char *argv[])
 {
   Catch::Session session;
 
-  using namespace Catch::clara;
+  using namespace Catch::Clara;
 
   // clang-format off
   auto cli = session.cli() |

Reply via email to