Class: FFI_Yajl::Benchmark::Encode

Inherits:
Object
  • Object
show all
Defined in:
lib/ffi_yajl/benchmark/encode.rb

Instance Method Summary collapse

Instance Method Details

#runObject



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/ffi_yajl/benchmark/encode.rb', line 31

def run
  # filename = ARGV[0] || 'benchmark/subjects/ohai.json'
  filename = File.expand_path(File.join(File.dirname(__FILE__), "subjects", "ohai.json"))
  hash = File.open(filename, "rb") { |f| FFI_Yajl::Parser.parse(f.read) }

  times = ARGV[1] ? ARGV[1].to_i : 1000
  puts "Starting benchmark encoding #{filename} #{times} times\n\n"
  ::Benchmark.bmbm do |x|
    x.report("FFI_Yajl::Encoder.encode (to a String)") do
      times.times { FFI_Yajl::Encoder.encode(hash) }
    end

    ffi_string_encoder = FFI_Yajl::Encoder.new
    x.report("FFI_Yajl::Encoder#encode (to a String)") do
      times.times { ffi_string_encoder.encode(hash) }
    end

    if defined?(Oj)
      x.report("Oj.dump (to a String)") do
        times.times { Oj.dump(hash) }
      end
    end

    if defined?(Yajl::Encoder)
      x.report("Yajl::Encoder.encode (to a String)") do
        times.times { Yajl::Encoder.encode(hash) }
      end

      io_encoder = Yajl::Encoder.new
      x.report("Yajl::Encoder#encode (to an IO)") do
        times.times { io_encoder.encode(hash, StringIO.new) }
      end

      string_encoder = Yajl::Encoder.new
      x.report("Yajl::Encoder#encode (to a String)") do
        times.times { string_encoder.encode(hash) }
      end
    end

    if defined?(JSON)
      x.report("JSON.generate") do
        times.times { JSON.generate(hash) }
      end
      x.report("JSON.fast_generate") do
        times.times { JSON.fast_generate(hash) }
      end
    end
  end
end