Benchmark report in C++#

This page documents examples/benchmark_report (view on GitHub). The program reads a CSV file with benchmark logs, declares the role of every column, adds a value computed with a C++ formula, then prints a pivot view, an aggregated view and a side by side comparison of two exporters.

Step 1 – Install the C++ library#

From the repository root:

cmake -S . -B build/install-lib -DCMAKE_BUILD_TYPE=Release \
      -DDATA_CUBE_BUILD_PYTHON=OFF -DCMAKE_INSTALL_PREFIX=<prefix>
cmake --build build/install-lib --parallel
cmake --install build/install-lib

Step 2 – Build and run the example#

cmake -S examples/benchmark_report -B build/examples/benchmark_report \
      -DCMAKE_BUILD_TYPE=Release -DCMAKE_PREFIX_PATH=<prefix>
cmake --build build/examples/benchmark_report
./build/examples/benchmark_report/benchmark_report [file.csv]

Both steps are done by bash examples/build.sh.

CMakeLists.txt#

cmake_minimum_required(VERSION 3.26)
project(benchmark_report LANGUAGES CXX)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

find_package(data_cube CONFIG REQUIRED)

add_executable(benchmark_report main.cc)
target_link_libraries(benchmark_report PRIVATE data_cube::lib_data_cube)
target_compile_definitions(benchmark_report
  PRIVATE DATA_CUBE_EXAMPLE_CSV="${CMAKE_CURRENT_SOURCE_DIR}/data.csv")

enable_testing()
add_test(NAME benchmark_report COMMAND benchmark_report)

main.cc#

// Standalone C++ example: builds a small benchmark report with data_cube.
//
// Usage: benchmark_report [file.csv]
// Without argument, the program uses data.csv stored next to the sources.

#include <cmath>
#include <iostream>
#include <stdexcept>
#include <string>
#include <vector>

#include "cube_core/data_cube.h"

#ifndef DATA_CUBE_EXAMPLE_CSV
#define DATA_CUBE_EXAMPLE_CSV "data.csv"
#endif

using namespace data_cube;

int main(int argc, char **argv) {
  try {
    const std::string filename = argc > 1 ? argv[1] : DATA_CUBE_EXAMPLE_CSV;

    // 1. Loads the logs: one row per (date, model_name, exporter, opt).
    Table logs = read_csv(filename);
    std::cout << "-- raw data\n" << logs.to_string() << "\n";

    // 2. Declares the roles of every column and a formula computing a new value.
    DataCube::Formula speedup = [](const Table &data) {
      const Column &latency = data.column("time_latency");
      const Column &baseline = data.column("time_baseline");
      std::vector<double> values(data.num_rows());
      for (size_t i = 0; i < values.size(); ++i)
        values[i] = baseline.as_double(i) / latency.as_double(i);
      return Column::from_doubles(std::move(values));
    };
    DataCube cube({logs}, "date", {"model_name", "exporter", "opt"}, {"time_.*"},
                  /*ignored=*/{}, /*recent=*/false, {{"speedup", speedup}});
    cube.load();
    std::cout << "-- description\n" << cube.describe().to_string() << "\n";

    // 3. A pivot view: models in rows, exporters, optimization levels and dates in columns.
    ViewDef view_def;
    view_def.key_index = {"model_name"};
    view_def.values = {"speedup"};
    std::cout << "-- speedup\n" << cube.view(view_def).to_string() << "\n";

    // 4. An aggregated view: average speedup and maximum latency per exporter,
    //    over all models and optimization levels.
    ViewDef agg_def;
    agg_def.key_index = {"exporter"};
    agg_def.values = {"speedup", "time_latency"};
    agg_def.key_agg = {"model_name", "opt"};
    agg_def.agg = "mean";
    agg_def.agg_by_column = {{"time_latency", "max"}};
    std::cout << "-- aggregated\n" << cube.view(agg_def).to_string() << "\n";

    // 5. Side by side: compares both exporters with the same optimization level.
    SideBySide sbs =
        cube.sbs({{"EXPORT", {{"exporter", std::string("export")}, {"opt", std::string("O2")}}},
                  {"DYNAMO", {{"exporter", std::string("dynamo")}, {"opt", std::string("O2")}}}});
    std::cout << "-- side by side\n" << sbs.agg.to_string() << "\n";
    return 0;
  } catch (const std::exception &e) {
    std::cerr << "error: " << e.what() << std::endl;
    return 1;
  }
}

Data#

date,model_name,exporter,opt,time_latency,time_baseline
2025/01/01,phi3,export,O2,0.10,0.15
2025/01/01,phi3,dynamo,O2,0.12,0.15
2025/01/01,phi4,export,O2,0.20,0.26
2025/01/01,phi4,dynamo,O2,,0.26
2025/01/01,llama,export,O2,0.31,0.35
2025/01/01,llama,dynamo,O2,0.29,0.35
2025/01/02,phi3,export,O2,0.09,0.15
2025/01/02,phi3,dynamo,O2,0.11,0.15
2025/01/02,phi4,export,O2,0.21,0.26
2025/01/02,phi4,dynamo,O2,0.19,0.26
2025/01/02,llama,export,O2,,0.35
2025/01/02,llama,dynamo,O2,0.28,0.35
2025/01/02,llama,dynamo,O0,0.40,0.35

Output#

The two views (the description and the side by side are skipped):

-- speedup
   METRICS              speedup              speedup              speedup              speedup              speedup
  exporter               dynamo               dynamo               dynamo               export               export
       opt                   O0                   O2                   O2                   O2                   O2
      date  2025-01-02 00:00:00  2025-01-01 00:00:00  2025-01-02 00:00:00  2025-01-01 00:00:00  2025-01-02 00:00:00
model_name
     llama   0.8749999999999999    1.206896551724138   1.2499999999999998    1.129032258064516                  NaN
      phi3                  NaN                 1.25   1.3636363636363635   1.4999999999999998   1.6666666666666667
      phi4                  NaN                  NaN    1.368421052631579                  1.3   1.2380952380952381
[3 rows x 5 columns]
-- aggregated
                  date  2025-01-01 00:00:00  2025-01-02 00:00:00
exporter       METRICS
  dynamo       speedup    1.228448275862069   1.2142643540669855
  dynamo  time_latency                 0.29                  0.4
  export       speedup   1.3096774193548386   1.4523809523809526
  export  time_latency                 0.31                 0.21
[4 rows x 2 columns]