Skip to content

Commit

Permalink
Merge Branch Benchmark
Browse files Browse the repository at this point in the history
  • Loading branch information
Clemapfel authored Mar 13, 2022
2 parents 9fea8e0 + 19de778 commit b0bbb02
Show file tree
Hide file tree
Showing 22 changed files with 4,035 additions and 379 deletions.
132 changes: 81 additions & 51 deletions .benchmark/benchmark.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,27 +24,38 @@ namespace jluna

struct Result
{
const Duration _min;
const Duration _max;
const Duration _average;
const Duration _median;

const size_t _n_loops;
const std::string _exception_maybe;

[[nodiscard]] Result operator-(const Result& other)
{
assert(_exception_maybe == "");

return Result{
abs(this->_min - other._min),
abs(this->_max - other._max),
abs(this->_average - other._average),
abs(this->_median - other._median),
std::min(this->_n_loops, other._n_loops),
""
};
}
Result()
: _name(""),
_min(0),
_max(0),
_average(0),
_median(0),
_n_loops(-1),
_exception_maybe()
{}

Result(std::string name, Duration min, Duration max, Duration average, Duration median, size_t n_loops, std::string exception_maybe)
: _name(name),
_min(min),
_max(max),
_average(average),
_median(median),
_n_loops(n_loops),
_exception_maybe(exception_maybe)
{}

std::string _name;

Duration _min;
Duration _max;
Duration _average;
Duration _median;

float _overhead = 0;
std::string _compared_to = "self";

size_t _n_loops;
std::string _exception_maybe;
};

static void initialize()
Expand All @@ -53,12 +64,24 @@ namespace jluna
_results.clear();
}

template<typename Lambda_t>
static Benchmark::Result run_as_base(const std::string& name, size_t count, Lambda_t lambda, bool log = true)
{
_base = Benchmark::Result();
auto res = run(name, count, lambda, log);
_base = res;
return res;
}

template<typename Lambda_t>
static Benchmark::Result run(const std::string& name, size_t count, Lambda_t lambda, bool log = true)
{
if (count % 2 == 0)
count += 1;

State::collect_garbage();

std::cout << "[C++][LOG] Running \"" << name << "\"" << std::endl;
std::vector<Duration> runs;
runs.reserve(count);

Expand Down Expand Up @@ -93,6 +116,7 @@ namespace jluna
avg = avg / (runs.size() != 0 ? runs.size() : 1);

auto res = Result{
name,
runs.front(),
runs.back(),
avg,
Expand All @@ -101,49 +125,52 @@ namespace jluna
exception_maybe
};

/*
std::cout << name << std::endl;
static auto overhead = [](std::chrono::duration<double> a, std::chrono::duration<double> b) -> double {

for (size_t i = runs.size() - 20; i < runs.size(); ++i)
std::cout << std::chrono::duration_cast<std::chrono::duration<double, std::milli>>(runs.at(i)).count() << std::endl;
if (a.count() == 0)
return 0;

std::cout << "----------------------------------------" << std::endl;
*/
return (a < b ? 1 : -1) * (abs(a - b) / a); //(a < b ? (b - a) / (a + b) : -1 *((a - b) / a));
};
res._overhead = overhead(_base._median, res._median);
if (res._overhead != 0)
res._compared_to = _base._name;

if (log)
_results.push_back({name, res});
_results.push_back(res);

return _results.back().second;
std::cout << "[C++][LOG] done." << std::endl;
return _results.back();
}

static void add_results(const std::string name, Benchmark::Result result)
{
_results.push_back({name, result});
_results.push_back(result);
}

static void conclude()
{
for (auto& pair : _results)
for (auto& res : _results)
{
auto min = std::chrono::duration_cast<std::chrono::duration<double, std::milli>>(pair.second._min).count();
auto avg = std::chrono::duration_cast<std::chrono::duration<double, std::milli>>(pair.second._average).count();
auto med = std::chrono::duration_cast<std::chrono::duration<double, std::milli>>(pair.second._median).count();
auto max = std::chrono::duration_cast<std::chrono::duration<double, std::milli>>(pair.second._max).count();
auto min = std::chrono::duration_cast<std::chrono::duration<double, std::milli>>(res._min).count();
auto avg = std::chrono::duration_cast<std::chrono::duration<double, std::milli>>(res._average).count();
auto med = std::chrono::duration_cast<std::chrono::duration<double, std::milli>>(res._median).count();
auto max = std::chrono::duration_cast<std::chrono::duration<double, std::milli>>(res._max).count();

std::cout << "┌────────────────────────────────\n";
std::cout << "" << pair.first << " (" << pair.second._n_loops << "): \n\n";
std::cout << "" << res._name << " (" << res._n_loops << "): \n\n";
std::cout << "│ Min : " << min << "ms" << std::endl;
std::cout << "│ Average: " << avg << "ms" << std::endl;
std::cout << "│ Max : " << max << "ms" << std::endl;
std::cout << "" << std::endl;
std::cout << "│ Median : " << med << "ms" << std::endl;
std::cout << "" << std::endl;
std::cout << "│ Overhead: " << (res._overhead * 100) << "%" << std::endl;


if (pair.second._exception_maybe != "")
if (res._exception_maybe != "")
{
std::cout << "" << std::endl;
std::cout << "│ Exception at run " << pair.second._n_loops << ":" << std::endl;
std::cout << "" << pair.second._exception_maybe << std::endl;
std::cout << "│ Exception at run " << res._n_loops << ":" << std::endl;
std::cout << "" << res._exception_maybe << std::endl;
}

std::cout << "└────────────────────────────────\n" << std::endl;
Expand All @@ -163,21 +190,23 @@ namespace jluna
file.open(name);

const std::string del = ",";
file << "name" << del << "count" << del << "min" << del << "max" << del << "average" << del << "median" << std::endl;
file << "name" << del << "count" << del << "min" << del << "max" << del << "average" << del << "median" << del << "overhead" << del << "compared_to" << std::endl;

for (auto& pair : _results)
for (auto& res : _results)
{
auto min = std::chrono::duration_cast<std::chrono::duration<double, std::milli>>(pair.second._min).count();
auto avg = std::chrono::duration_cast<std::chrono::duration<double, std::milli>>(pair.second._average).count();
auto med = std::chrono::duration_cast<std::chrono::duration<double, std::milli>>(pair.second._median).count();
auto max = std::chrono::duration_cast<std::chrono::duration<double, std::milli>>(pair.second._max).count();
auto min = std::chrono::duration_cast<std::chrono::duration<double, std::milli>>(res._min).count();
auto avg = std::chrono::duration_cast<std::chrono::duration<double, std::milli>>(res._average).count();
auto med = std::chrono::duration_cast<std::chrono::duration<double, std::milli>>(res._median).count();
auto max = std::chrono::duration_cast<std::chrono::duration<double, std::milli>>(res._max).count();

file << "\"" << pair.first << "\"" << del;
file << pair.second._n_loops << del;
file << "\"" << res._name << "\"" << del;
file << res._n_loops << del;
file << min << del;
file << max << del;
file << avg << del;
file << med << std::endl;
file << med << del;
file << res._overhead << del;
file << "\"" << res._compared_to << "\"" << std::endl;
}

file << std::endl;
Expand All @@ -187,8 +216,9 @@ namespace jluna
}

private:
static inline Benchmark::Result _base = Benchmark::Result();
static inline std::chrono::steady_clock _clock = std::chrono::steady_clock();
static inline std::vector<std::pair<std::string, Result>> _results = {};
static inline std::vector<Result> _results = {};
};

}
46 changes: 46 additions & 0 deletions .benchmark/benchmark_aux.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
//
// Copyright 2022 Clemens Cords
// Created on 08.03.22 by clem ([email protected])
//

#pragma once

#include <limits>
#include <random>

namespace jluna
{
constexpr static inline size_t seed = 12345;

template<typename T, std::enable_if_t<std::is_floating_point_v<T>, bool> = true>
T generate_number(T t_min = std::numeric_limits<T>::min(), T t_max = std::numeric_limits<T>::max())
{
static std::uniform_real_distribution<T> dist = std::uniform_real_distribution<T>(t_min, t_max);
static std::mt19937 engine = std::mt19937(seed);

return dist(engine);
}

template<typename T, std::enable_if_t<std::is_integral_v<T>, bool> = true>
T generate_number(T t_min = std::numeric_limits<T>::min(), T t_max = std::numeric_limits<T>::max())
{
static std::uniform_int_distribution<T> dist = std::uniform_int_distribution<T>(t_min, t_max);
static std::mt19937 engine = std::mt19937(seed + 1000);

return dist(engine);
}

std::string generate_string(size_t length)
{
static std::uniform_int_distribution<char> dist = std::uniform_int_distribution<char>(65, 90);
static std::mt19937 engine = std::mt19937(seed);

std::string out;
out.reserve(length);

for (size_t i = 0; i < length; ++i)
out.push_back(dist(engine));

return out;
}
}
Loading

0 comments on commit b0bbb02

Please sign in to comment.