Skip to content

Commit

Permalink
nomerge: benchmark buffer sizes
Browse files Browse the repository at this point in the history
  • Loading branch information
aatifsyed committed Jun 29, 2023
1 parent 85d3271 commit 7a20c4c
Show file tree
Hide file tree
Showing 6 changed files with 385 additions and 0 deletions.
110 changes: 110 additions & 0 deletions examples/benchmark.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
use ahash::AHashMap;
use anyhow::Context as _;
use cid::Cid;
use std::{
fs::File,
io::{
self, BufReader,
ErrorKind::{InvalidData, Other},
Read, Seek, SeekFrom,
},
path::PathBuf,
};

#[derive(clap::Parser)]
struct Args {
path: PathBuf,
#[arg(short, long)]
mode: Mode,
}

#[derive(clap::ValueEnum, Clone)]
enum Mode {
Buffer8k,
Buffer1k,
Buffer100,
Unbuffered,
}

fn main() -> anyhow::Result<()> {
let Args { path, mode } = clap::Parser::parse();
let mut file = File::open(path)?;

let (_header_start, header_len) = next_varint_frame(&mut file)?.context("no header")?;
file.seek(SeekFrom::Current(i64::from(header_len)))?;
let index = match mode {
Mode::Buffer100 => {
let mut buffered = BufReader::with_capacity(100usize.next_power_of_two(), file);
std::iter::from_fn(|| read_block_location_or_eof(&mut buffered).transpose())
.collect::<Result<AHashMap<_, _>, _>>()?
}
Mode::Buffer1k => {
let mut buffered = BufReader::with_capacity(1000usize.next_power_of_two(), file);
std::iter::from_fn(|| read_block_location_or_eof(&mut buffered).transpose())
.collect::<Result<AHashMap<_, _>, _>>()?
}
Mode::Buffer8k => {
let mut buffered = BufReader::with_capacity(8000usize.next_power_of_two(), file);
std::iter::from_fn(|| read_block_location_or_eof(&mut buffered).transpose())
.collect::<Result<AHashMap<_, _>, _>>()?
}
Mode::Unbuffered => {
std::iter::from_fn(|| read_block_location_or_eof(&mut file).transpose())
.collect::<Result<AHashMap<_, _>, _>>()?
}
};

println!("{}", index.len());

Ok(())
}

/// Importantly, we seek _past_ the data, rather than read any in.
/// This allows us to keep indexing fast.
///
/// [`Ok(None)`] on EOF
fn read_block_location_or_eof(
mut reader: (impl Read + Seek),
) -> cid::Result<Option<(Cid, (u64, u32))>> {
let Some((frame_body_offset, body_length)) = next_varint_frame(&mut reader)? else {
return Ok(None)
};
let cid = Cid::read_bytes(&mut reader)?;
// tradeoff: we perform a second syscall here instead of in Blockstore::get,
// and keep BlockDataLocation purely for the blockdata
let block_data_offset = reader.stream_position()?;
let next_frame_offset = frame_body_offset + u64::from(body_length);
let block_data_length = u32::try_from(next_frame_offset - block_data_offset).unwrap();
reader.seek(SeekFrom::Start(next_frame_offset))?;
Ok(Some((cid, (block_data_offset, block_data_length))))
}

fn next_varint_frame(mut reader: (impl Read + Seek)) -> io::Result<Option<(u64, u32)>> {
Ok(match read_u32_or_eof(&mut reader)? {
Some(body_length) => {
let frame_body_offset = reader.stream_position()?;
Some((frame_body_offset, body_length))
}
None => None,
})
}

fn read_u32_or_eof(mut reader: impl Read) -> io::Result<Option<u32>> {
use unsigned_varint::io::{
read_u32,
ReadError::{Decode, Io},
};

let mut byte = [0u8; 1]; // detect EOF
match reader.read(&mut byte)? {
0 => Ok(None),
1 => read_u32(byte.chain(reader))
.map_err(|varint_error| match varint_error {
Io(e) => e,
Decode(e) => io::Error::new(InvalidData, e),
other => io::Error::new(Other, other),
})
.map(Some),
_ => unreachable!(),
}
}
84 changes: 84 additions & 0 deletions filecoin_full_calibnet_2023-04-07_450000.car-results.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
{
"results": [
{
"command": "./target/release/examples/benchmark --mode buffer8k /home/aatif/chainsafe/snapshots/filecoin_full_calibnet_2023-04-07_450000.car",
"mean": 226.5450566773,
"stddev": null,
"median": 226.5450566773,
"user": 23.980617099999996,
"system": 40.015509959999996,
"min": 226.5450566773,
"max": 226.5450566773,
"times": [
226.5450566773
],
"exit_codes": [
0
],
"parameters": {
"mode": "buffer8k",
"snapshot": "/home/aatif/chainsafe/snapshots/filecoin_full_calibnet_2023-04-07_450000.car"
}
},
{
"command": "./target/release/examples/benchmark --mode buffer1k /home/aatif/chainsafe/snapshots/filecoin_full_calibnet_2023-04-07_450000.car",
"mean": 237.0628938463,
"stddev": null,
"median": 237.0628938463,
"user": 27.164688099999996,
"system": 31.28079596,
"min": 237.0628938463,
"max": 237.0628938463,
"times": [
237.0628938463
],
"exit_codes": [
0
],
"parameters": {
"mode": "buffer1k",
"snapshot": "/home/aatif/chainsafe/snapshots/filecoin_full_calibnet_2023-04-07_450000.car"
}
},
{
"command": "./target/release/examples/benchmark --mode buffer100 /home/aatif/chainsafe/snapshots/filecoin_full_calibnet_2023-04-07_450000.car",
"mean": 216.5353994763,
"stddev": null,
"median": 216.5353994763,
"user": 26.700675099999998,
"system": 25.51678196,
"min": 216.5353994763,
"max": 216.5353994763,
"times": [
216.5353994763
],
"exit_codes": [
0
],
"parameters": {
"mode": "buffer100",
"snapshot": "/home/aatif/chainsafe/snapshots/filecoin_full_calibnet_2023-04-07_450000.car"
}
},
{
"command": "./target/release/examples/benchmark --mode unbuffered /home/aatif/chainsafe/snapshots/filecoin_full_calibnet_2023-04-07_450000.car",
"mean": 234.79445332830002,
"stddev": null,
"median": 234.79445332830002,
"user": 27.946851099999996,
"system": 57.23329895999999,
"min": 234.79445332830002,
"max": 234.79445332830002,
"times": [
234.79445332830002
],
"exit_codes": [
0
],
"parameters": {
"mode": "unbuffered",
"snapshot": "/home/aatif/chainsafe/snapshots/filecoin_full_calibnet_2023-04-07_450000.car"
}
}
]
}
6 changes: 6 additions & 0 deletions filecoin_full_calibnet_2023-04-07_450000.car-results.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
| Command | Mean [s] | Min [s] | Max [s] | Relative |
|:---|---:|---:|---:|---:|
| `./target/release/examples/benchmark --mode buffer8k /home/aatif/chainsafe/snapshots/filecoin_full_calibnet_2023-04-07_450000.car` | 226.545 | 226.545 | 226.545 | 1.05 |
| `./target/release/examples/benchmark --mode buffer1k /home/aatif/chainsafe/snapshots/filecoin_full_calibnet_2023-04-07_450000.car` | 237.063 | 237.063 | 237.063 | 1.09 |
| `./target/release/examples/benchmark --mode buffer100 /home/aatif/chainsafe/snapshots/filecoin_full_calibnet_2023-04-07_450000.car` | 216.535 | 216.535 | 216.535 | 1.00 |
| `./target/release/examples/benchmark --mode unbuffered /home/aatif/chainsafe/snapshots/filecoin_full_calibnet_2023-04-07_450000.car` | 234.794 | 234.794 | 234.794 | 1.08 |
156 changes: 156 additions & 0 deletions forest_snapshot_calibnet_2023-06-29_height_690463.car-results.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
{
"results": [
{
"command": "./target/release/examples/benchmark --mode buffer8k /home/aatif/chainsafe/snapshots/forest_snapshot_calibnet_2023-06-29_height_690463.car",
"mean": 3.22930254264,
"stddev": 0.24806820070407667,
"median": 3.20044537534,
"user": 1.33205636,
"system": 1.8969634800000001,
"min": 2.9457971943400003,
"max": 3.66001846734,
"times": [
3.02743013234,
2.9457971943400003,
2.99231720134,
3.43576581334,
3.66001846734,
3.2353239723400002,
3.02081795234,
3.54482978834,
3.26515812634,
3.16556677834
],
"exit_codes": [
0,
0,
0,
0,
0,
0,
0,
0,
0,
0
],
"parameters": {
"mode": "buffer8k",
"snapshot": "/home/aatif/chainsafe/snapshots/forest_snapshot_calibnet_2023-06-29_height_690463.car"
}
},
{
"command": "./target/release/examples/benchmark --mode buffer1k /home/aatif/chainsafe/snapshots/forest_snapshot_calibnet_2023-06-29_height_690463.car",
"mean": 2.4425066196400005,
"stddev": 0.14781555916787464,
"median": 2.40402877584,
"user": 1.16816886,
"system": 1.27421748,
"min": 2.27591901834,
"max": 2.6675089673400003,
"times": [
2.61340454034,
2.4917311623400002,
2.4223377733400002,
2.6675089673400003,
2.62264870134,
2.28394702334,
2.38571977834,
2.27591901834,
2.34694624934,
2.31490298234
],
"exit_codes": [
0,
0,
0,
0,
0,
0,
0,
0,
0,
0
],
"parameters": {
"mode": "buffer1k",
"snapshot": "/home/aatif/chainsafe/snapshots/forest_snapshot_calibnet_2023-06-29_height_690463.car"
}
},
{
"command": "./target/release/examples/benchmark --mode buffer100 /home/aatif/chainsafe/snapshots/forest_snapshot_calibnet_2023-06-29_height_690463.car",
"mean": 2.56212955884,
"stddev": 0.3054505541872568,
"median": 2.5132598233400003,
"user": 1.2454192599999998,
"system": 1.31649688,
"min": 2.14895057434,
"max": 3.04280050834,
"times": [
2.14895057434,
2.17850935934,
2.32006233534,
2.53696151734,
2.48955812934,
2.93091760134,
3.04280050834,
2.83684636334,
2.65415455634,
2.48253464334
],
"exit_codes": [
0,
0,
0,
0,
0,
0,
0,
0,
0,
0
],
"parameters": {
"mode": "buffer100",
"snapshot": "/home/aatif/chainsafe/snapshots/forest_snapshot_calibnet_2023-06-29_height_690463.car"
}
},
{
"command": "./target/release/examples/benchmark --mode unbuffered /home/aatif/chainsafe/snapshots/forest_snapshot_calibnet_2023-06-29_height_690463.car",
"mean": 5.787849323640001,
"stddev": 0.19237863484279846,
"median": 5.7791764078400005,
"user": 1.8388615600000002,
"system": 3.9487628799999994,
"min": 5.57331974934,
"max": 6.10544456634,
"times": [
6.10544456634,
5.97569307234,
5.75277783034,
5.92960746534,
5.93550799634,
5.57732547634,
5.805574985340001,
5.57331974934,
5.58887090634,
5.63437118834
],
"exit_codes": [
0,
0,
0,
0,
0,
0,
0,
0,
0,
0
],
"parameters": {
"mode": "unbuffered",
"snapshot": "/home/aatif/chainsafe/snapshots/forest_snapshot_calibnet_2023-06-29_height_690463.car"
}
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
| Command | Mean [s] | Min [s] | Max [s] | Relative |
|:---|---:|---:|---:|---:|
| `./target/release/examples/benchmark --mode buffer8k /home/aatif/chainsafe/snapshots/forest_snapshot_calibnet_2023-06-29_height_690463.car` | 3.229 ± 0.248 | 2.946 | 3.660 | 1.32 ± 0.13 |
| `./target/release/examples/benchmark --mode buffer1k /home/aatif/chainsafe/snapshots/forest_snapshot_calibnet_2023-06-29_height_690463.car` | 2.443 ± 0.148 | 2.276 | 2.668 | 1.00 |
| `./target/release/examples/benchmark --mode buffer100 /home/aatif/chainsafe/snapshots/forest_snapshot_calibnet_2023-06-29_height_690463.car` | 2.562 ± 0.305 | 2.149 | 3.043 | 1.05 ± 0.14 |
| `./target/release/examples/benchmark --mode unbuffered /home/aatif/chainsafe/snapshots/forest_snapshot_calibnet_2023-06-29_height_690463.car` | 5.788 ± 0.192 | 5.573 | 6.105 | 2.37 ± 0.16 |
23 changes: 23 additions & 0 deletions run.bash
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#!/usr/bin/env bash


snapshot=~/chainsafe/snapshots/forest_snapshot_calibnet_2023-06-29_height_690463.car
filename=$(basename $snapshot)
hyperfine \
--warmup 1 \
--export-markdown "$filename-results.md" \
--export-json "$filename-results.json" \
--parameter-list mode buffer8k,buffer1k,buffer100,unbuffered \
--parameter-list snapshot "$snapshot" \
'./target/release/examples/benchmark --mode {mode} {snapshot}'

snapshot=~/chainsafe/snapshots/filecoin_full_calibnet_2023-04-07_450000.car
filename=$(basename $snapshot)

hyperfine \
--runs 1 \
--export-markdown "$filename-results.md" \
--export-json "$filename-results.json" \
--parameter-list mode buffer8k,buffer1k,buffer100,unbuffered \
--parameter-list snapshot "$snapshot" \
'./target/release/examples/benchmark --mode {mode} {snapshot}'

0 comments on commit 7a20c4c

Please sign in to comment.