Skip to content

Commit f6cc073

Browse files
fixup: first draft divan
1 parent ad631fc commit f6cc073

File tree

10 files changed

+162
-82
lines changed

10 files changed

+162
-82
lines changed

Cargo.lock

Lines changed: 66 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@ members = [
44
"crates/bencher_compat",
55
"crates/criterion_compat",
66
"crates/cargo-codspeed",
7+
78
"crates/divan_compat",
89
"crates/divan_compat/macros",
10+
"crates/divan_compat/examples",
911
]
1012
resolver = "2"

crates/divan_compat/Cargo.toml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ repository = "https://github.com/CodSpeedHQ/codspeed-rust"
1111
homepage = "https://codspeed.io"
1212
license = "MIT OR Apache-2.0"
1313
categories = [
14-
"development-tools",
15-
"development-tools::profiling",
16-
"development-tools::testing",
14+
"development-tools",
15+
"development-tools::profiling",
16+
"development-tools::testing",
1717
]
1818
keywords = ["codspeed", "benchmark", "divan"]
1919

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
use codspeed_divan_compat as divan;
2-
31
fn fibo(n: i32) -> i32 {
42
let mut a = 0;
53
let mut b = 1;
@@ -13,17 +11,16 @@ fn fibo(n: i32) -> i32 {
1311
a
1412
}
1513

16-
#[divan::bench]
14+
#[codspeed_divan_compat::bench]
1715
fn fibo_500() -> i32 {
18-
divan::black_box(fibo(500))
16+
codspeed_divan_compat::black_box(fibo(500))
1917
}
2018

21-
#[divan::bench]
22-
fn fibo_100() -> i32 {
23-
divan::black_box(fibo(10))
19+
#[codspeed_divan_compat::bench]
20+
fn fibo_10() -> i32 {
21+
codspeed_divan_compat::black_box(fibo(10))
2422
}
2523

2624
fn main() {
27-
// Run `add` benchmark:
28-
divan::main();
25+
codspeed_divan_compat::main();
2926
}

crates/divan_compat/macros/Cargo.toml

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ repository = "https://github.com/CodSpeedHQ/codspeed-rust"
1111
homepage = "https://codspeed.io"
1212
license = "MIT OR Apache-2.0"
1313
categories = [
14-
"development-tools",
15-
"development-tools::profiling",
16-
"development-tools::testing",
14+
"development-tools",
15+
"development-tools::profiling",
16+
"development-tools::testing",
1717
]
1818
keywords = ["codspeed", "benchmark", "divan"]
1919

@@ -22,7 +22,11 @@ proc-macro = true
2222

2323
[dependencies]
2424
divan-macros = { version = "=0.1.17" }
25+
proc-macro-crate = "3.2.0"
2526
proc-macro2 = "1"
2627
quote = { version = "1", default-features = false }
2728
# Versions prior to *.18 fail to parse empty attribute metadata.
28-
syn = { version = "^2.0.18", default-features = false, features = ["full", "clone-impls", "parsing", "printing", "proc-macro"] }
29+
# syn = { version = "^2.0.18", default-features = false, features = ["full", "clone-impls", "parsing", "printing", "proc-macro"] }
30+
syn = { version = "^2.0.18", default-features = true, features = [
31+
"extra-traits",
32+
] }

crates/divan_compat/macros/src/lib.rs

Lines changed: 36 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
11
use proc_macro::TokenStream;
2-
use quote::quote;
2+
use proc_macro_crate::{crate_name, FoundCrate};
3+
use quote::{format_ident, quote};
34
use syn::{
4-
parse::Parse, parse_macro_input, punctuated::Punctuated, ItemFn, Meta, MetaNameValue, Token,
5+
parse::Parse,
6+
parse_macro_input,
7+
punctuated::Punctuated,
8+
ItemFn,
9+
Meta::{self, NameValue},
10+
MetaNameValue, Token,
511
};
612

713
struct MyBenchArgs {
@@ -25,21 +31,45 @@ pub fn bench_compat(attr: TokenStream, item: TokenStream) -> TokenStream {
2531

2632
for arg in parsed_args.args {
2733
match &arg {
28-
Meta::NameValue(MetaNameValue { path, .. }) if path.is_ident("crate") => {
34+
NameValue(MetaNameValue { path, .. }) if path.is_ident("crate") => {
2935
return quote! {
30-
compile_error!("crate argument is not supported with codspeed_divan_compat");
36+
compile_error!("`crate` argument is not supported with codspeed_divan_compat");
3137
}
3238
.into();
3339
}
40+
NameValue(MetaNameValue { path, .. }) if path.is_ident("types") => {
41+
return quote! {
42+
compile_error!("`type` argument is not yet supported with codspeed_divan_compat");
43+
}
44+
.into();
45+
}
46+
NameValue(MetaNameValue { path, .. })
47+
if path.is_ident("min_time")
48+
|| path.is_ident("max_time")
49+
|| path.is_ident("sample_size")
50+
|| path.is_ident("sample_count")
51+
|| path.is_ident("skip_ext_time") =>
52+
{
53+
// These arguments are ignored in instrumented mode
54+
}
3455
_ => filtered_args.push(arg),
3556
}
3657
}
3758

38-
filtered_args.push(syn::parse_quote!(crate = ::codspeed_divan_compat));
59+
let codspeed_divan_crate_ident = format_ident!(
60+
"{}",
61+
crate_name("codspeed-divan-compat")
62+
.map(|found_crate| match found_crate {
63+
FoundCrate::Itself => "crate".to_string(),
64+
FoundCrate::Name(name) => name,
65+
})
66+
.unwrap_or("codspeed_divan_compat".to_string())
67+
);
3968

69+
filtered_args.push(syn::parse_quote!(crate = ::#codspeed_divan_crate_ident));
4070
// Important: keep macro name in sync with re-exported macro name in divan-compat lib
4171
let expanded = quote! {
42-
#[::codspeed_divan_compat::bench_original(#(#filtered_args),*)]
72+
#[::#codspeed_divan_crate_ident::bench_original(#(#filtered_args),*)]
4373
#input
4474
};
4575

crates/divan_compat/src/compat/bench.rs

Lines changed: 0 additions & 44 deletions
This file was deleted.

crates/divan_compat/src/compat/entry.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,10 @@ use std::{
88
},
99
};
1010

11-
use super::bench::{BenchOptions, Bencher};
11+
use super::{
12+
bench::{BenchOptions, Bencher},
13+
BenchArgsRunner,
14+
};
1215

1316
/// Benchmark entries generated by `#[divan::bench]`.
1417
///
@@ -21,8 +24,8 @@ pub static BENCH_ENTRIES: EntryList<BenchEntry> = EntryList::root();
2124
pub enum BenchEntryRunner {
2225
/// Benchmark without arguments.
2326
Plain(fn(Bencher)),
24-
// /// Benchmark with runtime arguments.
25-
// Args(fn() -> BenchArgsRunner),
27+
/// Benchmark with runtime arguments.
28+
Args(fn() -> BenchArgsRunner),
2629
}
2730

2831
/// Compile-time entry for a benchmark, generated by `#[divan::bench]`.
@@ -50,7 +53,7 @@ pub struct EntryMeta {
5053
/// Where the entry was defined.
5154
pub location: EntryLocation,
5255
/// Configures the benchmarker via attribute options.
53-
pub bench_options: Option<LazyLock<BenchOptions<'static>>>,
56+
pub bench_options: Option<LazyLock<BenchOptions>>,
5457
}
5558

5659
/// Where an entry is located.

0 commit comments

Comments
 (0)