1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308
|
// Copyright 2018-2022 Emil Dotchevski and Reverge Studios, Inc.
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
// See benchmark.md
#ifndef BENCHMARK_WHAT
# define BENCHMARK_WHAT 0
#endif
#if BENCHMARK_WHAT == 0
# ifndef TL_EXPECTED_HPP
# include "tl/expected.hpp"
# endif
# define BENCHMARK_SUCCESS(e) e
# define BENCHMARK_FAILURE(e) tl::make_unexpected(e)
# define BENCHMARK_TRY(v,r)\
auto && _r_##v = r;\
if( !_r_##v )\
return BENCHMARK_FAILURE(_r_##v.error());\
auto && v = _r_##v.value()
#else
# include <boost/outcome/std_outcome.hpp>
# include <boost/outcome/try.hpp>
# define BENCHMARK_SUCCESS(e) boost::outcome_v2::success(e)
# define BENCHMARK_FAILURE(e) boost::outcome_v2::failure(e)
# define BENCHMARK_TRY BOOST_OUTCOME_TRY
# ifndef BOOST_NO_EXCEPTIONS
# error Please disable exception handling.
# endif
#endif
#ifdef _MSC_VER
# define NOINLINE __declspec(noinline)
# define ALWAYS_INLINE __forceinline
#else
# define NOINLINE __attribute__((noinline))
# define ALWAYS_INLINE __attribute__((always_inline)) inline
#endif
#include <cstring>
#include <cstdlib>
#include <cassert>
#include <chrono>
#include <iostream>
#include <fstream>
#include <iomanip>
#include <numeric>
#include <algorithm>
#include <system_error>
#include <array>
namespace boost
{
void throw_exception( std::exception const & e )
{
std::cerr << "Terminating due to a C++ exception under BOOST_NO_EXCEPTIONS: " << e.what();
std::terminate();
}
struct source_location;
void throw_exception( std::exception const & e, boost::source_location const & )
{
throw_exception(e);
}
}
//////////////////////////////////////
#if BENCHMARK_WHAT == 0 // tl::expected
# define USING_RESULT_TYPE "tl::expected<T, E>"
template <class T, class E>
using result = tl::expected<T, E>;
#elif BENCHMARK_WHAT == 1 // outcome::result
# define USING_RESULT_TYPE "outcome::result<T, E>"
template <class T, class E>
using result = boost::outcome_v2::std_result<T, E, boost::outcome_v2::policy::terminate>;
#elif BENCHMARK_WHAT == 2 // outcome::outcome
# define USING_RESULT_TYPE "outcome::outcome<T, E>"
template <class T, class E>
using result = boost::outcome_v2::std_outcome<T, E>;
#else
# error Benchmark what?
#endif
//////////////////////////////////////
enum class e_error_code
{
ec0, ec1, ec2, ec3
};
struct e_system_error
{
int value;
std::string what;
};
struct e_heavy_payload
{
std::array<char, 4096> value;
};
template <class E>
E make_error() noexcept;
template <>
inline e_error_code make_error<e_error_code>() noexcept
{
switch(std::rand()%4)
{
default: return e_error_code::ec0;
case 1: return e_error_code::ec1;
case 2: return e_error_code::ec2;
case 3: return e_error_code::ec3;
}
}
template <>
inline std::error_code make_error<std::error_code>() noexcept
{
return std::error_code(std::rand(), std::system_category());
}
template <>
inline e_system_error make_error<e_system_error>() noexcept
{
return { std::rand(), std::string(std::rand()%32, ' ') };
}
template <>
inline e_heavy_payload make_error<e_heavy_payload>() noexcept
{
e_heavy_payload e;
std::fill(e.value.begin(), e.value.end(), std::rand());
return e;
}
inline bool should_fail( int failure_rate ) noexcept
{
assert(failure_rate>=0);
assert(failure_rate<=100);
return (std::rand()%100) < failure_rate;
}
inline int handle_error( e_error_code e ) noexcept
{
return int(e);
}
inline int handle_error( std::error_code const & e ) noexcept
{
return e.value();
}
inline int handle_error( e_system_error const & e ) noexcept
{
return e.value + e.what.size();
}
inline int handle_error( e_heavy_payload const & e ) noexcept
{
return std::accumulate(e.value.begin(), e.value.end(), 0);
}
//////////////////////////////////////
// This is used to change the "success" type at each level.
// Generally, functions return values of different types.
template <int N, class E, bool Odd = N%2>
struct select_result_type;
template <int N, class E>
struct select_result_type<N, E, true>
{
using type = result<int, E>;
};
template <int N, class E>
struct select_result_type<N, E, false>
{
using type = result<float, E>;
};
template <int N, class E>
using select_result_t = typename select_result_type<N, E>::type;
//////////////////////////////////////
template <int N, class E>
struct benchmark
{
using e_type = E;
NOINLINE static select_result_t<N, E> f( int failure_rate ) noexcept
{
BENCHMARK_TRY(x, (benchmark<N-1, E>::f(failure_rate)));
return BENCHMARK_SUCCESS(x+1);
}
};
template <class E>
struct benchmark<1, E>
{
using e_type = E;
NOINLINE static select_result_t<1, E> f( int failure_rate ) noexcept
{
if( should_fail(failure_rate) )
return BENCHMARK_FAILURE(make_error<E>());
else
return BENCHMARK_SUCCESS(std::rand());
}
};
//////////////////////////////////////
template <class Benchmark>
NOINLINE int runner( int failure_rate ) noexcept
{
if( auto r = Benchmark::f(failure_rate) )
return r.value();
else
return handle_error(r.error());
}
//////////////////////////////////////
std::fstream append_csv()
{
if( FILE * f = fopen("benchmark.csv","rb") )
{
fclose(f);
return std::fstream("benchmark.csv", std::fstream::out | std::fstream::app);
}
else
{
std::fstream fs("benchmark.csv", std::fstream::out | std::fstream::app);
fs << "\"Result Type\",2%,98%\n";
return fs;
}
}
template <class F>
int print_elapsed_time( int iteration_count, F && f )
{
auto start = std::chrono::steady_clock::now();
int val = 0;
for( int i = 0; i!=iteration_count; ++i )
val += std::forward<F>(f)();
auto stop = std::chrono::steady_clock::now();
int elapsed = std::chrono::duration_cast<std::chrono::microseconds>(stop-start).count();
std::cout << std::right << std::setw(9) << elapsed;
append_csv() << ',' << elapsed;
return val;
}
//////////////////////////////////////
template <int Depth, class E>
int benchmark_type( char const * type_name, int iteration_count )
{
int x=0;
append_csv() << "\"" USING_RESULT_TYPE "\"";
std::cout << '\n' << std::left << std::setw(16) << type_name << '|';
std::srand(0);
x += print_elapsed_time( iteration_count, [] { return runner<benchmark<Depth, E>>(2); } );
std::cout << " |";
std::srand(0);
x += print_elapsed_time( iteration_count, [] { return runner<benchmark<Depth, E>>(98); } );
append_csv() << '\n';
return x;
}
//////////////////////////////////////
int main()
{
int const depth = 10;
int const iteration_count = 10000000;
std::cout <<
iteration_count << " iterations, call depth " << depth << ", sizeof(e_heavy_payload) = " << sizeof(e_heavy_payload) << "\n"
USING_RESULT_TYPE "\n"
"Error type | 2% (μs) | 98% (μs)\n"
"----------------|----------|---------";
int r = 0;
r += benchmark_type<depth, e_error_code>("e_error_code", iteration_count);
r += benchmark_type<depth, std::error_code>("std::error_code", iteration_count);
r += benchmark_type<depth, e_system_error>("e_system_error", iteration_count);
r += benchmark_type<depth, e_heavy_payload>("e_heavy_payload", iteration_count);
std::cout << '\n';
// std::cout << std::rand() << '\n';
return r;
}
|