Skip to content

Instantly share code, notes, and snippets.

@ogibayashi
Created November 6, 2014 01:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ogibayashi/d2a46df1c3edc852e3f7 to your computer and use it in GitHub Desktop.
Save ogibayashi/d2a46df1c3edc852e3f7 to your computer and use it in GitHub Desktop.
LevelDBテストのためのスクリプト
--- iterator.rb.bak 2014-10-15 11:09:30.378246696 +0900
+++ iterator.rb 2014-10-15 11:48:56.200251049 +0900
@@ -29,6 +29,7 @@
def each(&block)
return self unless block_given?
if current = self.next
+ break if range? && current[0] >= @_range[1]
block[*current]
end while valid?
@_range = nil
#!/usr/bin/env ruby
require 'leveldb'
require 'benchmark'
require 'optparse'
require 'fileutils'
opts = {}
ARGV.options {|opt|
opt.on('-n number', 'Number of records'){|v| opts[:nrec] = v.to_i }
opt.on('-d Directory', 'LevelDB directory path'){|v| opts[:dbpath] = v }
opt.on('-w','Wait for compaction'){ |v| opts[:wait] = true}
opt.on('-r','Put keys in random order'){ |v| opts[:random_order] = true}
opt.on('--debug','Debug mode (Print each key when put)'){ |v| opts[:debug] = true}
opt.on('--compress','Enable compression'){ |v| opts[:compression] = true}
opt.parse!
}
opts = {
:nrec => 5_000_000,
:dbpath => "/var/lib/leveldb",
:wait => false,
:random_order => false,
:debug => false,
:compression => false,
}.merge(opts)
puts opts
puts "Starting to put :" + Time.now.to_s
## make a new database
FileUtils.mkdir_p(opts[:dbpath])
db = LevelDB::DB.new(opts[:dbpath], {:block_cache_size => 0,:compression => opts[:compression]})
## getting and setting
mb = 1*1024
nrec_num_char = opts[:nrec].to_s.size
keys = (1..opts[:nrec]).to_a
keys.shuffle! if opts[:random_order]
Benchmark.bm(30) do |x|
x.report("#{opts[:nrec].to_s} Records"){
keys.each { |i|
puts i if opts[:debug]
db.put "%0#{nrec_num_char}d"%i, "ab"*mb
}
}
end
puts "Finished: " + Time.now.to_s
if opts[:wait]
stats_prev = ""
print "Waiting "
while db.stats != stats_prev
stats_prev = db.stats
print "."
sleep 10
end
end
puts
puts db.stats
puts "Completed: " + Time.now.to_s
db.close
#!/usr/bin/env ruby
require 'leveldb'
require 'benchmark'
require 'optparse'
opts = {}
ARGV.options {|opt|
opt.on('-d Directory', 'LevelDB directory path'){|v| opts[:dbpath] = v }
opt.on('-s Key range start (comma separated)', 'Key range start'){|v| opts[:start_keys] = v }
opt.on('-w Key range width', 'Key range width'){|v| opts[:width] = v.to_i }
opt.on('-i Number of iteration', 'Number of iteration'){|v| opts[:num_iteration] = v.to_i }
opt.on('-k key_length', 'Key length'){|v| opts[:key_length] = v.to_i }
opt.on('-r', 'Range scan'){ |v| opts[:range_scan] = true}
opt.parse!
}
opts = {
:dbpath => "/var/lib/leveldb",
:start_keys => "1",
:width => 1000,
:num_iteration => 1,
:range_scan => false
}.merge(opts)
puts opts
unless opts[:key_length]
puts "-k should be specified"
exit 1
end
opts[:start_keys] = opts[:start_keys].split(",").map{ |i| i.to_i }
## make a new database
db = LevelDB::DB.new(opts[:dbpath], {:block_cache_size => 0,:compression => false })
N=100
#ranges = [1,1000,5000,50000,500000,1000000,4999000].map{ |m| m..(m+1000) }
#ranges = [1,1000,5000,50000,500000,1000000,4999000].map{ |m| m..(m+500) }
#ranges = [1,1000,2000,3000,4000,5000,50000,500000,1000000,4999000].map{ |m| m..(m+500) }
ranges = opts[:start_keys].map{ |m| m..(m+opts[:width])}
Benchmark.bm(30) do |x|
opts[:num_iteration].times do |j|
ranges.each do |r|
db.reopen!
x.report("#{r.to_s}_#{j+1}") {
N.times do |i|
if opts[:range_scan]
nullio = open(File::NULL,"w")
it = db.each
it.range("%0#{opts[:key_length]}d"%r.first,"%0#{opts[:key_length]}d"%r.last) do |k,v|
nullio.puts k
end
it.instance_eval{ LevelDB::C.iter_destroy(@_iterator) }
nullio.close
else
r.each do |k|
db.get("%0#{opts[:key_length]}d"%k)
end
end
end
}
end
end
end
puts db.stats
db.close
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment