| 1 | use criterion::{black_box, criterion_group, criterion_main, Criterion, SamplingMode}; |
| 2 | use yrs::encoding::read::{Cursor, Read}; |
| 3 | use yrs::encoding::write::Write; |
| 4 | use yrs::Any; |
| 5 | |
| 6 | const BENCHMARK_SIZE: u32 = 100000; |
| 7 | |
| 8 | fn bench_encoding(c: &mut Criterion) { |
| 9 | let mut encoding_group = c.benchmark_group("encoding"); |
| 10 | encoding_group.sampling_mode(SamplingMode::Flat); |
| 11 | |
| 12 | encoding_group.bench_function("var_int (64 bit)", |b| { |
| 13 | b.iter(|| { |
| 14 | let mut encoder = Vec::with_capacity(BENCHMARK_SIZE as usize * 8); |
| 15 | for i in 0..(BENCHMARK_SIZE as i64) { |
| 16 | encoder.write_var(i); |
| 17 | } |
| 18 | let mut decoder = Cursor::from(&encoder); |
| 19 | for i in 0..(BENCHMARK_SIZE as i64) { |
| 20 | let num: i64 = decoder.read_var().unwrap(); |
| 21 | assert_eq!(num, i); |
| 22 | } |
| 23 | }) |
| 24 | }); |
| 25 | |
| 26 | encoding_group.bench_function("var_uint (32 bit)", |b| { |
| 27 | b.iter(|| { |
| 28 | let mut encoder = Vec::with_capacity(BENCHMARK_SIZE as usize * 8); |
| 29 | for i in 0..BENCHMARK_SIZE { |
| 30 | encoder.write_var(i); |
| 31 | } |
| 32 | let mut decoder = Cursor::from(&encoder); |
| 33 | for i in 0..BENCHMARK_SIZE { |
| 34 | let num: u32 = decoder.read_var().unwrap(); |
| 35 | assert_eq!(num, i); |
| 36 | } |
| 37 | }) |
| 38 | }); |
| 39 | |
| 40 | encoding_group.bench_function("uint32", |b| { |
| 41 | b.iter(|| { |
| 42 | let mut encoder = Vec::with_capacity(BENCHMARK_SIZE as usize * 8); |
| 43 | for i in 0..BENCHMARK_SIZE { |
| 44 | encoder.write_u32(i); |
| 45 | } |
| 46 | let mut decoder = Cursor::from(&encoder); |
| 47 | for i in 0..BENCHMARK_SIZE { |
| 48 | let num: u32 = decoder.read_u32().unwrap(); |
| 49 | assert_eq!(num, i); |
| 50 | } |
| 51 | }) |
| 52 | }); |
| 53 | |
| 54 | encoding_group.bench_function("var_uint (64 bit)", |b| { |
| 55 | b.iter(|| { |
| 56 | let mut encoder = Vec::with_capacity(BENCHMARK_SIZE as usize * 8); |
| 57 | for i in 0..(BENCHMARK_SIZE as u64) { |
| 58 | encoder.write_var(i); |
| 59 | } |
| 60 | let mut decoder = Cursor::from(&encoder); |
| 61 | for i in 0..(BENCHMARK_SIZE as u64) { |
| 62 | let num: u64 = decoder.read_var().unwrap(); |
| 63 | assert_eq!(num, i); |
| 64 | } |
| 65 | }) |
| 66 | }); |
| 67 | |
| 68 | encoding_group.bench_function("uint64", |b| { |
| 69 | b.iter(|| { |
| 70 | let mut encoder = Vec::with_capacity(BENCHMARK_SIZE as usize * 8); |
| 71 | for i in 0..(BENCHMARK_SIZE as u64) { |
| 72 | encoder.write_u64(i) |
| 73 | } |
| 74 | let mut decoder = Cursor::from(&encoder); |
| 75 | for i in 0..(BENCHMARK_SIZE as u64) { |
| 76 | let num = decoder.read_u64().unwrap(); |
| 77 | assert_eq!(num, i); |
| 78 | } |
| 79 | }) |
| 80 | }); |
| 81 | } |
| 82 | |
| 83 | fn bench_serialization(c: &mut Criterion) { |
| 84 | use std::collections::HashMap; |
| 85 | |
| 86 | let any = Any::from(HashMap::from([ |
| 87 | ("bool".into(), Any::from(true)), |
| 88 | ("int".into(), Any::from(1)), |
| 89 | ("negativeInt".into(), Any::from(-1)), |
| 90 | ("maxInt".into(), Any::from(i64::MAX)), |
| 91 | ("minInt".into(), Any::from(i64::MIN)), |
| 92 | ("realNumber".into(), Any::from(-123.2387f64)), |
| 93 | ("maxNumber".into(), Any::from(f64::MIN)), |
| 94 | ("minNumber".into(), Any::from(f64::MAX)), |
| 95 | ("null".into(), Any::Null), |
| 96 | ( |
| 97 | "map".into(), |
| 98 | Any::from(HashMap::from([ |
| 99 | ("bool".into(), Any::from(true)), |
| 100 | ("int".into(), Any::from(1)), |
| 101 | ("negativeInt".into(), Any::from(-1)), |
| 102 | ("maxInt".into(), Any::from(i64::MAX)), |
| 103 | ("minInt".into(), Any::from(i64::MIN)), |
| 104 | ("realNumber".into(), Any::from(-123.2387f64)), |
| 105 | ("maxNumber".into(), Any::from(f64::MIN)), |
| 106 | ("minNumber".into(), Any::from(f64::MAX)), |
| 107 | ("null".into(), Any::Null), |
| 108 | ])), |
| 109 | ), |
| 110 | ( |
| 111 | "key6".into(), |
| 112 | Any::from(vec![ |
| 113 | Any::from(true), |
| 114 | Any::from(1), |
| 115 | Any::from(-1), |
| 116 | Any::from(i64::MAX), |
| 117 | Any::from(i64::MIN), |
| 118 | Any::from(-123.2387f64), |
| 119 | Any::from(f64::MIN), |
| 120 | Any::from(f64::MAX), |
| 121 | Any::Null, |
| 122 | ]), |
| 123 | ), |
| 124 | ])); |
| 125 | |
| 126 | let any_json = serde_json::to_string(&any).unwrap(); |
| 127 | |
| 128 | let mut serde_group = c.benchmark_group("serde"); |
| 129 | serde_group.bench_function("Any-JSON roundtrip", |b| { |
| 130 | b.iter(|| { |
| 131 | black_box(serde_json::from_str::<Any>(&serde_json::to_string(&any).unwrap()).unwrap()); |
| 132 | }) |
| 133 | }); |
| 134 | |
| 135 | serde_group.bench_function("Any serialize", |b| { |
| 136 | b.iter(|| { |
| 137 | black_box(serde_json::to_string(&any).unwrap()); |
| 138 | }) |
| 139 | }); |
| 140 | |
| 141 | serde_group.bench_function("Any deserialize", |b| { |
| 142 | b.iter(|| { |
| 143 | black_box(serde_json::to_string(&any).unwrap()); |
| 144 | }) |
| 145 | }); |
| 146 | |
| 147 | serde_group.finish(); |
| 148 | |
| 149 | let mut custom_group = c.benchmark_group("custom json serialization"); |
| 150 | |
| 151 | custom_group.bench_function("Any-JSON roundtrip", |b| { |
| 152 | b.iter(|| { |
| 153 | let mut roundtrip = String::new(); |
| 154 | any.to_json(&mut roundtrip); |
| 155 | |
| 156 | black_box(Any::from_json(&roundtrip)); |
| 157 | }) |
| 158 | }); |
| 159 | |
| 160 | custom_group.bench_function("Any serialize", |b| { |
| 161 | b.iter(|| { |
| 162 | let mut roundtrip = String::new(); |
| 163 | black_box(any.to_json(&mut roundtrip)); |
| 164 | }) |
| 165 | }); |
| 166 | |
| 167 | custom_group.bench_function("Any deserialize", |b| { |
| 168 | b.iter(|| { |
| 169 | black_box(Any::from_json(&any_json)); |
| 170 | }) |
| 171 | }); |
| 172 | |
| 173 | custom_group.finish(); |
| 174 | } |
| 175 | |
| 176 | criterion_group!(serialization, bench_encoding, bench_serialization); |
| 177 | criterion_main!(serialization); |
| 178 | |