-
Notifications
You must be signed in to change notification settings - Fork 15
/
leveldb.rb
85 lines (67 loc) · 1.83 KB
/
leveldb.rb
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
require 'bundler/setup'
require 'leveldb'
require 'benchmark'
require 'minitest'
puts '## Please wait, I\'m generating 100mb of random data ...'
N = 10_240
SAMPLE = []
File.open('/dev/urandom', File::RDONLY || File::NONBLOCK || File::NOCTTY) do |f|
N.times { |i| SAMPLE << f.readpartial(5_120).unpack("H*")[0] }
end
db = LevelDB::DB.new '/tmp/bench', compression: false
db.clear!
puts '## Without compression:'
Benchmark.bm do |x|
x.report('put') { N.times { |i| db.put(i, SAMPLE[i]) } }
x.report('get') { N.times { |i| raise unless db.get(i) == SAMPLE[i] } }
end
db.reopen!
puts db.stats
puts
db.close; db.destroy
db = LevelDB::DB.new '/tmp/bench', bloom_filter_bits_per_key: 100
db.clear!
puts '## With bloom filter @ 100 bits/key:'
Benchmark.bm do |x|
x.report('put') { N.times { |i| db.put(i, SAMPLE[i]) } }
x.report('get') { N.times { |i| raise unless db.get(i) == SAMPLE[i] } }
end
db.reopen!
puts db.stats
puts
db.close; db.destroy
db = LevelDB::DB.new '/tmp/bench', compression: true
db.clear!
puts '## With compression:'
Benchmark.bm do |x|
x.report('put') { N.times { |i| db.put(i, SAMPLE[i]) } }
x.report('get') { N.times { |i| raise unless db.get(i) == SAMPLE[i] } }
end
db.reopen!
puts db.stats
puts
db.close; db.destroy
db = LevelDB::DB.new '/tmp/bench', compression: true, bloom_filter_bits_per_key: 100
db.clear!
puts '## With compression and bloom filter @ 100 bits/key:'
Benchmark.bm do |x|
x.report('put') { N.times { |i| db.put(i, SAMPLE[i]) } }
x.report('get') { N.times { |i| raise unless db.get(i) == SAMPLE[i] } }
end
db.reopen!
puts db.stats
puts
db.close; db.destroy
db = LevelDB::DB.new '/tmp/bench', compression: true
db.clear!
puts '## With batch:'
Benchmark.bm do |x|
x.report 'put' do
db.batch do |batch|
N.times { |i| batch.put(i, SAMPLE[i]) }
end
end
end
db.reopen!
puts db.stats
puts