Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 9 additions & 8 deletions aqpbm-cli/src/benchmark_external_cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@

use std::path::PathBuf;

use anyhow::{bail, Context, Result};
use aqpbm_core::harness::benchmark_external::{report, run_engine, write_report, EngineRunSetup};
use anyhow::{bail, Result};
use aqpbm_core::harness::benchmark_external::{
report, run_engine, write_report, EngineRunSetup, ReportTruth,
};
use aqpbm_core::harness::cgroup::CgroupSource;
use aqpbm_core::harness::engine::EngineUnderTest;
use aqpbm_core::harness::fake::FakeEngine;
Expand Down Expand Up @@ -98,7 +100,9 @@ pub struct RunArgs {
pub struct ReportArgs {
#[arg(long)]
pub run_directory: PathBuf,
/// The cells answers are compared with: `<engine>/<variant>`.
/// What answers are compared with: `declared` (each query's declared
/// statistic, computed from the workload's data) or the cells of
/// `<engine>/<variant>`.
#[arg(long)]
pub truth: String,
}
Expand Down Expand Up @@ -207,12 +211,9 @@ fn server_cgroup(args: &RunArgs) -> Option<CgroupSource> {
}

fn report_cmd(args: ReportArgs) -> Result<()> {
let (engine, variant) = args
.truth
.split_once('/')
.context("--truth must be <engine>/<variant>")?;
let truth = ReportTruth::parse(&args.truth).map_err(anyhow::Error::msg)?;
let run_directory = RunDirectory::open(&args.run_directory)?;
let report = report(&run_directory, engine, variant)?;
let report = report(&run_directory, &truth)?;
write_report(&run_directory, &report)?;
print!("{}", report.summary_markdown());
Ok(())
Expand Down
1 change: 1 addition & 0 deletions aqpbm-cli/tests/benchmark_external_cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ fn workload(name: &str) -> WorkloadSpec {
}],
window_ms: 10_000,
evaluation_interval_ms: 10_000,
truth: None,
}],
data: vec![DataRecord {
metric: "m".into(),
Expand Down
286 changes: 286 additions & 0 deletions aqpbm-core/src/accuracy/declared_truth.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,286 @@
use std::collections::{BTreeMap, BTreeSet, HashMap};

use anyhow::Result;
use aqpbm_datagen::{ColumnData, GeneratedTable};
use serde::{Deserialize, Serialize};

use crate::accuracy::cardinality::CardinalityGT;
use crate::accuracy::keyed::KeyedEntropyGT;
use crate::accuracy::quantile::{RankErrorGT, ReferenceRankErrorGT};
use crate::accuracy::topk::TopkGT;
use crate::accuracy::windowed::{WindowScore, Windowed};
use crate::accuracy::GroundTruth;

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct Truth {
pub statistic: Statistic,
pub metric: String,
#[serde(default)]
pub matchers: BTreeMap<String, String>,
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(deny_unknown_fields, rename_all = "snake_case")]
pub enum Statistic {
Quantile(f64),
Entropy,
Distinct,
TopK { k: usize, by: String },
}

pub struct Series {
pub labels: BTreeMap<String, String>,
pub points: Vec<(i64, f64)>,
}

impl Series {
pub fn between(&self, start_exclusive: i64, end_inclusive: i64) -> &[(i64, f64)] {
let from = self.points.partition_point(|&(t, _)| t <= start_exclusive);
let to = self.points.partition_point(|&(t, _)| t <= end_inclusive);
&self.points[from..to.max(from)]
}

pub fn up_to(&self, end_inclusive: i64) -> &[(i64, f64)] {
&self.points[..self.points.partition_point(|&(t, _)| t <= end_inclusive)]
}
}

pub type Labels = BTreeMap<String, String>;

pub type Scores = BTreeMap<String, f64>;

pub type Cell = (i64, Labels, Option<Scores>);

pub type AnswerAt = (i64, Labels, f64);

const TIME_COLUMN: usize = 0;

pub struct QueryTruth {
statistic: Statistic,
metric: String,
matchers: Vec<(String, String)>,
width_ms: i64,
}

struct Samples {
table: GeneratedTable,
label_names: Vec<String>,
}

impl Samples {
fn value_column(&self) -> usize {
self.label_names.len() + 1
}

fn weight_column(&self) -> usize {
self.label_names.len() + 2
}

fn label_columns(&self) -> Vec<usize> {
(1..=self.label_names.len()).collect()
}

fn labels_of(&self, group: &[String]) -> Labels {
self.label_names
.iter()
.zip(group)
.filter(|(_, value)| !value.is_empty())
.map(|(name, value)| (name.clone(), value.clone()))
.collect()
}
}

pub fn without_name(labels: &Labels) -> Labels {
labels
.iter()
.filter(|(name, _)| name.as_str() != "__name__")
.map(|(name, value)| (name.clone(), value.clone()))
.collect()
}

pub fn keyed(answers: &[AnswerAt]) -> HashMap<(i64, Labels), f64> {
answers
.iter()
.map(|(time, labels, value)| ((*time, without_name(labels)), *value))
.collect()
}

impl QueryTruth {
pub fn new(truth: &Truth, width_ms: i64) -> QueryTruth {
QueryTruth {
statistic: truth.statistic.clone(),
metric: truth.metric.clone(),
matchers: truth
.matchers
.iter()
.map(|(name, value)| (name.clone(), value.clone()))
.collect(),
width_ms,
}
}

fn selects(&self, labels: &Labels) -> bool {
labels.get("__name__") == Some(&self.metric)
&& self
.matchers
.iter()
.all(|(name, value)| labels.get(name).map_or("", String::as_str) == value)
}

fn samples(&self, series: &[Series]) -> Samples {
let chosen: Vec<&Series> = series.iter().filter(|s| self.selects(&s.labels)).collect();
let label_names: Vec<String> = chosen
.iter()
.flat_map(|s| s.labels.keys())
.filter(|name| name.as_str() != "__name__")
.cloned()
.collect::<BTreeSet<_>>()
.into_iter()
.collect();
let rows: usize = chosen.iter().map(|s| s.points.len()).sum();
let mut time = Vec::with_capacity(rows);
let mut value = Vec::with_capacity(rows);
let mut labels: Vec<Vec<String>> = vec![Vec::with_capacity(rows); label_names.len()];
for s in chosen {
for &(t, v) in &s.points {
time.push(t);
value.push(v);
for (column, name) in labels.iter_mut().zip(&label_names) {
column.push(s.labels.get(name).cloned().unwrap_or_default());
}
}
}
let mut column_title = vec!["time".to_string()];
column_title.extend(label_names.iter().cloned());
column_title.extend(["value".to_string(), "weight".to_string()]);
let mut data = vec![ColumnData::Int64(time)];
data.extend(labels.into_iter().map(ColumnData::String));
data.push(ColumnData::Float64(value));
data.push(ColumnData::Int64(vec![1; rows]));
Samples {
table: GeneratedTable::new(column_title, data)
.expect("every column holds one entry per sample"),
label_names,
}
}

fn windowed<G>(&self, ground_truth: G, group_columns: Vec<usize>, ends: &[i64]) -> Windowed<G> {
Windowed {
ground_truth,
time_column: TIME_COLUMN,
group_columns,
width: self.width_ms,
ends: ends.to_vec(),
}
}

fn per_series<G, F>(
&self,
samples: &Samples,
ground_truth: G,
ends: &[i64],
mut answer: F,
) -> Result<Vec<Cell>>
where
G: GroundTruth,
F: FnMut(i64, &Labels) -> Option<G::Answer>,
G::Answer: Clone,
{
let cells = self
.windowed(ground_truth, samples.label_columns(), ends)
.score(&samples.table, |end, group, probes| {
answer(end, &samples.labels_of(group)).map(|a| vec![a; probes.len()])
})?;
Ok(cells_of(samples, cells))
}

pub fn score(
&self,
series: &[Series],
ends: &[i64],
answers: &[AnswerAt],
) -> Result<Vec<Cell>> {
let samples = self.samples(series);
let value = samples.value_column();
let by_key = keyed(answers);
let lookup = |end: i64, labels: &Labels| by_key.get(&(end, labels.clone())).copied();
match &self.statistic {
Statistic::Quantile(q) => self.per_series(
&samples,
RankErrorGT {
column: value,
fractions: vec![*q],
},
ends,
lookup,
),
Statistic::Entropy => self.per_series(
&samples,
KeyedEntropyGT::<f64>::over_columns(value, samples.weight_column()),
ends,
lookup,
),
Statistic::Distinct => {
self.per_series(&samples, CardinalityGT { column: value }, ends, lookup)
}
Statistic::TopK { k, by } => {
let Some(column) = samples.label_names.iter().position(|name| name == by) else {
return Ok(Vec::new());
};
let mut lists: BTreeMap<i64, Vec<(String, u64)>> = BTreeMap::new();
for (time, labels, count) in answers {
if let Some(key) = labels.get(by) {
lists
.entry(*time)
.or_default()
.push((key.clone(), count.round() as u64));
}
}
let cells = self
.windowed(
TopkGT::<String>::over_column(*k, column + 1),
Vec::new(),
ends,
)
.score(&samples.table, |end, _, probes| {
let list = lists.get(&end).cloned().unwrap_or_default();
Some(vec![list; probes.len()])
})?;
Ok(cells_of(&samples, cells))
}
}
}

pub fn reference_rank_errors(
&self,
series: &[Series],
ends: &[i64],
answers: &[AnswerAt],
reference: &HashMap<(i64, Labels), f64>,
) -> Result<Vec<Cell>> {
if !matches!(self.statistic, Statistic::Quantile(_)) {
return Ok(Vec::new());
}
let samples = self.samples(series);
let by_key = keyed(answers);
self.per_series(
&samples,
ReferenceRankErrorGT {
column: samples.value_column(),
},
ends,
|end, labels| {
let key = (end, labels.clone());
Some((*reference.get(&key)?, *by_key.get(&key)?))
},
)
}
}

fn cells_of(samples: &Samples, cells: Vec<WindowScore>) -> Vec<Cell> {
cells
.into_iter()
.map(|cell| (cell.end, samples.labels_of(&cell.group), cell.scores))
.collect()
}
1 change: 1 addition & 0 deletions aqpbm-core/src/accuracy/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use crate::error::RunError;
pub mod aggregate;
pub mod cardinality;
pub(crate) mod curve;
pub mod declared_truth;
pub mod frequency;
pub mod heavy_hitter;
pub mod keyed;
Expand Down
Loading
Loading