dodb.cr/spec/benchmark-todo.cr

82 lines
1.3 KiB
Crystal
Raw Permalink Normal View History

require "benchmark"
require "./db-ships.cr"
2024-05-22 18:29:08 +02:00
class DODBCached < DODB::Storage::Cached(Ship)
def initialize(storage_ext = "", remove_previous_data = true)
storage_dir = "test-storage#{storage_ext}"
if remove_previous_data
::FileUtils.rm_rf storage_dir
end
super storage_dir
end
end
2024-05-27 21:42:20 +02:00
class DODBUnCached < DODB::Storage::Uncached(Ship)
def initialize(storage_ext = "", remove_previous_data = true)
storage_dir = "test-storage#{storage_ext}"
if remove_previous_data
::FileUtils.rm_rf storage_dir
end
super storage_dir
end
end
cached = DODBCached.new
uncached = DODBUnCached.new
Benchmark.ips do |x|
x.report("adding values with a cache") do
Ship.all_ships.each do |ship|
cached << ship
end
end
x.report("adding values without cache") do
Ship.all_ships.each do |ship|
uncached << ship
end
end
end
cached = DODBCached.new
uncached = DODBUnCached.new
Ship.all_ships.each do |ship|
cached << ship
uncached << ship
end
Benchmark.ips do |x|
x.report("to_a with a cache") do
cached.to_a
end
x.report("to_a without cache") do
uncached.to_a
end
end
Benchmark.ips do |x|
x.report("to_h with a cache") do
cached.to_h
end
x.report("to_h without cache") do
uncached.to_h
end
end
Benchmark.ips do |x|
x.report("[0] with a cache") do
cached[0]
end
x.report("[0] without cache") do
uncached[0]
end
end