2024-07-21 16:41:09 +02:00
|
|
|
use clap::Parser;
|
2023-12-09 17:45:39 +01:00
|
|
|
use criterion::{black_box, criterion_group, criterion_main, Criterion};
|
|
|
|
use lazy_static::lazy_static;
|
2024-08-07 23:21:26 +02:00
|
|
|
use mimalloc::MiMalloc;
|
2024-01-16 05:31:20 +01:00
|
|
|
use std::{env, rc::Rc, sync::Arc, time::Duration};
|
2024-01-16 12:14:07 +01:00
|
|
|
use tvix_build::buildservice::DummyBuildService;
|
2024-01-20 00:49:01 +01:00
|
|
|
use tvix_eval::{builtins::impure_builtins, EvalIO};
|
2023-12-09 17:45:39 +01:00
|
|
|
use tvix_glue::{
|
2024-01-17 07:45:55 +01:00
|
|
|
builtins::{add_derivation_builtins, add_fetcher_builtins, add_import_builtins},
|
2024-02-19 16:17:13 +01:00
|
|
|
configure_nix_path,
|
|
|
|
tvix_io::TvixIO,
|
2023-12-09 17:45:39 +01:00
|
|
|
tvix_store_io::TvixStoreIO,
|
|
|
|
};
|
2024-07-21 16:41:09 +02:00
|
|
|
use tvix_store::utils::{construct_services, ServiceUrlsMemory};
|
2023-12-09 17:45:39 +01:00
|
|
|
|
2024-02-13 19:23:39 +01:00
|
|
|
#[global_allocator]
|
2024-08-07 23:21:26 +02:00
|
|
|
static GLOBAL: MiMalloc = MiMalloc;
|
2024-02-13 19:23:39 +01:00
|
|
|
|
2023-12-09 17:45:39 +01:00
|
|
|
lazy_static! {
|
|
|
|
static ref TOKIO_RUNTIME: tokio::runtime::Runtime = tokio::runtime::Runtime::new().unwrap();
|
|
|
|
}
|
|
|
|
|
|
|
|
fn interpret(code: &str) {
|
|
|
|
// TODO: this is a bit annoying.
|
|
|
|
// It'd be nice if we could set this up once and then run evaluate() with a
|
|
|
|
// piece of code. b/262
|
2024-05-10 07:59:25 +02:00
|
|
|
let (blob_service, directory_service, path_info_service, nar_calculation_service) =
|
|
|
|
TOKIO_RUNTIME
|
2024-07-21 16:41:09 +02:00
|
|
|
.block_on(async {
|
|
|
|
construct_services(ServiceUrlsMemory::parse_from(std::iter::empty::<&str>())).await
|
|
|
|
})
|
2024-05-10 07:59:25 +02:00
|
|
|
.unwrap();
|
2023-12-09 17:45:39 +01:00
|
|
|
|
2024-01-16 05:31:20 +01:00
|
|
|
// We assemble a complete store in memory.
|
|
|
|
let tvix_store_io = Rc::new(TvixStoreIO::new(
|
2024-05-10 07:59:25 +02:00
|
|
|
blob_service,
|
|
|
|
directory_service,
|
2024-07-19 15:22:48 +02:00
|
|
|
path_info_service,
|
2024-05-10 07:59:25 +02:00
|
|
|
nar_calculation_service.into(),
|
2024-01-16 12:14:07 +01:00
|
|
|
Arc::<DummyBuildService>::default(),
|
2024-01-16 05:31:20 +01:00
|
|
|
TOKIO_RUNTIME.handle().clone(),
|
|
|
|
));
|
|
|
|
|
2024-07-06 02:29:41 +02:00
|
|
|
let mut eval_builder = tvix_eval::Evaluation::builder(Box::new(TvixIO::new(
|
|
|
|
tvix_store_io.clone() as Rc<dyn EvalIO>,
|
|
|
|
)) as Box<dyn EvalIO>)
|
|
|
|
.enable_import()
|
|
|
|
.add_builtins(impure_builtins());
|
2024-01-16 05:31:20 +01:00
|
|
|
|
2024-07-06 02:29:41 +02:00
|
|
|
eval_builder = add_derivation_builtins(eval_builder, Rc::clone(&tvix_store_io));
|
|
|
|
eval_builder = add_fetcher_builtins(eval_builder, Rc::clone(&tvix_store_io));
|
|
|
|
eval_builder = add_import_builtins(eval_builder, tvix_store_io);
|
|
|
|
eval_builder = configure_nix_path(
|
|
|
|
eval_builder,
|
2023-12-09 17:45:39 +01:00
|
|
|
// The benchmark requires TVIX_BENCH_NIX_PATH to be set, so barf out
|
|
|
|
// early, rather than benchmarking tvix returning an error.
|
|
|
|
&Some(env::var("TVIX_BENCH_NIX_PATH").expect("TVIX_BENCH_NIX_PATH must be set")),
|
|
|
|
);
|
|
|
|
|
2024-07-06 02:29:41 +02:00
|
|
|
let eval = eval_builder.build();
|
2023-12-30 21:36:48 +01:00
|
|
|
let result = eval.evaluate(code, None);
|
2023-12-09 17:45:39 +01:00
|
|
|
|
2024-08-13 18:08:28 +02:00
|
|
|
assert!(result.errors.is_empty(), "{:#?}", result.errors);
|
2023-12-09 17:45:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
fn eval_nixpkgs(c: &mut Criterion) {
|
|
|
|
c.bench_function("hello outpath", |b| {
|
|
|
|
b.iter(|| {
|
|
|
|
interpret(black_box("(import <nixpkgs> {}).hello.outPath"));
|
|
|
|
})
|
|
|
|
});
|
2024-07-30 04:51:32 +02:00
|
|
|
|
|
|
|
c.bench_function("firefox outpath", |b| {
|
|
|
|
b.iter(|| {
|
|
|
|
interpret(black_box("(import <nixpkgs> {}).firefox.outPath"));
|
|
|
|
})
|
|
|
|
});
|
2023-12-09 17:45:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
criterion_group!(
|
|
|
|
name = benches;
|
|
|
|
config = Criterion::default().measurement_time(Duration::from_secs(30)).sample_size(10);
|
|
|
|
targets = eval_nixpkgs
|
|
|
|
);
|
|
|
|
criterion_main!(benches);
|