dodb.cr/spec/benchmark-cars.cr

198 lines
6.5 KiB
Crystal
Raw Normal View History

require "benchmark"
require "./utilities.cr"
require "./db-cars.cr"
# List of environment variables and default values:
# ENV["CARNAME"] rescue "Corvet-#{(db_size/2).to_i}"
# ENV["CARCOLOR"] rescue "red"
# ENV["CARKEYWORD"] rescue "spacious"
# ENV["DBSIZE"] rescue 1_000
# ENV["DBSIZE_START"] rescue 5_000
# ENV["DBSIZE_INCREMENT"] rescue 5_000
# ENV["REPORT_DIR"] rescue "results"
class DODB::Storage(V)
def empty_db
while pop
end
end
end
class Context
class_property report_dir = "results"
end
def report(storage, db_size, nb_run, name, title, &block)
#add_cars storage, db_size
2024-05-06 18:31:52 +02:00
durations = run_n_times nb_run, &block
File.open("#{Context.report_dir}/#{name}", "w") do |file|
durations.each do |d|
file.puts d
end
end
avr = durations.reduce { |a, b| a + b } / nb_run
puts "#{title}: #{avr}"
#storage.empty_db
avr
end
def batch(db_size, nb_run)
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
puts "add some values"
add_cars cars_ram, db_size
add_cars cars_cached, db_size
add_cars cars_semi, db_size
add_cars cars_uncached, db_size
2024-05-06 18:31:52 +02:00
# Searching for data with an index.
car_name_to_search = ENV["CARNAME"] rescue "Corvet-#{(db_size/2).to_i}"
puts "search by index '#{car_name_to_search}': get a single value"
report(cars_ram, db_size, nb_run, "ram_#{db_size}_index", "(ram db and index) searching with an index") do
corvet = ram_searchby_name.get car_name_to_search
end
report(cars_cached, db_size, nb_run, "cached_#{db_size}_index", "(cached db and index) searching with an index") do
corvet = cached_searchby_name.get car_name_to_search
2024-05-06 18:31:52 +02:00
end
report(cars_semi, db_size, nb_run, "semi_#{db_size}_index", "(semi: uncached db but cached index) searching with an index") do
corvet = semi_searchby_name.get car_name_to_search
end
report(cars_uncached, db_size, nb_run, "uncached_#{db_size}_index", "(uncached db and index) searching with an index") do
corvet = uncached_searchby_name.get car_name_to_search
end
# # Searching for data with a partition.
# car_color_to_search = ENV["CARCOLOR"] rescue "red"
# puts ""
# puts "search by partition #{car_color_to_search}: get #{ram_searchby_color.get(car_color_to_search).size} values"
# Benchmark.ips do |x|
# add_cars cars_ram, db_size
# x.report("(ram db and partition) searching with a partition") do
# corvet = ram_searchby_color.get car_color_to_search
# end
# cars_ram.empty_db
#
# add_cars cars_cached, db_size
# x.report("(cached db and partition) searching with a partition") do
# corvet = cached_searchby_color.get car_color_to_search
# end
# cars_cached.empty_db
#
# add_cars cars_semi, db_size
# x.report("(semi: uncached db but cached partition) searching with a partition") do
# corvet = semi_searchby_color.get car_color_to_search
# end
# cars_semi.empty_db
#
# add_cars cars_uncached, db_size
# x.report("(uncached db and partition) searching with a partition") do
# corvet = uncached_searchby_color.get car_color_to_search
# end
# cars_uncached.empty_db
# end
#
# # Searching for data with a tag.
# car_keyword_to_search = ENV["CARKEYWORD"] rescue "spacious"
# puts ""
# puts "search by tag #{car_keyword_to_search}: get #{ram_searchby_keywords.get(car_keyword_to_search).size} values"
# Benchmark.ips do |x|
# add_cars cars_ram, db_size
# x.report("(ram db and tag) searching with a tag") do
# corvet = ram_searchby_keywords.get car_keyword_to_search
# end
# cars_ram.empty_db
#
# add_cars cars_cached, db_size
# x.report("(cached db and tag) searching with a tag") do
# corvet = cached_searchby_keywords.get car_keyword_to_search
# end
# cars_cached.empty_db
#
# add_cars cars_semi, db_size
# x.report("(semi: uncached db but cached tag) searching with a tag") do
# corvet = semi_searchby_keywords.get car_keyword_to_search
# end
# cars_semi.empty_db
#
# add_cars cars_uncached, db_size
# x.report("(uncached db and tag) searching with a tag") do
# corvet = uncached_searchby_keywords.get car_keyword_to_search
# end
# cars_uncached.empty_db
# end
cars_ram.rm_storage_dir
cars_cached.rm_storage_dir
cars_semi.rm_storage_dir
cars_uncached.rm_storage_dir
end
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
def batch_add(nb_run)
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
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
end
ENV["REPORT_DIR"]?.try do |report_dir|
Context.report_dir = report_dir
end
Dir.mkdir_p Context.report_dir
nb_run = 1000
db_size_max = ENV["DBSIZE"].to_i rescue 1_000
current_size = ENV["DBSIZE_START"].to_i rescue 5_000
size_increment = ENV["DBSIZE_INCREMENT"].to_i rescue 5_000
while current_size <= db_size_max
batch current_size, nb_run
current_size += size_increment
end
batch_add nb_run