File indexing completed on 2025-01-18 10:17:36
0001
0002
0003
0004
0005
0006 #include "JBenchUtils.h"
0007
0008
0009 void JBenchUtils::set_seed(size_t event_number, std::string caller_name)
0010 {
0011 std::hash<std::string> hasher;
0012 long seed = event_number ^ hasher(caller_name);
0013 m_generator = std::mt19937(seed);
0014 }
0015
0016
0017 size_t JBenchUtils::rand_size(size_t avg, double spread) {
0018 auto delta = static_cast<size_t>(avg*spread);
0019 std::uniform_int_distribution<size_t> distribution(avg-delta, avg+delta);
0020 return distribution(m_generator);
0021 }
0022
0023
0024 int JBenchUtils::randint(int min, int max) {
0025 std::uniform_int_distribution<int> distribution(min, max);
0026 return distribution(m_generator);
0027 }
0028
0029 double JBenchUtils::randdouble(double min, double max) {
0030 std::uniform_real_distribution<double> dist(min, max);
0031 return dist(m_generator);
0032 }
0033
0034 float JBenchUtils::randfloat(float min, float max) {
0035 std::uniform_real_distribution<float> dist(min, max);
0036 return dist(m_generator);
0037 }
0038
0039 uint64_t JBenchUtils::consume_cpu_ms(uint64_t millisecs, double spread, bool fix_flops) {
0040
0041 uint64_t sampled = rand_size(millisecs, spread);
0042 uint64_t result = 0;
0043
0044 if (fix_flops) {
0045
0046 const uint64_t appx_iters_per_millisec = 14000;
0047 sampled *= appx_iters_per_millisec;
0048
0049 for (uint64_t i=0; i<sampled; ++i) {
0050 double a = (m_generator)();
0051 double b = sqrt(a * pow(1.23, -a)) / a;
0052 result += long(b);
0053 }
0054 }
0055 else {
0056
0057 auto duration = std::chrono::milliseconds(sampled);
0058 auto start_time = std::chrono::steady_clock::now();
0059 while ((std::chrono::steady_clock::now() - start_time) < duration) {
0060
0061 double a = (m_generator)();
0062 double b = sqrt(a * pow(1.23, -a)) / a;
0063 result += long(b);
0064 }
0065 }
0066 return result;
0067 }
0068
0069 uint64_t JBenchUtils::read_memory(const std::vector<char>& buffer) {
0070
0071 auto length = buffer.size();
0072 uint64_t sum = 0;
0073 for (unsigned i=0; i<length; ++i) {
0074 sum += buffer[i];
0075 }
0076 return sum;
0077 }
0078
0079 uint64_t JBenchUtils::write_memory(std::vector<char>& buffer, uint64_t bytes, double spread) {
0080
0081 uint64_t sampled = rand_size(bytes, spread);
0082 for (unsigned i=0; i<sampled; ++i) {
0083 buffer.push_back(2);
0084 }
0085 return sampled*2;
0086 }
0087
0088
0089
0090
0091