|
| 1 | +use core::fmt; |
| 2 | +use std::num::NonZeroU16; |
| 3 | + |
| 4 | +use criterion::{criterion_group, criterion_main, BenchmarkId, Criterion, SamplingMode, Throughput}; |
| 5 | +use spacetimedb_commitlog::{Commitlog, Options, Transaction}; |
| 6 | +use spacetimedb_paths::{server::CommitLogDir, FromPathUnchecked as _}; |
| 7 | +use tempfile::tempdir_in; |
| 8 | + |
| 9 | +mod common; |
| 10 | +use common::Payload; |
| 11 | + |
| 12 | +struct Params { |
| 13 | + payloads: Box<[Payload]>, |
| 14 | + txs_per_commit: NonZeroU16, |
| 15 | + total_appends: u64, |
| 16 | + fsync_every: u64, |
| 17 | +} |
| 18 | + |
| 19 | +impl Params { |
| 20 | + fn with_payloads(payloads: impl Into<Box<[Payload]>>) -> Self { |
| 21 | + Self { |
| 22 | + payloads: payloads.into(), |
| 23 | + txs_per_commit: NonZeroU16::new(1).unwrap(), |
| 24 | + total_appends: 1_000, |
| 25 | + fsync_every: 32, |
| 26 | + } |
| 27 | + } |
| 28 | +} |
| 29 | + |
| 30 | +impl fmt::Display for Params { |
| 31 | + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 32 | + write!( |
| 33 | + f, |
| 34 | + "n={} tx/commit={} fsync={}", |
| 35 | + self.total_appends, self.txs_per_commit, self.fsync_every |
| 36 | + ) |
| 37 | + } |
| 38 | +} |
| 39 | + |
| 40 | +fn bench_append(c: &mut Criterion, label: &str, params: Params) { |
| 41 | + let id = BenchmarkId::from_parameter(¶ms); |
| 42 | + c.benchmark_group(label) |
| 43 | + .sample_size(10) |
| 44 | + .sampling_mode(SamplingMode::Flat) |
| 45 | + .throughput(Throughput::Elements(params.total_appends)) |
| 46 | + .bench_with_input( |
| 47 | + id, |
| 48 | + ¶ms, |
| 49 | + |b, |
| 50 | + Params { |
| 51 | + payloads, |
| 52 | + txs_per_commit, |
| 53 | + total_appends, |
| 54 | + fsync_every, |
| 55 | + }| { |
| 56 | + let tmp = tempdir_in(".").unwrap(); |
| 57 | + let dir = CommitLogDir::from_path_unchecked(tmp.path()); |
| 58 | + let clog = Commitlog::open(dir, Options::default(), None).unwrap(); |
| 59 | + let mut offset = clog.max_committed_offset().unwrap_or_default(); |
| 60 | + |
| 61 | + b.iter(|| { |
| 62 | + let mut payloads = payloads.iter().cycle(); |
| 63 | + for i in 0..*total_appends { |
| 64 | + clog.commit(payloads.by_ref().take(txs_per_commit.get() as usize).map(|payload| { |
| 65 | + let tx = Transaction { |
| 66 | + offset, |
| 67 | + txdata: payload, |
| 68 | + }; |
| 69 | + offset += 1; |
| 70 | + tx |
| 71 | + })) |
| 72 | + .unwrap(); |
| 73 | + if i % fsync_every == 0 { |
| 74 | + clog.flush_and_sync().unwrap(); |
| 75 | + } |
| 76 | + } |
| 77 | + clog.flush_and_sync().unwrap(); |
| 78 | + }) |
| 79 | + }, |
| 80 | + ); |
| 81 | +} |
| 82 | + |
| 83 | +fn baseline(c: &mut Criterion) { |
| 84 | + let params = Params::with_payloads([Payload::new([b'z'; 64])]); |
| 85 | + bench_append(c, "baseline", params); |
| 86 | +} |
| 87 | + |
| 88 | +fn large_payload(c: &mut Criterion) { |
| 89 | + let params = Params::with_payloads([Payload::new([b'z'; 4096])]); |
| 90 | + bench_append(c, "large payload", params); |
| 91 | +} |
| 92 | + |
| 93 | +fn mixed_payloads(c: &mut Criterion) { |
| 94 | + let params = Params::with_payloads([ |
| 95 | + Payload::new([b'a'; 64]), |
| 96 | + Payload::new([b'b'; 512]), |
| 97 | + Payload::new([b'c'; 1024]), |
| 98 | + Payload::new([b'd'; 4096]), |
| 99 | + Payload::new([b'e'; 8102]), |
| 100 | + ]); |
| 101 | + bench_append(c, "mixed payloads", params); |
| 102 | +} |
| 103 | + |
| 104 | +fn mixed_payloads_with_batching(c: &mut Criterion) { |
| 105 | + let params = Params { |
| 106 | + txs_per_commit: NonZeroU16::new(16).unwrap(), |
| 107 | + ..Params::with_payloads([ |
| 108 | + Payload::new([b'a'; 64]), |
| 109 | + Payload::new([b'b'; 512]), |
| 110 | + Payload::new([b'c'; 1024]), |
| 111 | + Payload::new([b'd'; 4096]), |
| 112 | + Payload::new([b'e'; 8102]), |
| 113 | + ]) |
| 114 | + }; |
| 115 | + bench_append(c, "mixed payloads with batching", params); |
| 116 | +} |
| 117 | + |
| 118 | +criterion_group!( |
| 119 | + benches, |
| 120 | + baseline, |
| 121 | + large_payload, |
| 122 | + mixed_payloads, |
| 123 | + mixed_payloads_with_batching |
| 124 | +); |
| 125 | +criterion_main!(benches); |
0 commit comments