require "benchmark" require "./utilities.cr" require "../src/lru.cr" def add(lru : LRU(Int32) | EfficientLRU(Int32), nb : UInt32) i = 0 while i < nb lru << i i += 1 end end def report_add(lru : LRU(Int32) | EfficientLRU(Int32), nb : UInt32, fname : String) File.open("#{Context.report_dir}/#{fname}.raw", "w") do |file| i = 0 while i < nb elapsed_time = perform_something { lru << i } i += 1 file.puts "#{i} #{elapsed_time.total_nanoseconds}" end end end class Context class_property nb_values : UInt32 = 100_000 class_property lru_size : UInt32 = 10_000 class_property report_dir = "results" end if nb_values = ENV["NBVAL"]? Context.nb_values = nb_values.to_u32 end if lru_size = ENV["LRUSIZE"]? Context.lru_size = lru_size.to_u32 end if ARGV.size > 0 puts "Usage: benchmark-lru" puts "" puts "envvar: REPORT_DIR= where to put the results" puts "envvar: REPORT_EACH_ADD= to report the duration of each addition of a value in the structure" puts "envvar: NBVAL= (default: 100_000) nb of values to add to the structure" puts "envvar: LRUSIZE= (default: 10_000) max number of values in the structure" exit 0 end ENV["REPORT_DIR"]?.try { |report_dir| Context.report_dir = report_dir } Dir.mkdir_p Context.report_dir if ENV["REPORT_EACH_ADD"]? LRU(Int32).new(Context.lru_size).tap do |lru| report_add lru, Context.nb_values, "lru_#{Context.lru_size}_#{Context.nb_values}" end EfficientLRU(Int32).new(Context.lru_size).tap do |lru| report_add lru, Context.nb_values, "efficientlru_#{Context.lru_size}_#{Context.nb_values}" end else Benchmark.ips do |x| x.report("adding #{Context.nb_values} values, LRU limited to #{Context.lru_size}") do lru = LRU(Int32).new Context.lru_size add lru, Context.nb_values end x.report("adding #{Context.nb_values} values, EfficientLRU limited to #{Context.lru_size}") do lru = EfficientLRU(Int32).new Context.lru_size add lru, Context.nb_values end end end