@@ -6,34 +6,51 @@ use tempfile::tempdir;
66
77fn insertion_benchmark ( c : & mut Criterion ) {
88 let mut group = c. benchmark_group ( "insertion" ) ;
9+
910 let num_docs = 10_000 ;
11+
1012 group. throughput ( Throughput :: Elements ( num_docs as u64 ) ) ;
1113
12- group. bench_function ( BenchmarkId :: new ( "insert" , num_docs) , |b| {
13- b. iter_custom ( |iters| {
14- let mut total_duration = std:: time:: Duration :: new ( 0 , 0 ) ;
15- for _ in 0 ..iters {
16- // Setup for each iteration: Create a new DB in a temp directory
17- let dir = tempdir ( ) . unwrap ( ) ;
18- let mut db = DB :: new ( dir. path ( ) . to_str ( ) . unwrap ( ) , num_docs + 1 , 10 ) ;
19- db. create_collection ( "test" ) . unwrap ( ) ;
20-
21- let start = std:: time:: Instant :: now ( ) ;
22-
23- // Benchmarked routine: Insert num_docs
24- for i in 0 ..num_docs {
25- db. insert ( "test" , hint:: black_box ( json ! ( { "a" : i } ) ) )
26- . unwrap ( ) ;
27- }
14+ for num_keys in [ 1 , 10 , 100 ] . iter ( ) {
15+ group. measurement_time ( std:: time:: Duration :: from_secs ( * num_keys * 5 ) ) ;
16+ group. bench_function ( BenchmarkId :: new ( "insert" , * num_keys) , |b| {
17+ b. iter_custom ( |iters| {
18+ let mut total_duration = std:: time:: Duration :: new ( 0 , 0 ) ;
19+
20+ for _ in 0 ..iters {
21+ // Setup for each iteration: Create a new DB in a temp directory
22+ let dir = tempdir ( ) . unwrap ( ) ;
23+ let mut db = DB :: new ( dir. path ( ) . to_str ( ) . unwrap ( ) , num_docs + 1 , 10 ) ;
24+
25+ db. create_collection ( "test" ) . unwrap ( ) ;
26+
27+ let start = std:: time:: Instant :: now ( ) ;
2828
29- total_duration += start. elapsed ( ) ;
29+ // Benchmarked routine: Insert num_docs
30+ for i in 0 ..num_docs {
31+ let doc = {
32+ let mut doc = serde_json:: Map :: new ( ) ;
33+
34+ for j in 0 ..* num_keys {
35+ doc. insert ( format ! ( "key{}" , j) , serde_json:: Value :: from ( i) ) ;
36+ }
37+
38+ serde_json:: Value :: Object ( doc)
39+ } ;
40+
41+ db. insert ( "test" , hint:: black_box ( doc) ) . unwrap ( ) ;
42+ }
43+
44+ total_duration += start. elapsed ( ) ;
45+
46+ // Teardown for each iteration: `dir` (TempDir) is dropped here,
47+ // cleaning up the temporary database files.
48+ }
3049
31- // Teardown for each iteration: `dir` (TempDir) is dropped here,
32- // cleaning up the temporary database files.
33- }
34- total_duration
35- } )
36- } ) ;
50+ total_duration
51+ } )
52+ } ) ;
53+ }
3754
3855 group. finish ( ) ;
3956}
0 commit comments