|
| 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 | +} |
0 commit comments