|
| 1 | +// SPDX-License-Identifier: MIT or Apache-2.0 |
| 2 | +//! Performance benchmarks for multi-codec |
| 3 | +
|
| 4 | +use std::hint::black_box; |
| 5 | + |
| 6 | +use multi_codec::Codec; |
| 7 | +use multi_trait::TryDecodeFrom; |
| 8 | +use criterion::{criterion_group, criterion_main, BenchmarkId, Criterion}; |
| 9 | + |
| 10 | +/// Benchmark conversions from u64 |
| 11 | +fn bench_from_u64(c: &mut Criterion) { |
| 12 | + c.bench_function("codec_from_u64_valid", |b| { |
| 13 | + b.iter(|| Codec::try_from(black_box(0xEDu64))) |
| 14 | + }); |
| 15 | + |
| 16 | + c.bench_function("codec_from_u64_invalid", |b| { |
| 17 | + b.iter(|| Codec::try_from(black_box(0xDEADBEEFu64))) |
| 18 | + }); |
| 19 | +} |
| 20 | + |
| 21 | +/// Benchmark conversions to u64 |
| 22 | +fn bench_to_u64(c: &mut Criterion) { |
| 23 | + let codec = Codec::Ed25519Pub; |
| 24 | + |
| 25 | + c.bench_function("codec_to_u64", |b| { |
| 26 | + b.iter(|| { |
| 27 | + let _code: u64 = black_box(codec).into(); |
| 28 | + }) |
| 29 | + }); |
| 30 | + |
| 31 | + c.bench_function("codec_code_method", |b| b.iter(|| black_box(codec).code())); |
| 32 | +} |
| 33 | + |
| 34 | +/// Benchmark conversions from string |
| 35 | +fn bench_from_str(c: &mut Criterion) { |
| 36 | + c.bench_function("codec_from_str_valid", |b| { |
| 37 | + b.iter(|| Codec::try_from(black_box("ed25519-pub"))) |
| 38 | + }); |
| 39 | + |
| 40 | + c.bench_function("codec_from_str_invalid", |b| { |
| 41 | + b.iter(|| Codec::try_from(black_box("unknown-codec"))) |
| 42 | + }); |
| 43 | +} |
| 44 | + |
| 45 | +/// Benchmark conversions to string |
| 46 | +fn bench_to_str(c: &mut Criterion) { |
| 47 | + c.bench_function("codec_as_str", |b| { |
| 48 | + let codec = Codec::Ed25519Pub; |
| 49 | + b.iter(|| { |
| 50 | + let s = codec.as_str(); |
| 51 | + black_box(s.len()) |
| 52 | + }) |
| 53 | + }); |
| 54 | +} |
| 55 | + |
| 56 | +/// Benchmark encoding |
| 57 | +fn bench_encoding(c: &mut Criterion) { |
| 58 | + let mut group = c.benchmark_group("encoding"); |
| 59 | + |
| 60 | + let codecs = vec![ |
| 61 | + ("Identity", Codec::Identity), |
| 62 | + ("Sha2256", Codec::Sha2256), |
| 63 | + ("Ed25519Pub", Codec::Ed25519Pub), |
| 64 | + ]; |
| 65 | + |
| 66 | + for (name, codec) in codecs { |
| 67 | + group.bench_with_input(BenchmarkId::new("into_vec", name), &codec, |b, &codec| { |
| 68 | + b.iter(|| { |
| 69 | + let _v: Vec<u8> = black_box(codec).into(); |
| 70 | + }) |
| 71 | + }); |
| 72 | + } |
| 73 | + |
| 74 | + group.finish(); |
| 75 | +} |
| 76 | + |
| 77 | +/// Benchmark decoding |
| 78 | +fn bench_decoding(c: &mut Criterion) { |
| 79 | + let mut group = c.benchmark_group("decoding"); |
| 80 | + |
| 81 | + let test_cases = vec![ |
| 82 | + ("Identity", vec![0x00]), |
| 83 | + ("Sha2256", vec![0x12]), |
| 84 | + ("Ed25519Pub", vec![0xED, 0x01]), |
| 85 | + ]; |
| 86 | + |
| 87 | + for (name, bytes) in test_cases { |
| 88 | + group.bench_with_input( |
| 89 | + BenchmarkId::new("try_decode_from", name), |
| 90 | + &bytes, |
| 91 | + |b, bytes| b.iter(|| Codec::try_decode_from(black_box(bytes.as_slice()))), |
| 92 | + ); |
| 93 | + } |
| 94 | + |
| 95 | + group.finish(); |
| 96 | +} |
| 97 | + |
| 98 | +/// Benchmark hash operations |
| 99 | +fn bench_hash(c: &mut Criterion) { |
| 100 | + use std::collections::hash_map::DefaultHasher; |
| 101 | + use std::hash::{Hash, Hasher}; |
| 102 | + |
| 103 | + let codec = Codec::Ed25519Pub; |
| 104 | + |
| 105 | + c.bench_function("codec_hash", |b| { |
| 106 | + b.iter(|| { |
| 107 | + let mut hasher = DefaultHasher::new(); |
| 108 | + black_box(codec).hash(&mut hasher); |
| 109 | + hasher.finish() |
| 110 | + }) |
| 111 | + }); |
| 112 | +} |
| 113 | + |
| 114 | +/// Benchmark roundtrip operations |
| 115 | +fn bench_roundtrip(c: &mut Criterion) { |
| 116 | + let mut group = c.benchmark_group("roundtrip"); |
| 117 | + |
| 118 | + group.bench_function("encode_decode", |b| { |
| 119 | + let codec = Codec::Ed25519Pub; |
| 120 | + b.iter(|| { |
| 121 | + let encoded: Vec<u8> = black_box(codec).into(); |
| 122 | + let (decoded, _) = Codec::try_decode_from(black_box(&encoded)).unwrap(); |
| 123 | + black_box(decoded); |
| 124 | + }) |
| 125 | + }); |
| 126 | + |
| 127 | + group.bench_function("str_codec_str", |b| { |
| 128 | + b.iter(|| { |
| 129 | + let codec = Codec::try_from(black_box("ed25519-pub")).unwrap(); |
| 130 | + let len = codec.as_str().len(); |
| 131 | + black_box(len); |
| 132 | + }) |
| 133 | + }); |
| 134 | + |
| 135 | + group.bench_function("u64_codec_u64", |b| { |
| 136 | + b.iter(|| { |
| 137 | + let codec = Codec::try_from(black_box(0xEDu64)).unwrap(); |
| 138 | + let _code: u64 = codec.into(); |
| 139 | + }) |
| 140 | + }); |
| 141 | + |
| 142 | + group.finish(); |
| 143 | +} |
| 144 | + |
| 145 | +/// Benchmark serde operations |
| 146 | +#[cfg(feature = "serde")] |
| 147 | +fn bench_serde(c: &mut Criterion) { |
| 148 | + use serde_json; |
| 149 | + |
| 150 | + let mut group = c.benchmark_group("serde"); |
| 151 | + |
| 152 | + let codec = Codec::Ed25519Pub; |
| 153 | + |
| 154 | + group.bench_function("json_serialize", |b| { |
| 155 | + b.iter(|| serde_json::to_string(&black_box(codec))) |
| 156 | + }); |
| 157 | + |
| 158 | + let json = serde_json::to_string(&codec).unwrap(); |
| 159 | + group.bench_function("json_deserialize", |b| { |
| 160 | + b.iter(|| serde_json::from_str::<Codec>(black_box(&json))) |
| 161 | + }); |
| 162 | + |
| 163 | + group.bench_function("cbor_serialize", |b| { |
| 164 | + b.iter(|| serde_cbor::to_vec(&black_box(codec))) |
| 165 | + }); |
| 166 | + |
| 167 | + let cbor = serde_cbor::to_vec(&codec).unwrap(); |
| 168 | + group.bench_function("cbor_deserialize", |b| { |
| 169 | + b.iter(|| serde_cbor::from_slice::<Codec>(black_box(&cbor))) |
| 170 | + }); |
| 171 | + |
| 172 | + group.finish(); |
| 173 | +} |
| 174 | + |
| 175 | +#[cfg(feature = "serde")] |
| 176 | +criterion_group!( |
| 177 | + benches, |
| 178 | + bench_from_u64, |
| 179 | + bench_to_u64, |
| 180 | + bench_from_str, |
| 181 | + bench_to_str, |
| 182 | + bench_encoding, |
| 183 | + bench_decoding, |
| 184 | + bench_hash, |
| 185 | + bench_roundtrip, |
| 186 | + bench_serde |
| 187 | +); |
| 188 | + |
| 189 | +#[cfg(not(feature = "serde"))] |
| 190 | +criterion_group!( |
| 191 | + benches, |
| 192 | + bench_from_u64, |
| 193 | + bench_to_u64, |
| 194 | + bench_from_str, |
| 195 | + bench_to_str, |
| 196 | + bench_encoding, |
| 197 | + bench_decoding, |
| 198 | + bench_hash, |
| 199 | + bench_roundtrip |
| 200 | +); |
| 201 | + |
| 202 | +criterion_main!(benches); |
0 commit comments