From 59ac17d21c225e2f6b7c0936afc7a3fc91f5fe0c Mon Sep 17 00:00:00 2001 From: Karchnu Date: Sat, 18 Jul 2020 11:24:31 +0200 Subject: [PATCH] Adding benchmark. --- spec/benchmark.cr | 83 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 83 insertions(+) create mode 100644 spec/benchmark.cr diff --git a/spec/benchmark.cr b/spec/benchmark.cr new file mode 100644 index 0000000..16dd521 --- /dev/null +++ b/spec/benchmark.cr @@ -0,0 +1,83 @@ +require "benchmark" + +require "../src/dodb.cr" +require "./test-data.cr" + +class DODBCached < DODB::CachedDataBase(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 + +class DODBUnCached < DODB::DataBase(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