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
3 changes: 3 additions & 0 deletions aqpbm-core/src/harness/engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,9 @@ pub struct QueryText {
pub enum QueryLanguage {
PromQl,
Sql,
/// MetricsQL: read natively by VictoriaMetrics and planned by
/// ASAPPlanner's MetricsQL frontend.
MetricsQl,
}

/// One installed arm. The harness calls `ingest` per refresh chunk and
Expand Down
235 changes: 235 additions & 0 deletions aqpbm-core/tests/harness_suites.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,235 @@
//! Every committed benchmark-external suite: each workload parses as a harness
//! `WorkloadSpec`, has a schedule, and lists the same queries, in the same
//! order, as the ASAPPlanner planning workload beside it (the planner
//! numbers them q1..qN in that order, and engine runs map plan roots to queries
//! by that index).

use std::path::Path;

use aqpbm_core::harness::arm_run::ArmSchedule;
use aqpbm_core::harness::engine::{QueryLanguage, WorkloadSpec};

#[test]
fn suite_workloads_match_their_planning_workloads() {
let suites = Path::new(env!("CARGO_MANIFEST_DIR")).join("../suites");
let mut checked = 0;
for suite in std::fs::read_dir(&suites).unwrap() {
let suite = suite.unwrap().path();
if !suite.is_dir() {
continue;
}
let mut names: Vec<_> = std::fs::read_dir(&suite)
.unwrap()
.map(|entry| entry.unwrap().file_name().to_string_lossy().into_owned())
.filter_map(|file| file.strip_suffix(".workload.json").map(str::to_string))
.collect();
names.sort();
for name in names {
let read = |suffix: &str| {
std::fs::read_to_string(suite.join(format!("{name}.{suffix}.json")))
.unwrap_or_else(|error| panic!("{name}.{suffix}.json: {error}"))
};
let spec: WorkloadSpec = serde_json::from_str(&read("workload")).unwrap();
assert_eq!(spec.name, name);
let planner: serde_json::Value = serde_json::from_str(&read("planner")).unwrap();
let planned: Vec<&str> = planner["query_workload"]["repeating_queries"]
.as_array()
.unwrap()
.iter()
.map(|query| query["query"].as_str().unwrap())
.collect();
let specified: Vec<&str> = spec
.queries
.iter()
.map(|query| {
query
.texts
.iter()
.find(|text| text.language == QueryLanguage::PromQl)
.unwrap()
.text
.as_str()
})
.collect();
assert_eq!(specified, planned, "{name}");
let ids: Vec<String> = (1..=spec.queries.len()).map(|i| format!("q{i}")).collect();
let spec_ids: Vec<String> = spec.queries.iter().map(|q| q.id.clone()).collect();
assert_eq!(spec_ids, ids, "{name}");
let schedule =
ArmSchedule::for_workload(&spec).unwrap_or_else(|error| panic!("{name}: {error}"));
for ((query, planned), text) in spec
.queries
.iter()
.zip(
planner["query_workload"]["repeating_queries"]
.as_array()
.unwrap(),
)
.zip(&specified)
{
let at = &planned["demand"]["fixed_interval_at"];
let interval = at["interval"].as_u64().unwrap();
assert_eq!(
query.evaluation_interval_ms, interval,
"{name} {}",
query.id
);
// Evaluations land on the planner's phase.
let phase = at["evaluation_phase"].as_i64().unwrap();
assert_eq!(
schedule.evaluate_at_ms[0].rem_euclid(interval as i64),
phase.rem_euclid(interval as i64),
"{name} {}",
query.id
);
// Every range in the text is the query's window.
let ranges = range_ms(text);
assert!(!ranges.is_empty(), "{name}: no range in {text}");
assert!(
ranges.iter().all(|&ms| ms == query.window_ms),
"{name}: {text} vs {}",
query.window_ms
);
// Grouping labels are labels the data has.
for label in grouping_labels(text) {
assert!(
spec.data.iter().all(|record| record
.description
.labels
.iter()
.any(|spec| spec.name == label)),
"{name}: {text} groups by {label}, which the data lacks"
);
}
// The metrics it reads have data.
for record_metric in metrics_read(text, &spec) {
assert!(
spec.data.iter().any(|r| r.metric == record_metric),
"{name}: {record_metric}"
);
}
}
// The data is what the planner was told: per source, the
// series count, sample interval, counter kind, distinct label
// values and value sign; and no source without data.
let sources = planner["data_workload"]["sources"].as_object().unwrap();
let rows_per_second: f64 = spec
.data
.iter()
.map(|r| {
r.description.series_num as f64 * 1_000.0
/ r.description.sample_interval_ms as f64
})
.sum();
assert_eq!(
planner["data_workload"]["arrival"]["ingesting"]["rows_per_second"].as_f64(),
Some(rows_per_second),
"{name}: rows_per_second"
);
assert_eq!(
sources.len(),
spec.data.len(),
"{name}: sources vs data records"
);
for record in &spec.data {
let facts = &sources[&record.metric];
let description = &record.description;
assert_eq!(
description.series_num,
facts["series_count"].as_u64().unwrap(),
"{name}"
);
assert_eq!(
description.sample_interval_ms,
facts["sample_interval"].as_u64().unwrap(),
"{name}"
);
assert_eq!(
description.counter,
facts["metric_type"] == "counter",
"{name} {}",
record.metric
);
let product: u64 = description
.labels
.iter()
.map(|label| label.cardinality)
.product();
assert_eq!(
product, description.series_num,
"{name}: label sets vs series"
);
for label in &description.labels {
assert_eq!(
facts["distinct_values_per_label"][&label.name].as_u64(),
Some(label.cardinality),
"{name} {} {}",
record.metric,
label.name
);
}
if facts["column_signs"]["value"] == "non_negative" {
let lower = serde_json::to_value(&description.value_distribution).unwrap()
["lower_bound"]
.as_f64()
.unwrap();
assert!(lower >= 0.0, "{name} {}: negative values", record.metric);
}
}
checked += 1;
}
}
assert!(checked >= 8, "only {checked} suite workloads");
}

/// Every `[<n>m]` / `[<n>s]` range of a PromQL text, in ms.
fn range_ms(text: &str) -> Vec<u64> {
text.split('[')
.skip(1)
.filter_map(|rest| rest.split_once(']'))
.map(|(range, _)| {
let (number, unit) = range.split_at(range.len() - 1);
let number: u64 = number.parse().unwrap_or_else(|_| panic!("range {range}"));
match unit {
"s" => number * 1_000,
"m" => number * 60_000,
"h" => number * 3_600_000,
_ => panic!("range {range}"),
}
})
.collect()
}

/// The labels of every `by (...)` / `without (...)` clause.
fn grouping_labels(text: &str) -> Vec<String> {
let mut labels = Vec::new();
for keyword in ["by (", "without ("] {
for rest in text.split(keyword).skip(1) {
let (list, _) = rest.split_once(')').unwrap();
labels.extend(list.split(',').map(|label| label.trim().to_string()));
}
}
labels
}

/// The data metrics whose names appear in the text.
fn metrics_read<'a>(text: &str, spec: &'a WorkloadSpec) -> Vec<&'a str> {
let read: Vec<&str> = spec
.data
.iter()
.map(|record| record.metric.as_str())
.filter(|metric| {
text.contains(&format!("{metric}[")) || text.contains(&format!("{metric}{{"))
})
.collect();
assert!(!read.is_empty(), "{text} reads no metric with data");
read
}

/// `"MetricsQl"` is a query language of a workload spec.
#[test]
fn metricsql_is_a_workload_language() {
let language: aqpbm_core::harness::engine::QueryLanguage =
serde_json::from_str("\"MetricsQl\"").unwrap();
assert_eq!(language, QueryLanguage::MetricsQl);
}
80 changes: 80 additions & 0 deletions scripts/plan-suite.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
#!/usr/bin/env bash
# Prepare a benchmark-external run directory from a suite: copy each workload and
# plan it once per variant with ASAPPlanner's `asap_planner --plan-file`.
#
# scripts/plan-suite.sh <suite-dir> <run-directory>
#
# Environment: ASAP_PLANNER, the `asap_planner` binary (needs --plan-file
# and --executor-check, and deployment exclusions in capability files).
#
# A suite directory holds, per workload <w>: <w>.workload.json (the harness
# WorkloadSpec: queries q1..qN in planning order, and the data) and
# <w>.planner.json (the ASAPPlanner PlanningWorkload of the same queries).
#
# Variants (plans/<variant>/):
# clickhouse.equivalent-only capabilities/clickhouse.equivalent-only.json, executor check skipped
# clickhouse.with-substitutes capabilities/clickhouse.with-substitutes.json, executor check skipped
# asap.default the planner's defaults, compiled for the Planner's executor
# asap.exact every query's accuracy target exact (max_error 0, failure_probability 0), compiled
# A workload the planner selects no plan for gets <w>.no-plan.json with the
# planner's error line (or its last line) as the reason.
#
# Example: ASAP_PLANNER=target/release/asap_planner scripts/plan-suite.sh suites/showcase-1m /data/run-1
set -euo pipefail

suite=${1:?suite dir}; run=${2:?run dir}
: "${ASAP_PLANNER:?set ASAP_PLANNER to the asap_planner binary}"
repo=$(cd "$(dirname "$0")/.." && pwd)
caps="$repo/system-bench/capabilities"

[ -e "$run" ] && { echo "$run exists; runs are never overwritten" >&2; exit 1; }
compgen -G "$suite/*.workload.json" >/dev/null || { echo "$suite has no *.workload.json" >&2; exit 1; }
# planner-logs/ holds each planner run's log and the rewritten exact inputs.
mkdir -p "$run"/{workloads,data,plans,results,planner-logs}

variants=(
"clickhouse.equivalent-only|$caps/clickhouse.equivalent-only.json|skip|false|keep"
"clickhouse.with-substitutes|$caps/clickhouse.with-substitutes.json|skip|true|keep"
"asap.default||compile|true|keep"
"asap.exact||compile|false|exact"
)

for variant_line in "${variants[@]}"; do
IFS='|' read -r variant _ _ substitutions _ <<<"$variant_line"
mkdir -p "$run/plans/$variant"
printf '{"substitutions_allowed": %s}\n' "$substitutions" > "$run/plans/$variant/variant.json"
done

for spec in "$suite"/*.workload.json; do
w=$(basename "$spec" .workload.json)
cp "$spec" "$run/workloads/$w.json"
for variant_line in "${variants[@]}"; do
IFS='|' read -r variant cap check substitutions accuracy <<<"$variant_line"
planner_input="$suite/$w.planner.json"
if [ "$accuracy" = exact ]; then
planner_input="$run/planner-logs/$w.exact.planner.json"
python3 - "$suite/$w.planner.json" "$planner_input" <<'PY'
import json, sys
workload = json.load(open(sys.argv[1]))
for query in workload["query_workload"]["repeating_queries"]:
query.setdefault("requirements", {})["accuracy"] = {"max_error": 0.0, "failure_probability": 0.0}
json.dump(workload, open(sys.argv[2], "w"), indent=1)
PY
fi
args=(--workload "$planner_input" --run-until stage3 --executor-check "$check"
--plan-file "$run/plans/$variant/$w.plan.json")
[ -n "$cap" ] && args+=(--deployment-capabilities "$cap")
log="$run/planner-logs/$w.$variant.log"
if "$ASAP_PLANNER" "${args[@]}" > "$log" 2>&1 && [ -s "$run/plans/$variant/$w.plan.json" ]; then
echo "$w $variant: plan"
else
# The planner's error line when there is one, else its last line.
reason=$(grep -iE '^error[: ]' "$log" | tail -n 1 || true)
[ -n "$reason" ] || reason=$(grep -v '^[[:space:]]*$' "$log" | tail -n 1 || true)
python3 -c 'import json,sys; print(json.dumps({"reason": sys.argv[1]}))' "${reason:-no plan file written}" \
> "$run/plans/$variant/$w.no-plan.json"
rm -f "$run/plans/$variant/$w.plan.json"
echo "$w $variant: no plan (${reason:-no plan file written})"
fi
done
done
50 changes: 50 additions & 0 deletions suites/showcase-1h/avg_quantile.planner.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
{
"query_workload": {
"language": "promql",
"repeating_queries": [
{
"query": "avg by (job) (quantile_over_time(0.9, latency[1h]))",
"demand": {
"fixed_interval_at": {
"interval": 60000,
"evaluation_phase": 0
}
},
"requirements": {
"accuracy": {
"max_error": 0.01,
"failure_probability": 0.01,
"accuracy_scope": "per_value",
"accuracy_metric": "relative"
}
},
"predictability": {
"predictable": {
"known_at": null
}
}
}
]
},
"data_workload": {
"arrival": {
"ingesting": {
"rows_per_second": 1000.0
}
},
"sources": {
"latency": {
"series_count": 1000,
"metric_type": "gauge",
"column_signs": {
"value": "non_negative"
},
"sample_interval": 1000,
"distinct_values_per_label": {
"job": 10,
"instance": 100
}
}
}
}
}
Loading
Loading