-
Notifications
You must be signed in to change notification settings - Fork 36
Expand file tree
/
Copy pathdisk.rs
More file actions
120 lines (105 loc) · 3.45 KB
/
disk.rs
File metadata and controls
120 lines (105 loc) · 3.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
use std::time::{Duration, Instant};
use criterion::{Criterion, black_box, criterion_group, criterion_main};
use hypercore::{Hypercore, HypercoreBuilder, HypercoreError, Storage};
use tempfile::Builder as TempfileBuilder;
fn bench_create_disk(c: &mut Criterion) {
let mut group = c.benchmark_group("slow_call");
group.measurement_time(Duration::from_secs(20));
#[cfg(feature = "tokio")]
group.bench_function("create_disk", move |b| {
let rt = tokio::runtime::Runtime::new().unwrap();
b.to_async(&rt).iter(|| create_hypercore("create"));
});
}
#[cfg(feature = "cache")]
async fn create_hypercore(name: &str) -> Result<Hypercore, HypercoreError> {
let dir = TempfileBuilder::new()
.prefix(name)
.tempdir()
.unwrap()
.keep();
let storage = Storage::new_disk(&dir, true).await?;
HypercoreBuilder::new(storage)
.node_cache_options(hypercore::CacheOptionsBuilder::new())
.build()
.await
}
#[cfg(not(feature = "cache"))]
async fn create_hypercore(name: &str) -> Result<Hypercore, HypercoreError> {
let dir = TempfileBuilder::new()
.prefix(name)
.tempdir()
.unwrap()
.keep();
let storage = Storage::new_disk(&dir, true).await?;
HypercoreBuilder::new(storage).build().await
}
fn bench_write_disk(c: &mut Criterion) {
let mut group = c.benchmark_group("slow_call");
group.measurement_time(Duration::from_secs(20));
#[cfg(feature = "tokio")]
group.bench_function("write disk", |b| {
let rt = tokio::runtime::Runtime::new().unwrap();
b.to_async(&rt).iter_custom(write_disk);
});
}
async fn write_disk(iters: u64) -> Duration {
let mut hypercore = create_hypercore("write").await.unwrap();
let data = Vec::from("hello");
let start = Instant::now();
for _ in 0..iters {
black_box(hypercore.append(&data).await.unwrap());
}
start.elapsed()
}
fn bench_read_disk(c: &mut Criterion) {
let mut group = c.benchmark_group("slow_call");
group.measurement_time(Duration::from_secs(20));
#[cfg(feature = "tokio")]
group.bench_function("read disk", |b| {
let rt = tokio::runtime::Runtime::new().unwrap();
b.to_async(&rt).iter_custom(read_disk);
});
}
async fn read_disk(iters: u64) -> Duration {
let mut hypercore = create_hypercore("read").await.unwrap();
let data = Vec::from("hello");
for _ in 0..iters {
hypercore.append(&data).await.unwrap();
}
let start = Instant::now();
for i in 0..iters {
black_box(hypercore.get(i).await.unwrap());
}
start.elapsed()
}
fn bench_clear_disk(c: &mut Criterion) {
let mut group = c.benchmark_group("slow_call");
group.measurement_time(Duration::from_secs(20));
#[cfg(feature = "tokio")]
group.bench_function("clear disk", |b| {
let rt = tokio::runtime::Runtime::new().unwrap();
b.to_async(&rt).iter_custom(clear_disk);
});
}
#[allow(clippy::unit_arg)]
async fn clear_disk(iters: u64) -> Duration {
let mut hypercore = create_hypercore("clear").await.unwrap();
let data = Vec::from("hello");
for _ in 0..iters {
hypercore.append(&data).await.unwrap();
}
let start = Instant::now();
for i in 0..iters {
black_box(hypercore.clear(i, 1).await.unwrap());
}
start.elapsed()
}
criterion_group!(
benches,
bench_create_disk,
bench_write_disk,
bench_read_disk,
bench_clear_disk
);
criterion_main!(benches);