Debian Codegraph @

debian/rust-vendor/codspeed-divan-compat/src/compat/uri.rs

raw
1 use super::AnyBenchEntry;
2
3 /// Generate the codspeed URI for a benchmark entry.
4 /// The format is `"{file}::{module_path}::{bench_name}"`.
5 ///
6 /// # Bench Name Computation
7 /// There are three elements to consider:
8 /// - The static entry name from metadata, i.e., the name of the benchmarked function.
9 /// - The type (since benchmarks can be generic).
10 /// - The arguments (as you can statically specify a list of inputs for a benchmark).
11 ///
12 /// Depending on the nesting, you need to check three places:
13 /// - `entry.meta().display_name`
14 /// - `entry.display_name()`
15 /// - `closure_bench_display_name`, computed by divan when calling the closure that runs the bench
16 ///
17 /// From these three elements, we derive the codspeed bench name `function_name[type?, arg?]`:
18 /// - In the simple case (no generic, no args via macro), all three are equivalent.
19 /// - With an arg and no type, the first two are equal to the function name.
20 /// - With no arg and a type, the last two are equal to the type name.
21 /// - With both an arg and a type, all three have distinct values: the function name, the arg, and the type, respectively.
22 pub(crate) fn generate(bench_entry: &AnyBenchEntry, closure_bench_display_name: &str) -> String {
23 let bench_function_name = bench_entry.meta().display_name;
24
25 let (bench_type_name, bench_arg_name) = {
26 let bench_function_or_type_name = bench_entry.display_name().to_string();
27
28 let type_name = if bench_function_or_type_name == bench_function_name {
29 None
30 } else {
31 Some(bench_function_or_type_name)
32 };
33
34 let arg_name = match type_name.as_ref() {
35 None => {
36 if closure_bench_display_name == bench_function_name {
37 None
38 } else {
39 Some(closure_bench_display_name)
40 }
41 }
42 Some(type_name) => {
43 if closure_bench_display_name == type_name {
44 None
45 } else {
46 Some(closure_bench_display_name)
47 }
48 }
49 };
50
51 (type_name, arg_name)
52 };
53
54 let mut bench_name = bench_function_name.to_string();
55
56 match (bench_type_name, bench_arg_name) {
57 (None, None) => {}
58 (Some(type_name), None) => {
59 bench_name.push_str(format!("[{type_name}]").as_str());
60 }
61 (None, Some(arg_name)) => {
62 bench_name.push_str(format!("[{arg_name}]").as_str());
63 }
64 (Some(type_name), Some(arg_name)) => {
65 bench_name.push_str(format!("[{type_name}, {arg_name}]").as_str());
66 }
67 }
68
69 let file = bench_entry.meta().location.file;
70 // In the context of a bench, the top level module will be a repetition of the file name, we
71 // chose to skip it
72 let mut module_path = bench_entry
73 .meta()
74 .module_path_components()
75 .skip(1)
76 .collect::<Vec<_>>()
77 .join("::");
78 if !module_path.is_empty() {
79 module_path.push_str("::");
80 }
81 let uri = format!("{file}::{module_path}{bench_name}");
82
83 uri
84 }
85
86 #[cfg(test)]
87 mod tests {
88 use crate::__private::*;
89
90 use super::*;
91
92 #[test]
93 fn test_generate_simple_case() {
94 let meta = EntryMeta {
95 display_name: "bench_function",
96 raw_name: "bench_function",
97 module_path: "test::module",
98 location: EntryLocation {
99 file: "foo.rs",
100 ..Default::default()
101 },
102 bench_options: None,
103 };
104 let bench_entry = BenchEntry {
105 meta,
106 bench: BenchEntryRunner::Plain(|_| {}),
107 };
108 let closure_bench_display_name = "bench_function";
109 let uri = generate(
110 &AnyBenchEntry::Bench(&bench_entry),
111 closure_bench_display_name,
112 );
113 assert_eq!(uri, "foo.rs::module::bench_function");
114 }
115
116 #[test]
117 fn test_generate_with_arg() {
118 let meta = EntryMeta {
119 display_name: "bench_function",
120 raw_name: "bench_function",
121 module_path: "test::module",
122 location: EntryLocation {
123 file: "foo.rs",
124 ..Default::default()
125 },
126 bench_options: None,
127 };
128 let bench_entry = BenchEntry {
129 meta,
130 bench: BenchEntryRunner::Plain(|_| {}),
131 };
132 let closure_bench_display_name = "ArgName";
133 let uri = generate(
134 &AnyBenchEntry::Bench(&bench_entry),
135 closure_bench_display_name,
136 );
137 assert_eq!(uri, "foo.rs::module::bench_function[ArgName]");
138 }
139
140 #[test]
141 fn test_generate_no_module_path() {
142 let meta = EntryMeta {
143 display_name: "bench_function",
144 raw_name: "bench_function",
145 module_path: "test",
146 location: EntryLocation {
147 file: "bar.rs",
148 ..Default::default()
149 },
150 bench_options: None,
151 };
152 let bench_entry = BenchEntry {
153 meta,
154 bench: BenchEntryRunner::Plain(|_| {}),
155 };
156 let closure_bench_display_name = "bench_function";
157 let uri = generate(
158 &AnyBenchEntry::Bench(&bench_entry),
159 closure_bench_display_name,
160 );
161 assert_eq!(uri, "bar.rs::bench_function");
162 }
163
164 #[allow(non_upper_case_globals)]
165 static mock_group_entry: GroupEntry = GroupEntry {
166 meta: EntryMeta {
167 display_name: "bench_function",
168 raw_name: "bench_function",
169 module_path: "test::module",
170 location: EntryLocation {
171 file: "main.rs",
172 line: 0,
173 col: 0,
174 },
175 bench_options: None,
176 },
177 generic_benches: None,
178 };
179 #[test]
180 fn test_generate_with_type() {
181 // Without arg
182 let hashmap_bench_entry = GenericBenchEntry {
183 group: &mock_group_entry,
184 bench: BenchEntryRunner::Plain(|_| {}),
185 ty: Some(EntryType::new::<std::collections::HashMap<&str, f64>>()),
186 const_value: None,
187 };
188 let entry = AnyBenchEntry::GenericBench(&hashmap_bench_entry);
189 let uri = generate(&entry, entry.display_name());
190 assert_eq!(uri, "main.rs::module::bench_function[HashMap<&str, f64>]");
191 }
192
193 #[test]
194 fn test_generate_with_type_and_arg() {
195 let vec_bench_entry = GenericBenchEntry {
196 group: &mock_group_entry,
197 bench: BenchEntryRunner::Plain(|_| {}),
198 ty: Some(EntryType::new::<Vec<f64>>()),
199 const_value: None,
200 };
201
202 let closure_bench_display_name = "ArgName";
203 let uri = generate(
204 &AnyBenchEntry::GenericBench(&vec_bench_entry),
205 closure_bench_display_name,
206 );
207 assert_eq!(uri, "main.rs::module::bench_function[Vec<f64>, ArgName]");
208 }
209 }