Skip to content

Commit 3d4cac7

Browse files
committed
Fix benchmarks
1 parent 83c1183 commit 3d4cac7

File tree

4 files changed

+81
-19
lines changed

4 files changed

+81
-19
lines changed

lib/benches/benchmarks.rs

Lines changed: 70 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -39,29 +39,76 @@ fn random_resource(atom: &Atom) -> Resource {
3939
fn criterion_benchmark(c: &mut Criterion) {
4040
let store = Db::init_temp("bench").unwrap();
4141

42-
c.bench_function("add_resource", |b| {
43-
b.iter(|| {
44-
let resource = random_resource(&random_atom_string());
45-
store
46-
.add_resource_opts(&resource, true, true, false)
47-
.unwrap();
48-
})
42+
let mut flushing = c.benchmark_group("IO bound benchmarks");
43+
flushing.significance_level(0.1).sample_size(10);
44+
45+
flushing.bench_function("flush 100 resources", |b| {
46+
b.iter_batched(
47+
|| {
48+
// SETUP: Create 100 dirty resources
49+
for _ in 0..100 {
50+
let resource = random_resource(&random_atom_string());
51+
store
52+
.add_resource_opts(&resource, true, true, false)
53+
.unwrap();
54+
}
55+
},
56+
|()| {
57+
// MEASURE: Only the flush
58+
store.flush().unwrap();
59+
},
60+
criterion::BatchSize::SmallInput,
61+
)
4962
});
5063

51-
c.bench_function("resource.save() string", |b| {
52-
b.iter(|| {
53-
let mut resource = random_resource(&random_atom_string());
54-
resource.save(&store).unwrap();
64+
flushing.bench_function("resource.save() string", |b| {
65+
b.iter_custom(|iters| {
66+
let mut total_duration = std::time::Duration::new(0, 0);
67+
let mut i = 0;
68+
let flush_interval = 100;
69+
70+
while i < iters {
71+
let batch_size = std::cmp::min(flush_interval, iters - i);
72+
73+
let start = std::time::Instant::now();
74+
for _ in 0..batch_size {
75+
let mut resource = random_resource(&random_atom_string());
76+
resource.save_locally(&store).unwrap();
77+
}
78+
total_duration += start.elapsed();
79+
80+
store.flush().unwrap();
81+
i += batch_size;
82+
}
83+
total_duration
5584
})
5685
});
5786

58-
c.bench_function("resource.save() array", |b| {
59-
b.iter(|| {
60-
let mut resource = random_resource(&random_atom_array());
61-
resource.save(&store).unwrap();
87+
flushing.bench_function("resource.save() array", |b| {
88+
b.iter_custom(|iters| {
89+
let mut total_duration = std::time::Duration::new(0, 0);
90+
let mut i = 0;
91+
let flush_interval = 100;
92+
93+
while i < iters {
94+
let batch_size = std::cmp::min(flush_interval, iters - i);
95+
96+
let start = std::time::Instant::now();
97+
for _ in 0..batch_size {
98+
let mut resource = random_resource(&random_atom_array());
99+
resource.save_locally(&store).unwrap();
100+
}
101+
total_duration += start.elapsed();
102+
103+
store.flush().unwrap();
104+
i += batch_size;
105+
}
106+
total_duration
62107
})
63108
});
64109

110+
flushing.finish();
111+
65112
let big_resource = store
66113
.get_resource_extended(
67114
"https://localhost/collections",
@@ -94,13 +141,20 @@ fn criterion_benchmark(c: &mut Criterion) {
94141
})
95142
});
96143

97-
c.bench_function("all_resources()", |b| {
144+
let mut all_resources_group = c.benchmark_group("all_resources");
145+
all_resources_group.sample_size(10);
146+
147+
all_resources_group.bench_function("all_resources()", |b| {
98148
b.iter(|| {
99149
let _all = store.all_resources(false).collect::<Vec<Resource>>();
100150
})
101151
});
102152

153+
all_resources_group.finish();
154+
println!("Clearing store");
155+
// If this takes a long time, it probably means there is still a lot of data that needs to be flushed.
103156
store.clear_all_danger().unwrap();
157+
println!("Store cleared");
104158
}
105159

106160
criterion_group!(benches, criterion_benchmark);

lib/src/commit.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ impl Commit {
114114
/// Throws an error if the parent is set to itself
115115
pub fn check_for_circular_parents(&self) -> AtomicResult<()> {
116116
// Check if the set hashset has a parent property and if it matches with this subject.
117-
if let Some(set) = self.set.clone() {
117+
if let Some(set) = &self.set {
118118
if let Some(parent) = set.get(urls::PARENT) {
119119
if parent.to_string() == self.subject {
120120
return Err("Circular parent reference".into());

lib/src/db.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -278,10 +278,18 @@ impl Db {
278278
Ok(())
279279
}
280280

281+
/// Flushes the current state to disk.
282+
pub fn flush(&self) -> AtomicResult<()> {
283+
self.db
284+
.flush()
285+
.map_err(|e| format!("Failed to flush: {}", e).into())
286+
.map(|_| ())
287+
}
288+
281289
/// Removes the DB and all content from disk.
282290
/// WARNING: This is irreversible.
283291
pub fn clear_all_danger(self) -> AtomicResult<()> {
284-
self.clear_index()?;
292+
// self.clear_index()?;
285293
let path = self.path.clone();
286294
drop(self);
287295
fs::remove_dir_all(path)?;

lib/src/populate.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,7 @@ pub fn populate_default_store(store: &impl Storelike) -> AtomicResult<()> {
268268
)
269269
.map_err(|e| format!("Failed to import ontologies.json: {e}"))?;
270270
store
271-
.import(include_str!("../defaults/ai.json",), &ParseOpts::default())
271+
.import(include_str!("../defaults/ai.json"), &ParseOpts::default())
272272
.map_err(|e| format!("Failed to import ai.json: {e}"))?;
273273
Ok(())
274274
}

0 commit comments

Comments
 (0)