Project

General

Profile

Bug #2438 ยป marshal_benchmark.rb

rpenney (Russell Penney), 12/04/2009 10:51 PM

 
require 'tempfile'
require 'fileutils'
require 'yaml'

MIN_COUNT = 1_000
MAX_COUNT = 70_000
HEADER = "name size size/obj time"
FORMAT = "%-20s %-8i %-8i %-8.3f"

def benchmark(name, size, count)
start_time = Time.now
yield
end_time = Time.now

puts FORMAT % [name, size, count, end_time - start_time]
end

class CustomClass
def initialize
@value = rand
end
end

STDOUT.sync = true
count = MIN_COUNT
factor = 5
while count <= MAX_COUNT
GC.enable
GC.start
GC.disable
symbol_generator = "this_is_a_symbol"
string_generator = "this_is_a_string"
integer = 0
objects = (1..count).inject(Array.new) do |objs, _|
objs << CustomClass.new
symbol_generator = symbol_generator.next
objs << symbol_generator.to_sym
objs << (integer += 1)
string_generator = string_generator.next
objs << string_generator
end

puts "=========== #{count}"
benchmark("separated write", objects.size, count) do
open("seperated_#{count}.out", "w") do |io|
objects.each do |obj|
Marshal.dump(obj, io)
end
end
end
benchmark("separated read", objects.size, count) do
open("seperated_#{count}.out", "r") do |io|
begin
while true
obj = Marshal.load(io)
end
rescue EOFError
end
end
end
benchmark("array write", objects.size, count) do
open("array_#{count}.out", "w") do |io|
Marshal.dump(objects, io)
end
end
benchmark("array read", objects.size, count) do
open("array_#{count}.out", "r") do |io|
obj = Marshal.load(io)
end
end
benchmark("yaml write", objects.size, count) do
open("yaml_#{count}.out", "w") do |io|
YAML.dump(objects, io)
end
end
benchmark("yaml read", objects.size, count) do
open("yaml_#{count}.out", "r") do |io|
obj = YAML.load(io)
end
end
factor += 10
count = MIN_COUNT * factor
end

    (1-1/1)