|
| 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 | +} |
0 commit comments