summaryrefslogtreecommitdiff
path: root/lib/ffi_yajl/benchmark/encode.rb
blob: 0a33a53a347bb0b64ea0a626e5d16245193a484d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# Portions Originally Copyright (c) 2008-2011 Brian Lopez - http://github.com/brianmario
# See MIT-LICENSE

require 'rubygems'
require 'benchmark'
require 'stringio'
if !defined?(RUBY_ENGINE) || RUBY_ENGINE !~ /jruby/
  begin
    require 'yajl'
  rescue LoadError
    puts "INFO: yajl-ruby not installed"
  end
else
  puts "INFO: skipping yajl-ruby on jruby"
end
require 'ffi_yajl'
begin
  require 'json'
rescue LoadError
  puts "INFO: json gem not installed"
end
begin
  require 'psych'
rescue LoadError
  puts "INFO: psych gem not installed"
end
begin
  require 'active_support'
rescue LoadError
  puts "INFO: active_support gem not installed"
end
begin
  require 'oj'
rescue LoadError
  puts "INFO: oj gem not installed"
end

module FFI_Yajl
  class Benchmark
    class Encode
      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 { |x|
          x.report("FFI_Yajl::Encoder.encode (to a String)") {
            times.times {
              output = FFI_Yajl::Encoder.encode(hash)
            }
          }

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

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

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

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

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

          if defined?(JSON)
            x.report("JSON.generate") {
              times.times {
                JSON.generate(hash)
              }
            }
            x.report("JSON.fast_generate") {
              times.times {
                JSON.fast_generate(hash)
              }
            }
          end
          if defined?(Psych)
            x.report("Psych.to_json") {
              times.times {
                Psych.to_json(hash)
              }
            }
            if defined?(Psych::JSON::Stream)
              x.report("Psych::JSON::Stream") {
                times.times {
                  io = StringIO.new
                  stream = Psych::JSON::Stream.new io
                  stream.start
                  stream.push hash
                  stream.finish
                }
              }
            end
          end
          #          if defined?(ActiveSupport::JSON)
          #            x.report("ActiveSupport::JSON.encode") {
          #              times.times {
          #                ActiveSupport::JSON.encode(hash)
          #              }
          #            }
          #          end
        }
      end
    end
  end
end