require 'yaml' require 'benchmark' module Jeka class Implementation < JekaHelper jeka_reader :language jeka_reader :source jeka_reader :comment attr_accessor :algorithm def initialize(folder) super(File.join(folder, "_implementation.yaml")) end def source_code f = File.open(File.join(@folder, source), "r") return f.readlines.join() end def build if @jeka["build"] command = @jeka["build"]["command"].gsub("$", @folder) system(command) end end def run(input, verbose) command = "#{@jeka["run"]["command"].gsub("$", @folder)} #{"< #{input}" if input}" if verbose system(command) else out = "" IO.popen(command) do |io| out = io.readlines end return out.join("") end end def benchmark(input, n) b = Benchmark.measure {run(input, false)}.to_a bench = Hash.new bench[:user_cpu_time] = b[1] bench[:system_cpu_time] = b[2] bench[:childrens_use_cpu_time] = b[3] bench[:childrens_system_cpu_time] = b[4] bench[:elapsed_real_time] = b[5] (n-1).times do b = Benchmark.measure {run(input, false)}.to_a bench[:user_cpu_time] += b[1] bench[:system_cpu_time] += b[2] bench[:childrens_use_cpu_time] += b[3] bench[:childrens_system_cpu_time] += b[4] bench[:elapsed_real_time] += b[5] end bench[:user_cpu_time] /= n bench[:system_cpu_time] /= n bench[:childrens_use_cpu_time] /= n bench[:childrens_system_cpu_time] /= n bench[:elapsed_real_time] /= n filename = File.join(@folder, File.basename(input).gsub(/\..*/, ".benchmark")) f = File.open(filename, "w") f.write(bench) f.close return bench end def benchmark_results benchmarks = [] n = 0 dir = Dir.new(@folder) dir.each do |d| if d.index(/^test_.*\.benchmark$/) f = File.open(File.join(@folder, d), "r") benchmarks << eval(f.readlines.join("")) n += 1 end end bench = Hash.new bench[:language] = language bench[:comment] = comment bench[:user_cpu_time] = 0 bench[:system_cpu_time] = 0 bench[:childrens_use_cpu_time] = 0 bench[:childrens_system_cpu_time] = 0 bench[:elapsed_real_time] = 0 benchmarks.each do |b| bench[:user_cpu_time] += b[:user_cpu_time] bench[:system_cpu_time] += b[:system_cpu_time] bench[:childrens_use_cpu_time] += b[:childrens_use_cpu_time] bench[:childrens_system_cpu_time] += b[:childrens_system_cpu_time] bench[:elapsed_real_time] += b[:elapsed_real_time] end bench[:user_cpu_time] /= n bench[:system_cpu_time] /= n bench[:childrens_use_cpu_time] /= n bench[:childrens_system_cpu_time] /= n bench[:elapsed_real_time] /= n return bench end end end