120 lines
4.1 KiB
Rust
120 lines
4.1 KiB
Rust
use std::{
|
|
fs::{self, File},
|
|
io::Write,
|
|
};
|
|
|
|
use criterion::{Criterion, Throughput, criterion_group, criterion_main};
|
|
use john_slop::{AnalysisContext, Config, Detector, read_inputs};
|
|
use tempfile::TempDir;
|
|
use zip::write::FileOptions;
|
|
|
|
fn benchmark_large_document(criterion: &mut Criterion) {
|
|
let config = Config::embedded();
|
|
let detector = Detector::new(&config).expect("embedded detector");
|
|
let source = "Reviewed prose without configured signals.\n".repeat(25_000);
|
|
let context = AnalysisContext::for_path("large.txt");
|
|
let mut group = criterion.benchmark_group("analysis");
|
|
group.throughput(Throughput::Bytes(source.len() as u64));
|
|
group.bench_function("one-megabyte-document", |bencher| {
|
|
bencher.iter(|| detector.analyze_with(&source, &context));
|
|
});
|
|
group.finish();
|
|
}
|
|
|
|
fn benchmark_many_small_files(criterion: &mut Criterion) {
|
|
let directory = small_file_tree(250);
|
|
let config = Config::embedded();
|
|
let mut group = criterion.benchmark_group("input");
|
|
group.throughput(Throughput::Elements(250));
|
|
group.bench_function("250-small-files", |bencher| {
|
|
bencher.iter(|| {
|
|
read_inputs(&config, directory.path())
|
|
.expect("open fixture directory")
|
|
.collect::<Result<Vec<_>, _>>()
|
|
.expect("read fixture directory")
|
|
});
|
|
});
|
|
group.finish();
|
|
}
|
|
|
|
fn benchmark_rule_scaling(criterion: &mut Criterion) {
|
|
let mut config = Config::embedded();
|
|
let original = config.rules.clone();
|
|
for generation in 1..10 {
|
|
config
|
|
.rules
|
|
.extend(original.iter().cloned().map(|mut rule| {
|
|
rule.id = format!("{}-{generation}", rule.id);
|
|
rule
|
|
}));
|
|
}
|
|
config.validate().expect("scaled config remains valid");
|
|
let source = "Assistant: Certainly! Here's a result—review it.\n".repeat(100);
|
|
let context = AnalysisContext::for_path("scaled.txt");
|
|
|
|
criterion.bench_function("rules/build-and-analyze-110-rules", |bencher| {
|
|
bencher.iter(|| {
|
|
Detector::new(&config)
|
|
.expect("build scaled detector")
|
|
.analyze_with(&source, &context)
|
|
});
|
|
});
|
|
}
|
|
|
|
fn benchmark_archive_limits(criterion: &mut Criterion) {
|
|
let directory = archive_with_members(100);
|
|
let path = directory.path().join("many.zip");
|
|
let config = Config::embedded();
|
|
let mut group = criterion.benchmark_group("input");
|
|
group.throughput(Throughput::Elements(100));
|
|
group.bench_function("100-member-archive", |bencher| {
|
|
bencher.iter(|| {
|
|
read_inputs(&config, &path)
|
|
.expect("open fixture archive")
|
|
.collect::<Result<Vec<_>, _>>()
|
|
.expect("read fixture archive")
|
|
});
|
|
});
|
|
group.finish();
|
|
}
|
|
|
|
fn small_file_tree(count: usize) -> TempDir {
|
|
let directory = tempfile::tempdir().expect("create benchmark directory");
|
|
for index in 0..count {
|
|
fs::write(
|
|
directory.path().join(format!("document-{index:04}.txt")),
|
|
"Reviewed prose without configured signals.\n",
|
|
)
|
|
.expect("write benchmark document");
|
|
}
|
|
directory
|
|
}
|
|
|
|
fn archive_with_members(count: usize) -> TempDir {
|
|
let directory = tempfile::tempdir().expect("create benchmark directory");
|
|
let file = File::create(directory.path().join("many.zip")).expect("create benchmark archive");
|
|
let mut archive = zip::ZipWriter::new(file);
|
|
let options: FileOptions<()> = FileOptions::default()
|
|
.compression_method(zip::CompressionMethod::Stored)
|
|
.unix_permissions(0o644);
|
|
for index in 0..count {
|
|
archive
|
|
.start_file(format!("document-{index:04}.txt"), options)
|
|
.expect("start benchmark member");
|
|
archive
|
|
.write_all(b"Reviewed prose without configured signals.\n")
|
|
.expect("write benchmark member");
|
|
}
|
|
archive.finish().expect("finish benchmark archive");
|
|
directory
|
|
}
|
|
|
|
criterion_group!(
|
|
benches,
|
|
benchmark_large_document,
|
|
benchmark_many_small_files,
|
|
benchmark_rule_scaling,
|
|
benchmark_archive_limits
|
|
);
|
|
criterion_main!(benches);
|