Skip to content

Commit 532d0fa

Browse files
committedSep 8, 2024
benchmarks: move all benchmarks to their own file
By having everything in one file it reduces clutter in the codebase, will make it easier to unify benchmark-generation code, and (IMO most importantly) lets you write `cargo bench benchmarks` and then it'll only run the benchmarks instead of outputting a gazillion "ignored" lines telling you that it's not benchmarking random shit that aren't benchmarks. Don't re-format stuff so it'll be obvious in the diff that this is a pure code move. Also we are going to rewrite the benchmarks in the next commit anyway.
1 parent 5d54ee3 commit 532d0fa

File tree

8 files changed

+126
-130
lines changed

8 files changed

+126
-130
lines changed
 

‎README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ please join us in
5757
## Benchmarks
5858

5959
We use a custom Rust compiler configuration conditional to guard the bench mark code. To run the
60-
bench marks use: `RUSTFLAGS='--cfg=bench' cargo +nightly bench`.
60+
benchmarks use: `RUSTFLAGS='--cfg=bench' cargo +nightly bench benchmarks`.
6161

6262

6363
## Release Notes

‎justfile

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ fmt-check:
2323

2424
# Run the benchmark suite.
2525
bench:
26-
RUSTFLAGS='--cfg=bench' cargo +nightly bench
26+
RUSTFLAGS='--cfg=bench' cargo +nightly bench benchmarks
2727

2828
# Build the docs (same as for docs.rs).
2929
docsrs:

‎src/benchmarks.rs

+122
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
// Written in 2019 by Andrew Poelstra <apoelstra@wpsoftware.net>
2+
// SPDX-License-Identifier: CC0-1.0
3+
4+
//! Benchmarks
5+
//!
6+
//! Benchmarks using the built-in rustc benchmark infrastructure. Requires a
7+
//! nightly compiler to run. See the README for exact instructions.
8+
//!
9+
10+
use test::{black_box, Bencher};
11+
12+
use crate::expression::Tree;
13+
use crate::miniscript::context;
14+
use crate::{Miniscript, ExtParams};
15+
16+
#[bench]
17+
pub fn parse_segwit0(bh: &mut Bencher) {
18+
bh.iter(|| {
19+
let tree = Miniscript::<String, context::Segwitv0>::from_str_ext(
20+
"and_v(v:pk(E),thresh(2,j:and_v(v:sha256(H),t:or_i(v:sha256(H),v:pkh(A))),s:pk(B),s:pk(C),s:pk(D),sjtv:sha256(H)))",
21+
&ExtParams::sane(),
22+
).unwrap();
23+
black_box(tree);
24+
});
25+
}
26+
27+
#[bench]
28+
pub fn parse_segwit0_deep(bh: &mut Bencher) {
29+
bh.iter(|| {
30+
let tree = Miniscript::<String, context::Segwitv0>::from_str_ext(
31+
"and_v(v:and_v(v:and_v(v:and_v(v:and_v(v:and_v(v:and_v(v:and_v(v:and_v(v:and_v(v:and_v(v:and_v(v:and_v(v:and_v(v:and_v(v:and_v(v:and_v(v:and_v(v:and_v(v:and_v(v:pk(1),pk(2)),pk(3)),pk(4)),pk(5)),pk(6)),pk(7)),pk(8)),pk(9)),pk(10)),pk(11)),pk(12)),pk(13)),pk(14)),pk(15)),pk(16)),pk(17)),pk(18)),pk(19)),pk(20)),pk(21))",
32+
&ExtParams::sane(),
33+
).unwrap();
34+
black_box(tree);
35+
});
36+
}
37+
38+
#[bench]
39+
pub fn parse_tree(bh: &mut Bencher) {
40+
bh.iter(|| {
41+
let tree = Tree::from_str(
42+
"and(thresh(2,and(sha256(H),or(sha256(H),pk(A))),pk(B),pk(C),pk(D),sha256(H)),pk(E))",
43+
).unwrap();
44+
black_box(tree);
45+
});
46+
}
47+
48+
#[bench]
49+
pub fn parse_tree_deep(bh: &mut Bencher) {
50+
bh.iter(|| {
51+
let tree = Tree::from_str(
52+
"and(and(and(and(and(and(and(and(and(and(and(and(and(and(and(and(and(and(and(and(1,2),3),4),5),6),7),8),9),10),11),12),13),14),15),16),17),18),19),20),21)"
53+
).unwrap();
54+
black_box(tree);
55+
});
56+
}
57+
58+
#[cfg(feature = "compiler")]
59+
mod compiler_benches {
60+
use super::*;
61+
62+
use core::str::FromStr;
63+
64+
use crate::Error;
65+
use crate::policy::Concrete;
66+
use crate::policy::compiler::CompilerError;
67+
use crate::descriptor::Descriptor;
68+
use crate::miniscript::Tap;
69+
use crate::prelude::*;
70+
71+
type TapMsRes = Result<Miniscript<String, Tap>, CompilerError>;
72+
type TapDesc = Result<Descriptor<String>, Error>;
73+
74+
#[bench]
75+
pub fn compile_large_tap(bh: &mut Bencher) {
76+
let pol = Concrete::<String>::from_str(
77+
"thresh(20,pk(A),pk(B),pk(C),pk(D),pk(E),pk(F),pk(G),pk(H),pk(I),pk(J),pk(K),pk(L),pk(M),pk(N),pk(O),pk(P),pk(Q),pk(R),pk(S),pk(T),pk(U),pk(V),pk(W),pk(X),pk(Y),pk(Z))",
78+
)
79+
.expect("parsing");
80+
bh.iter(|| {
81+
let pt: TapDesc = pol.compile_tr_private_experimental(Some("UNSPEND".to_string()));
82+
black_box(pt).unwrap();
83+
});
84+
}
85+
86+
#[bench]
87+
pub fn compile_basic(bh: &mut Bencher) {
88+
let h = (0..64).map(|_| "a").collect::<String>();
89+
let pol = Concrete::<String>::from_str(&format!(
90+
"and(thresh(2,and(sha256({}),or(sha256({}),pk(A))),pk(B),pk(C),pk(D),sha256({})),pk(E))",
91+
h, h, h
92+
))
93+
.expect("parsing");
94+
bh.iter(|| {
95+
let pt: TapMsRes = pol.compile();
96+
black_box(pt).unwrap();
97+
});
98+
}
99+
100+
#[bench]
101+
pub fn compile_large(bh: &mut Bencher) {
102+
let h = (0..64).map(|_| "a").collect::<String>();
103+
let pol = Concrete::<String>::from_str(
104+
&format!("or(pk(L),thresh(9,sha256({}),pk(A),pk(B),and(or(pk(C),pk(D)),pk(E)),after(100),pk(F),pk(G),pk(H),pk(I),and(pk(J),pk(K))))", h)
105+
).expect("parsing");
106+
bh.iter(|| {
107+
let pt: TapMsRes = pol.compile();
108+
black_box(pt).unwrap();
109+
});
110+
}
111+
112+
#[bench]
113+
pub fn compile_xlarge(bh: &mut Bencher) {
114+
let pol = Concrete::<String>::from_str(
115+
"or(pk(A),thresh(4,pk(B),older(100),pk(C),and(after(100),or(pk(D),or(pk(E),and(pk(F),thresh(2,pk(G),or(pk(H),and(thresh(5,pk(I),or(pk(J),pk(K)),pk(L),pk(M),pk(N),pk(O),pk(P),pk(Q),pk(R),pk(S),pk(T)),pk(U))),pk(V),or(and(pk(W),pk(X)),pk(Y)),after(100)))))),pk(Z)))"
116+
).expect("parsing");
117+
bh.iter(|| {
118+
let pt: TapMsRes = pol.compile();
119+
black_box(pt).unwrap();
120+
});
121+
}
122+
}

‎src/expression/mod.rs

-26
Original file line numberDiff line numberDiff line change
@@ -315,29 +315,3 @@ mod tests {
315315
}
316316
}
317317

318-
#[cfg(bench)]
319-
mod benches {
320-
use test::{black_box, Bencher};
321-
322-
use super::*;
323-
324-
#[bench]
325-
pub fn parse_tree(bh: &mut Bencher) {
326-
bh.iter(|| {
327-
let tree = Tree::from_str(
328-
"and(thresh(2,and(sha256(H),or(sha256(H),pk(A))),pk(B),pk(C),pk(D),sha256(H)),pk(E))",
329-
).unwrap();
330-
black_box(tree);
331-
});
332-
}
333-
334-
#[bench]
335-
pub fn parse_tree_deep(bh: &mut Bencher) {
336-
bh.iter(|| {
337-
let tree = Tree::from_str(
338-
"and(and(and(and(and(and(and(and(and(and(and(and(and(and(and(and(and(and(and(and(1,2),3),4),5),6),7),8),9),10),11),12),13),14),15),16),17),18),19),20),21)"
339-
).unwrap();
340-
black_box(tree);
341-
});
342-
}
343-
}

‎src/lib.rs

+2
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,8 @@ mod macros;
113113
#[macro_use]
114114
mod pub_macros;
115115

116+
#[cfg(bench)]
117+
mod benchmarks;
116118
mod blanket_traits;
117119
pub mod descriptor;
118120
pub mod expression;

‎src/miniscript/mod.rs

-29
Original file line numberDiff line numberDiff line change
@@ -1635,32 +1635,3 @@ mod tests {
16351635
Tapscript::parse_insane(&script.into_script()).unwrap_err();
16361636
}
16371637
}
1638-
1639-
#[cfg(bench)]
1640-
mod benches {
1641-
use test::{black_box, Bencher};
1642-
1643-
use super::*;
1644-
1645-
#[bench]
1646-
pub fn parse_segwit0(bh: &mut Bencher) {
1647-
bh.iter(|| {
1648-
let tree = Miniscript::<String, context::Segwitv0>::from_str_ext(
1649-
"and_v(v:pk(E),thresh(2,j:and_v(v:sha256(H),t:or_i(v:sha256(H),v:pkh(A))),s:pk(B),s:pk(C),s:pk(D),sjtv:sha256(H)))",
1650-
&ExtParams::sane(),
1651-
).unwrap();
1652-
black_box(tree);
1653-
});
1654-
}
1655-
1656-
#[bench]
1657-
pub fn parse_segwit0_deep(bh: &mut Bencher) {
1658-
bh.iter(|| {
1659-
let tree = Miniscript::<String, context::Segwitv0>::from_str_ext(
1660-
"and_v(v:and_v(v:and_v(v:and_v(v:and_v(v:and_v(v:and_v(v:and_v(v:and_v(v:and_v(v:and_v(v:and_v(v:and_v(v:and_v(v:and_v(v:and_v(v:and_v(v:and_v(v:and_v(v:and_v(v:pk(1),pk(2)),pk(3)),pk(4)),pk(5)),pk(6)),pk(7)),pk(8)),pk(9)),pk(10)),pk(11)),pk(12)),pk(13)),pk(14)),pk(15)),pk(16)),pk(17)),pk(18)),pk(19)),pk(20)),pk(21))",
1661-
&ExtParams::sane(),
1662-
).unwrap();
1663-
black_box(tree);
1664-
});
1665-
}
1666-
}

‎src/policy/compiler.rs

-49
Original file line numberDiff line numberDiff line change
@@ -1649,52 +1649,3 @@ mod tests {
16491649
}
16501650
}
16511651
}
1652-
1653-
#[cfg(bench)]
1654-
mod benches {
1655-
use std::str::FromStr;
1656-
1657-
use test::{black_box, Bencher};
1658-
1659-
use super::{CompilerError, Concrete};
1660-
use crate::miniscript::Tap;
1661-
use crate::prelude::*;
1662-
use crate::Miniscript;
1663-
type TapMsRes = Result<Miniscript<String, Tap>, CompilerError>;
1664-
#[bench]
1665-
pub fn compile_basic(bh: &mut Bencher) {
1666-
let h = (0..64).map(|_| "a").collect::<String>();
1667-
let pol = Concrete::<String>::from_str(&format!(
1668-
"and(thresh(2,and(sha256({}),or(sha256({}),pk(A))),pk(B),pk(C),pk(D),sha256({})),pk(E))",
1669-
h, h, h
1670-
))
1671-
.expect("parsing");
1672-
bh.iter(|| {
1673-
let pt: TapMsRes = pol.compile();
1674-
black_box(pt).unwrap();
1675-
});
1676-
}
1677-
1678-
#[bench]
1679-
pub fn compile_large(bh: &mut Bencher) {
1680-
let h = (0..64).map(|_| "a").collect::<String>();
1681-
let pol = Concrete::<String>::from_str(
1682-
&format!("or(pk(L),thresh(9,sha256({}),pk(A),pk(B),and(or(pk(C),pk(D)),pk(E)),after(100),pk(F),pk(G),pk(H),pk(I),and(pk(J),pk(K))))", h)
1683-
).expect("parsing");
1684-
bh.iter(|| {
1685-
let pt: TapMsRes = pol.compile();
1686-
black_box(pt).unwrap();
1687-
});
1688-
}
1689-
1690-
#[bench]
1691-
pub fn compile_xlarge(bh: &mut Bencher) {
1692-
let pol = Concrete::<String>::from_str(
1693-
"or(pk(A),thresh(4,pk(B),older(100),pk(C),and(after(100),or(pk(D),or(pk(E),and(pk(F),thresh(2,pk(G),or(pk(H),and(thresh(5,pk(I),or(pk(J),pk(K)),pk(L),pk(M),pk(N),pk(O),pk(P),pk(Q),pk(R),pk(S),pk(T)),pk(U))),pk(V),or(and(pk(W),pk(X)),pk(Y)),after(100)))))),pk(Z)))"
1694-
).expect("parsing");
1695-
bh.iter(|| {
1696-
let pt: TapMsRes = pol.compile();
1697-
black_box(pt).unwrap();
1698-
});
1699-
}
1700-
}

‎src/policy/mod.rs

-24
Original file line numberDiff line numberDiff line change
@@ -539,27 +539,3 @@ mod tests {
539539
}
540540
}
541541
}
542-
543-
#[cfg(all(bench, feature = "compiler"))]
544-
mod benches {
545-
use core::str::FromStr;
546-
547-
use test::{black_box, Bencher};
548-
549-
use super::{Concrete, Error};
550-
use crate::descriptor::Descriptor;
551-
use crate::prelude::*;
552-
type TapDesc = Result<Descriptor<String>, Error>;
553-
554-
#[bench]
555-
pub fn compile_large_tap(bh: &mut Bencher) {
556-
let pol = Concrete::<String>::from_str(
557-
"thresh(20,pk(A),pk(B),pk(C),pk(D),pk(E),pk(F),pk(G),pk(H),pk(I),pk(J),pk(K),pk(L),pk(M),pk(N),pk(O),pk(P),pk(Q),pk(R),pk(S),pk(T),pk(U),pk(V),pk(W),pk(X),pk(Y),pk(Z))",
558-
)
559-
.expect("parsing");
560-
bh.iter(|| {
561-
let pt: TapDesc = pol.compile_tr_private_experimental(Some("UNSPEND".to_string()));
562-
black_box(pt).unwrap();
563-
});
564-
}
565-
}

0 commit comments

Comments
 (0)
Please sign in to comment.