Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 288 lines (259 sloc) 7.196 kb
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 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287
#!/usr/bin/env ruby -wW1
# encoding: UTF-8

$: << File.join(File.dirname(__FILE__), "../lib")
$: << File.join(File.dirname(__FILE__), "../ext")

require 'optparse'
require 'yajl'
require 'json'
require 'json/pure'
require 'json/ext'
require 'msgpack'
require 'oj'
require 'ox'

class Jazz
  def initialize()
    @boolean = true
    @number = 58
    @string = "A string"
    @array = [true, false, nil]
    @hash = { 'one' => 1, 'two' => 2 }
  end
  def to_json()
    %{
{ "boolean":#{@boolean},
"number":#{@number},
"string":#{@string},
"array":#{@array},
"hash":#{@hash},
}
}
  end
  def to_hash()
    { 'boolean' => @boolean,
      'number' => @number,
      'string' => @string,
      'array' => @array,
      'hash' => @hash,
    }
  end
  def to_msgpack(out)
    out << MessagePack.pack(to_hash())
  end
end

$indent = 2
$iter = 10000
$with_object = true
$with_bignum = true
$with_nums = true

opts = OptionParser.new
opts.on("-c", "--count [Int]", Integer, "iterations") { |i| $iter = i }
opts.on("-i", "--indent [Int]", Integer, "indentation") { |i| $indent = i }
opts.on("-o", "without objects") { $with_object = false }
opts.on("-b", "without bignum") { $with_bignum = false }
opts.on("-n", "without numbers") { $with_nums = false }
opts.on("-h", "--help", "Show this display") { puts opts; Process.exit!(0) }
files = opts.parse(ARGV)

if $with_nums
  obj = {
    'a' => 'Alpha',
    'b' => true,
    'c' => 12345,
    'd' => [ true, [false, [12345, nil], 3.967, ['something', false], nil]],
    'e' => { 'one' => 1, 'two' => 2 },
    'f' => nil,
  }
  obj['g'] = Jazz.new() if $with_object
  obj['h'] = 12345678901234567890123456789 if $with_bignum
else
  obj = {
    'a' => 'Alpha',
    'b' => true,
    'c' => '12345',
    'd' => [ true, [false, ['12345', nil], '3.967', ['something', false], nil]],
    'e' => { 'one' => '1', 'two' => '2' },
    'f' => nil,
  }
end

Oj.default_options = { :indent => $indent, :mode => :object }

s = Oj.dump(obj)

xml = Ox.dump(obj, :indent => $indent)

puts

# Put Oj in strict mode so it only create JSON native types instead of the
# original Ruby Objects. None of the other packages other than Ox support
# Object recreation so no need for Oj to do it in the performance tests.
Oj.default_options = { :mode => :strict }
parse_results = { :oj => 0.0, :yajl => 0.0, :msgpack => 0.0, :pure => 0.0, :ext => 0.0, :ox => 0.0 }

start = Time.now
$iter.times do
  Oj.load(s)
end
dt = Time.now - start
base_dt = dt
parse_results[:oj] = dt
puts "%d Oj.load()s in %0.3f seconds or %0.1f loads/msec" % [$iter, dt, $iter/dt/1000.0]

start = Time.now
$iter.times do
  Yajl::Parser.parse(s)
end
dt = Time.now - start
if base_dt < dt
  base_dt = dt
  base_name = 'Yajl'
end
parse_results[:yajl] = dt
puts "%d Yajl::Parser.parse()s in %0.3f seconds or %0.1f parses/msec" % [$iter, dt, $iter/dt/1000.0]

begin
  JSON.parser = JSON::Ext::Parser
  start = Time.now
  $iter.times do
    JSON.parse(s)
  end
  dt = Time.now - start
  if base_dt < dt
    base_dt = dt
    base_name = 'JSON::Ext'
  end
  parse_results[:ext] = dt
  puts "%d JSON::Ext::Parser parse()s in %0.3f seconds or %0.1f parses/msec" % [$iter, dt, $iter/dt/1000.0]
rescue Exception => e
  puts "JSON::Ext failed: #{e.class}: #{e.message}"
end

begin
  JSON.parser = JSON::Pure::Parser
  start = Time.now
  $iter.times do
    JSON.parse(s)
  end
  dt = Time.now - start
  if base_dt < dt
    base_dt = dt
    base_name = 'JSON::Pure'
  end
  parse_results[:pure] = dt
  puts "%d JSON::Pure::Parser parse()s in %0.3f seconds or %0.1f parses/msec" % [$iter, dt, $iter/dt/1000.0]
rescue Exception => e
  puts "JSON::Pure failed: #{e.class}: #{e.message}"
end

begin
  mp = MessagePack.pack(obj)
  start = Time.now
  $iter.times do
    MessagePack.unpack(mp)
  end
  dt = Time.now - start
  if base_dt < dt
    base_dt = dt
    base_name = 'MessagePack'
  end
  parse_results[:msgpack] = dt
  puts "%d MessagePack.unpack()s in %0.3f seconds or %0.1f packs/msec" % [$iter, dt, $iter/dt/1000.0]
rescue Exception => e
  puts "MessagePack failed: #{e.class}: #{e.message}"
end

start = Time.now
$iter.times do
  Ox.load(xml)
end
dt = Time.now - start
parse_results[:ox] = dt
puts "%d Ox.load()s in %0.3f seconds or %0.1f loads/msec" % [$iter, dt, $iter/dt/1000.0]

puts "Parser results:"
puts "gem seconds parses/msec X faster than #{base_name} (higher is better)"
parse_results.each do |name,dt|
  if 0.0 == dt
    puts "#{name} failed to generate JSON"
    next
  end
  puts "%-7s %6.3f %5.1f %4.1f" % [name, dt, $iter/dt/1000.0, base_dt/dt]
end

puts

# Back to object mode for best performance when dumping.
Oj.default_options = { :indent => $indent, :mode => :object }
dump_results = { :oj => 0.0, :yajl => 0.0, :msgpack => 0.0, :pure => 0.0, :ext => 0.0, :ox => 0.0 }

start = Time.now
$iter.times do
  Oj.dump(obj)
end
dt = Time.now - start
base_dt = dt
base_name = 'Oj'
parse_results[:oj] = dt
puts "%d Oj.dump()s in %0.3f seconds or %0.1f dumps/msec" % [$iter, dt, $iter/dt/1000.0]

start = Time.now
$iter.times do
  Yajl::Encoder.encode(obj)
end
dt = Time.now - start
if base_dt < dt
  base_dt = dt
  base_name = 'Yajl'
end
parse_results[:yajl] = dt
puts "%d Yajl::Encoder.encode()s in %0.3f seconds or %0.1f encodes/msec" % [$iter, dt, $iter/dt/1000.0]

begin
  JSON.parser = JSON::Ext::Parser
  start = Time.now
  $iter.times do
    JSON.generate(obj)
  end
  dt = Time.now - start
  if base_dt < dt
    base_dt = dt
    base_name = 'JSON::Ext'
  end
  parse_results[:pure] = dt
  puts "%d JSON::Ext generate()s in %0.3f seconds or %0.1f generates/msec" % [$iter, dt, $iter/dt/1000.0]
rescue Exception => e
  parse_results[:ext] = 0.0
  puts "JSON::Ext failed: #{e.class}: #{e.message}"
end

begin
  JSON.parser = JSON::Pure::Parser
  start = Time.now
  $iter.times do
    JSON.generate(obj)
  end
  dt = Time.now - start
  if base_dt < dt
    base_dt = dt
    base_name = 'JSON::Pure'
  end
  parse_results[:pure] = dt
  puts "%d JSON::Pure generate()s in %0.3f seconds or %0.1f generates/msec" % [$iter, dt, $iter/dt/1000.0]
rescue Exception => e
  parse_results[:pure] = 0.0
  puts "JSON::Pure failed: #{e.class}: #{e.message}"
end

begin
  start = Time.now
  $iter.times do
    MessagePack.pack(obj)
  end
  dt = Time.now - start
  if base_dt < dt
    base_dt = dt
    base_name = 'MessagePack'
  end
  parse_results[:msgpack] = dt
  puts "%d Msgpack()s in %0.3f seconds or %0.1f unpacks/msec" % [$iter, dt, $iter/dt/1000.0]
rescue Exception => e
  parse_results[:msgpack] = 0.0
  puts "MessagePack failed: #{e.class}: #{e.message}"
end

start = Time.now
$iter.times do
  Ox.dump(obj)
end
dt = Time.now - start
parse_results[:ox] = dt
puts "%d Ox.dump()s in %0.3f seconds or %0.1f dumps/msec" % [$iter, dt, $iter/dt/1000.0]

puts "Parser results:"
puts "gem seconds dumps/msec X faster than #{base_name} (higher is better)"
parse_results.each do |name,dt|
  if 0.0 == dt
    puts "#{name} failed to generate JSON"
    next
  end
  puts "%-7s %6.3f %5.1f %4.1f" % [name, dt, $iter/dt/1000.0, base_dt/dt]
end

puts
Something went wrong with that request. Please try again.