require "benchmark" require "./utilities.cr" require "../src/fifo.cr" def add(fifo : FIFO(Int32) | EfficientFIFO(Int32), nb : UInt32) i = 0 while i < nb fifo << i i += 1 end end def report_add(fifo : FIFO(Int32) | EfficientFIFO(Int32), nb : UInt32, fname : String) File.open("#{Context.report_dir}/#{fname}.raw", "w") do |file| i = 0 while i < nb elapsed_time = perform_something { fifo << i } i += 1 file.puts "#{i} #{elapsed_time.total_nanoseconds}" end end end class Context class_property nb_values : UInt32 = 100_000 class_property fifo_size : UInt32 = 10_000 class_property report_dir = "results" end if nb_values = ENV["NBVAL"]? Context.nb_values = nb_values.to_u32 end if fifo_size = ENV["FIFOSIZE"]? Context.fifo_size = fifo_size.to_u32 end if ARGV.size > 0 puts "Usage: benchmark-fifo" 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: FIFOSIZE= (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"]? FIFO(Int32).new(Context.fifo_size).tap do |fifo| report_add fifo, Context.nb_values, "fifo_#{Context.fifo_size}_#{Context.nb_values}" end EfficientFIFO(Int32).new(Context.fifo_size).tap do |fifo| report_add fifo, Context.nb_values, "efficientfifo_#{Context.fifo_size}_#{Context.nb_values}" end else Benchmark.ips do |x| x.report("adding #{Context.nb_values} values, FIFO limited to #{Context.fifo_size}") do fifo = FIFO(Int32).new Context.fifo_size add fifo, Context.nb_values end x.report("adding #{Context.nb_values} values, EfficientFIFO limited to #{Context.fifo_size}") do fifo = EfficientFIFO(Int32).new Context.fifo_size add fifo, Context.nb_values end end end