dodb.cr/spec/benchmark-cars.cr

130 lines
4.6 KiB
Crystal

require "benchmark"
require "./utilities.cr"
require "./db-cars.cr"
cars_ram = DODB::RAMOnlySpecDataBase(Car).new
cars_cached = DODB::CachedSpecDataBase(Car).new
cars_semi = DODB::SpecDataBase(Car).new "-semi"
cars_uncached = DODB::SpecDataBase(Car).new
ram_searchby_name, ram_searchby_color, ram_searchby_keywords = ram_indexes cars_ram
cached_searchby_name, cached_searchby_color, cached_searchby_keywords = cached_indexes cars_cached
semi_searchby_name, semi_searchby_color, semi_searchby_keywords = cached_indexes cars_semi
uncached_searchby_name, uncached_searchby_color, uncached_searchby_keywords = uncached_indexes cars_uncached
db_size = ENV["DBSIZE"].to_i rescue 1_000
car_name_to_search = ENV["CARNAME"] rescue "Corvet-#{(db_size/2).to_i}"
car_color_to_search = ENV["CARCOLOR"] rescue "red"
car_keyword_to_search = ENV["CARKEYWORD"] rescue "spacious"
add_cars cars_ram, db_size
add_cars cars_cached, db_size
add_cars cars_semi, db_size
add_cars cars_uncached, db_size
# Searching for data with an index.
puts "search by index '#{car_name_to_search}': get a single value"
Benchmark.ips do |x|
x.report("(ram db and index) searching with an index") do
corvet = ram_searchby_name.get car_name_to_search
end
x.report("(cached db and index) searching with an index") do
corvet = cached_searchby_name.get car_name_to_search
end
x.report("(semi: uncached db but cached index) searching with an index") do
corvet = semi_searchby_name.get car_name_to_search
end
x.report("(uncached db and index) searching with an index") do
corvet = uncached_searchby_name.get car_name_to_search
end
end
# Searching for data with a partition.
puts ""
puts "search by partition #{car_color_to_search}: get #{ram_searchby_color.get(car_color_to_search).size} values"
Benchmark.ips do |x|
x.report("(ram db and partition) searching with a partition") do
corvet = ram_searchby_color.get car_color_to_search
end
x.report("(cached db and partition) searching with a partition") do
corvet = cached_searchby_color.get car_color_to_search
end
x.report("(semi: uncached db but cached partition) searching with a partition") do
corvet = semi_searchby_color.get car_color_to_search
end
x.report("(uncached db and partition) searching with a partition") do
corvet = uncached_searchby_color.get car_color_to_search
end
end
# Searching for data with a tag.
puts ""
puts "search by tag #{car_keyword_to_search}: get #{ram_searchby_keywords.get(car_keyword_to_search).size} values"
Benchmark.ips do |x|
x.report("(ram db and tag) searching with a tag") do
corvet = ram_searchby_keywords.get car_keyword_to_search
end
x.report("(cached db and tag) searching with a tag") do
corvet = cached_searchby_keywords.get car_keyword_to_search
end
x.report("(semi: uncached db but cached tag) searching with a tag") do
corvet = semi_searchby_keywords.get car_keyword_to_search
end
x.report("(uncached db and tag) searching with a tag") do
corvet = uncached_searchby_keywords.get car_keyword_to_search
end
end
cars_ram.rm_storage_dir
cars_cached.rm_storage_dir
cars_semi.rm_storage_dir
cars_uncached.rm_storage_dir
cars_ram = DODB::RAMOnlySpecDataBase(Car).new
cars_cached = DODB::CachedSpecDataBase(Car).new
cars_semi = DODB::SpecDataBase(Car).new "-semi"
cars_uncached = DODB::SpecDataBase(Car).new
ram_searchby_name, ram_searchby_color, ram_searchby_keywords = ram_indexes cars_ram
cached_searchby_name, cached_searchby_color, cached_searchby_keywords = cached_indexes cars_cached
semi_searchby_name, semi_searchby_color, semi_searchby_keywords = cached_indexes cars_semi
uncached_searchby_name, uncached_searchby_color, uncached_searchby_keywords = uncached_indexes cars_uncached
def perform_add(storage : DODB::Storage(Car), nb_run)
corvet0 = Car.new "Corvet", "red", [ "shiny", "impressive", "fast", "elegant" ]
i = 0
perform_benchmark_average nb_run, do
corvet = corvet0.clone
corvet.name = "Corvet-#{i}"
storage << corvet
i += 1
end
end
nb_run = 1000
avr = perform_add(cars_ram, nb_run)
puts "(ram db and indexes) add a value (average on #{nb_run} tries): #{avr}"
avr = perform_add(cars_cached, nb_run)
puts "(cached db and indexes) add a value (average on #{nb_run} tries): #{avr}"
avr = perform_add(cars_semi, nb_run)
puts "(uncached db but cached indexes) add a value (average on #{nb_run} tries): #{avr}"
avr = perform_add(cars_uncached, nb_run)
puts "(uncached db and indexes) add a value (average on #{nb_run} tries): #{avr}"
cars_ram.rm_storage_dir
cars_cached.rm_storage_dir
cars_semi.rm_storage_dir
cars_uncached.rm_storage_dir