summaryrefslogtreecommitdiffstats
path: root/third_party/rust/lmdb-rkv/benches/transaction.rs
blob: c0a713a88be24fe9d02e681ace7c481a5f0b6400 (plain)
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#![feature(test)]

extern crate libc;
extern crate lmdb;
extern crate lmdb_sys as ffi;
extern crate rand;
extern crate test;

mod utils;

use ffi::*;
use libc::size_t;
use lmdb::{
    Transaction,
    WriteFlags,
};
use rand::{
    Rng,
    XorShiftRng,
};
use std::ptr;
use test::{
    black_box,
    Bencher,
};
use utils::*;

#[bench]
fn bench_get_rand(b: &mut Bencher) {
    let n = 100u32;
    let (_dir, env) = setup_bench_db(n);
    let db = env.open_db(None).unwrap();
    let txn = env.begin_ro_txn().unwrap();

    let mut keys: Vec<String> = (0..n).map(get_key).collect();
    XorShiftRng::new_unseeded().shuffle(&mut keys[..]);

    b.iter(|| {
        let mut i = 0usize;
        for key in &keys {
            i += txn.get(db, key).unwrap().len();
        }
        black_box(i);
    });
}

#[bench]
fn bench_get_rand_raw(b: &mut Bencher) {
    let n = 100u32;
    let (_dir, env) = setup_bench_db(n);
    let db = env.open_db(None).unwrap();
    let _txn = env.begin_ro_txn().unwrap();

    let mut keys: Vec<String> = (0..n).map(get_key).collect();
    XorShiftRng::new_unseeded().shuffle(&mut keys[..]);

    let dbi = db.dbi();
    let txn = _txn.txn();

    let mut key_val: MDB_val = MDB_val {
        mv_size: 0,
        mv_data: ptr::null_mut(),
    };
    let mut data_val: MDB_val = MDB_val {
        mv_size: 0,
        mv_data: ptr::null_mut(),
    };

    b.iter(|| unsafe {
        let mut i: size_t = 0;
        for key in &keys {
            key_val.mv_size = key.len() as size_t;
            key_val.mv_data = key.as_bytes().as_ptr() as *mut _;

            mdb_get(txn, dbi, &mut key_val, &mut data_val);

            i += key_val.mv_size;
        }
        black_box(i);
    });
}

#[bench]
fn bench_put_rand(b: &mut Bencher) {
    let n = 100u32;
    let (_dir, env) = setup_bench_db(0);
    let db = env.open_db(None).unwrap();

    let mut items: Vec<(String, String)> = (0..n).map(|n| (get_key(n), get_data(n))).collect();
    XorShiftRng::new_unseeded().shuffle(&mut items[..]);

    b.iter(|| {
        let mut txn = env.begin_rw_txn().unwrap();
        for &(ref key, ref data) in items.iter() {
            txn.put(db, key, data, WriteFlags::empty()).unwrap();
        }
        txn.abort();
    });
}

#[bench]
fn bench_put_rand_raw(b: &mut Bencher) {
    let n = 100u32;
    let (_dir, _env) = setup_bench_db(0);
    let db = _env.open_db(None).unwrap();

    let mut items: Vec<(String, String)> = (0..n).map(|n| (get_key(n), get_data(n))).collect();
    XorShiftRng::new_unseeded().shuffle(&mut items[..]);

    let dbi = db.dbi();
    let env = _env.env();

    let mut key_val: MDB_val = MDB_val {
        mv_size: 0,
        mv_data: ptr::null_mut(),
    };
    let mut data_val: MDB_val = MDB_val {
        mv_size: 0,
        mv_data: ptr::null_mut(),
    };

    b.iter(|| unsafe {
        let mut txn: *mut MDB_txn = ptr::null_mut();
        mdb_txn_begin(env, ptr::null_mut(), 0, &mut txn);

        let mut i: ::libc::c_int = 0;
        for &(ref key, ref data) in items.iter() {
            key_val.mv_size = key.len() as size_t;
            key_val.mv_data = key.as_bytes().as_ptr() as *mut _;
            data_val.mv_size = data.len() as size_t;
            data_val.mv_data = data.as_bytes().as_ptr() as *mut _;

            i += mdb_put(txn, dbi, &mut key_val, &mut data_val, 0);
        }
        assert_eq!(0, i);
        mdb_txn_abort(txn);
    });
}