|
| 1 | +#include "codspeed.h" |
| 2 | +#include <algorithm> |
| 3 | +#include <cmath> |
| 4 | +#include <cstdlib> |
| 5 | +#include <filesystem> |
| 6 | +#include <fstream> |
| 7 | +#include <iostream> |
| 8 | +#include <numeric> |
| 9 | +#include <sstream> |
| 10 | +#include <string> |
| 11 | +#include <thread> |
| 12 | +#include <vector> |
| 13 | + |
| 14 | +const double IQR_OUTLIER_FACTOR = 1.5; |
| 15 | +const double STDEV_OUTLIER_FACTOR = 3.0; |
| 16 | + |
| 17 | +// Times are per iteration |
| 18 | +struct BenchmarkStats { |
| 19 | + double min_ns; |
| 20 | + double max_ns; |
| 21 | + double mean_ns; |
| 22 | + double stdev_ns; |
| 23 | + double q1_ns; |
| 24 | + double median_ns; |
| 25 | + double q3_ns; |
| 26 | + uint64_t rounds; |
| 27 | + double total_time; |
| 28 | + uint64_t iqr_outlier_rounds; |
| 29 | + uint64_t stdev_outlier_rounds; |
| 30 | + long iter_per_round; |
| 31 | + uint64_t warmup_iters; |
| 32 | +}; |
| 33 | + |
| 34 | +struct BenchmarkMetadata { |
| 35 | + std::string name; |
| 36 | + std::string uri; |
| 37 | +}; |
| 38 | + |
| 39 | +struct CodspeedWalltimeBenchmark { |
| 40 | + BenchmarkMetadata metadata; |
| 41 | + BenchmarkStats stats; |
| 42 | +}; |
| 43 | + |
| 44 | +double compute_quantile(const std::vector<double> &data, double quantile) { |
| 45 | + size_t n = data.size(); |
| 46 | + if (n == 0) |
| 47 | + return 0.0; |
| 48 | + |
| 49 | + double pos = quantile * (n - 1); |
| 50 | + size_t k = static_cast<size_t>(pos); |
| 51 | + double d = pos - k; |
| 52 | + |
| 53 | + if (k + 1 < n) { |
| 54 | + return data[k] + d * (data[k + 1] - data[k]); |
| 55 | + } |
| 56 | + return data[k]; |
| 57 | +} |
| 58 | + |
| 59 | +void compute_iqr_and_outliers(const std::vector<double> ×_ns, double mean, |
| 60 | + double median, double stdev, double &q1, |
| 61 | + double &q3, double &iqr, |
| 62 | + size_t &iqr_outlier_rounds, |
| 63 | + size_t &stdev_outlier_rounds) { |
| 64 | + std::vector<double> sorted_times = times_ns; |
| 65 | + std::sort(sorted_times.begin(), sorted_times.end()); |
| 66 | + |
| 67 | + q1 = compute_quantile(sorted_times, 0.25); |
| 68 | + q3 = compute_quantile(sorted_times, 0.75); |
| 69 | + |
| 70 | + iqr = q3 - q1; |
| 71 | + |
| 72 | + const double IQR_OUTLIER_FACTOR = 1.5; |
| 73 | + const double STDEV_OUTLIER_FACTOR = 3.0; |
| 74 | + |
| 75 | + iqr_outlier_rounds = |
| 76 | + std::count_if(sorted_times.begin(), sorted_times.end(), |
| 77 | + [q1, q3, iqr, IQR_OUTLIER_FACTOR](double x) { |
| 78 | + return x < q1 - IQR_OUTLIER_FACTOR * iqr || |
| 79 | + x > q3 + IQR_OUTLIER_FACTOR * iqr; |
| 80 | + }); |
| 81 | + |
| 82 | + stdev_outlier_rounds = |
| 83 | + std::count_if(sorted_times.begin(), sorted_times.end(), |
| 84 | + [mean, stdev, STDEV_OUTLIER_FACTOR](double x) { |
| 85 | + return x < mean - STDEV_OUTLIER_FACTOR * stdev || |
| 86 | + x > mean + STDEV_OUTLIER_FACTOR * stdev; |
| 87 | + }); |
| 88 | +} |
| 89 | + |
| 90 | +void write_codspeed_benchmarks_to_json( |
| 91 | + const std::vector<CodspeedWalltimeBenchmark> &benchmarks) { |
| 92 | + std::ostringstream oss; |
| 93 | + |
| 94 | + std::string creator_name = "codspeed-cpp"; |
| 95 | + std::string creator_version = CODSPEED_VERSION; |
| 96 | + std::thread::id creator_pid = std::this_thread::get_id(); |
| 97 | + std::string instrument_type = "walltime"; |
| 98 | + |
| 99 | + oss << "{\n"; |
| 100 | + oss << " \"creator\": {\n"; |
| 101 | + oss << " \"name\": \"" << creator_name << "\",\n"; |
| 102 | + oss << " \"version\": \"" << creator_version << "\",\n"; |
| 103 | + oss << " \"pid\": " << creator_pid << "\n"; |
| 104 | + oss << " },\n"; |
| 105 | + oss << " \"instrument\": {\n"; |
| 106 | + oss << " \"type\": \"" << instrument_type << "\"\n"; |
| 107 | + oss << " },\n"; |
| 108 | + oss << " \"benchmarks\": [\n"; |
| 109 | + |
| 110 | + for (size_t i = 0; i < benchmarks.size(); ++i) { |
| 111 | + const auto &benchmark = benchmarks[i]; |
| 112 | + const auto &stats = benchmark.stats; |
| 113 | + const auto &metadata = benchmark.metadata; |
| 114 | + |
| 115 | + oss << " {\n"; |
| 116 | + oss << " \"name\": \"" << metadata.name << "\",\n"; |
| 117 | + oss << " \"uri\": \"" << metadata.uri << "\",\n"; |
| 118 | + // TODO: Manage config fields from actual configuration |
| 119 | + oss << " \"config\": {\n"; |
| 120 | + oss << " \"warmup_time_ns\": null,\n"; |
| 121 | + oss << " \"min_round_time_ns\": null,\n"; |
| 122 | + oss << " \"max_time_ns\": null,\n"; |
| 123 | + oss << " \"max_rounds\": null\n"; |
| 124 | + oss << " },\n"; |
| 125 | + oss << " \"stats\": {\n"; |
| 126 | + oss << " \"min_ns\": " << stats.min_ns << ",\n"; |
| 127 | + oss << " \"max_ns\": " << stats.max_ns << ",\n"; |
| 128 | + oss << " \"mean_ns\": " << stats.mean_ns << ",\n"; |
| 129 | + oss << " \"stdev_ns\": " << stats.stdev_ns << ",\n"; |
| 130 | + oss << " \"q1_ns\": " << stats.q1_ns << ",\n"; |
| 131 | + oss << " \"median_ns\": " << stats.median_ns << ",\n"; |
| 132 | + oss << " \"q3_ns\": " << stats.q3_ns << ",\n"; |
| 133 | + oss << " \"rounds\": " << stats.rounds << ",\n"; |
| 134 | + oss << " \"total_time\": " << stats.total_time << ",\n"; |
| 135 | + oss << " \"iqr_outlier_rounds\": " << stats.iqr_outlier_rounds |
| 136 | + << ",\n"; |
| 137 | + oss << " \"stdev_outlier_rounds\": " << stats.stdev_outlier_rounds |
| 138 | + << ",\n"; |
| 139 | + oss << " \"iter_per_round\": " << stats.iter_per_round << ",\n"; |
| 140 | + oss << " \"warmup_iters\": " << stats.warmup_iters << "\n"; |
| 141 | + oss << " }\n"; |
| 142 | + oss << " }"; |
| 143 | + |
| 144 | + if (i < benchmarks.size() - 1) { |
| 145 | + oss << ","; |
| 146 | + } |
| 147 | + oss << "\n"; |
| 148 | + } |
| 149 | + |
| 150 | + oss << " ]\n"; |
| 151 | + oss << "}"; |
| 152 | + |
| 153 | + // Determine the directory path |
| 154 | + const char *profile_folder = std::getenv("CODSPEED_PROFILE_FOLDER"); |
| 155 | + std::string directory = profile_folder ? profile_folder : "."; |
| 156 | + |
| 157 | + // Create the results directory if it does not exist |
| 158 | + std::filesystem::path results_path = directory + "/results"; |
| 159 | + if (!std::filesystem::exists(results_path)) { |
| 160 | + if (!std::filesystem::create_directories(results_path)) { |
| 161 | + std::cerr << "Failed to create directory: " << results_path << std::endl; |
| 162 | + return; |
| 163 | + } |
| 164 | + } |
| 165 | + |
| 166 | + // Create the file path |
| 167 | + std::ostringstream file_path; |
| 168 | + file_path << results_path.string() << "/" << creator_pid << ".json"; |
| 169 | + |
| 170 | + std::cout << file_path.str() << std::endl; |
| 171 | + |
| 172 | + // Write to file |
| 173 | + std::ofstream out_file(file_path.str()); |
| 174 | + if (out_file.is_open()) { |
| 175 | + out_file << oss.str(); |
| 176 | + out_file.close(); |
| 177 | + std::cout << "JSON written to " << file_path.str() << std::endl; |
| 178 | + } else { |
| 179 | + std::cerr << "Unable to open file " << file_path.str() << std::endl; |
| 180 | + } |
| 181 | +} |
| 182 | + |
| 183 | +void generate_codspeed_walltime_report( |
| 184 | + const std::vector<RawWalltimeBenchmark> &raw_walltime_benchmarks) { |
| 185 | + std::vector<CodspeedWalltimeBenchmark> codspeed_walltime_benchmarks; |
| 186 | + |
| 187 | + for (const auto &raw_benchmark : raw_walltime_benchmarks) { |
| 188 | + CodspeedWalltimeBenchmark codspeed_benchmark; |
| 189 | + codspeed_benchmark.metadata = {raw_benchmark.name, raw_benchmark.uri}; |
| 190 | + |
| 191 | + double total_time = |
| 192 | + std::accumulate(raw_benchmark.round_times_ns.begin(), |
| 193 | + raw_benchmark.round_times_ns.end(), 0.0); |
| 194 | + |
| 195 | + double mean = raw_benchmark.mean_ns; |
| 196 | + double median = raw_benchmark.median_ns; |
| 197 | + double stdev = raw_benchmark.stdev_ns; |
| 198 | + double q1, q3, iqr; |
| 199 | + size_t iqr_outlier_rounds, stdev_outlier_rounds; |
| 200 | + compute_iqr_and_outliers(raw_benchmark.round_times_ns, mean, median, stdev, |
| 201 | + q1, q3, iqr, iqr_outlier_rounds, |
| 202 | + stdev_outlier_rounds); |
| 203 | + |
| 204 | + // Populate stats |
| 205 | + codspeed_benchmark.stats = { |
| 206 | + *std::min_element(raw_benchmark.round_times_ns.begin(), |
| 207 | + raw_benchmark.round_times_ns.end()), |
| 208 | + *std::max_element(raw_benchmark.round_times_ns.begin(), |
| 209 | + raw_benchmark.round_times_ns.end()), |
| 210 | + mean, |
| 211 | + stdev, |
| 212 | + q1, |
| 213 | + median, |
| 214 | + q3, |
| 215 | + raw_benchmark.round_times_ns.size(), |
| 216 | + total_time, |
| 217 | + iqr_outlier_rounds, |
| 218 | + stdev_outlier_rounds, |
| 219 | + raw_benchmark.iter_per_round, |
| 220 | + 0 // TODO: warmup_iters |
| 221 | + }; |
| 222 | + |
| 223 | + codspeed_walltime_benchmarks.push_back(codspeed_benchmark); |
| 224 | + } |
| 225 | + |
| 226 | + write_codspeed_benchmarks_to_json(codspeed_walltime_benchmarks); |
| 227 | +} |
0 commit comments