summaryrefslogtreecommitdiff
path: root/lib/ffi_yajl/benchmark/encode_json_and_yaml.rb
blob: 6a145f2f0bc78c2e1c1403e7079d9e2cc2aff889 (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
$LOAD_PATH.unshift File.expand_path(File.dirname(__FILE__) + "/..")
$LOAD_PATH.unshift File.expand_path(File.dirname(__FILE__) + "/../lib")

require "rubygems" unless defined?(Gem)
require "benchmark" unless defined?(Benchmark)
require "yajl"
begin
  require "json" unless defined?(JSON)
rescue LoadError
end
require "yaml" unless defined?(YAML)

# JSON Section
filename = "benchmark/subjects/ohai.json"
json = File.new(filename, "r")
hash = Yajl::Parser.new.parse(json)
json.close

times = ARGV[0] ? ARGV[0].to_i : 1000
puts "Starting benchmark encoding #{filename} into JSON #{times} times\n\n"
Benchmark.bmbm do |x|
  encoder = Yajl::Encoder.new
  x.report do
    puts "Yajl::Encoder#encode"
    times.times { encoder.encode(hash, StringIO.new) }
  end
  if defined?(JSON)
    x.report do
      puts "JSON's #to_json"
      times.times { JSON.generate(hash) }
    end
  end
end

# YAML Section
filename = "benchmark/subjects/ohai.yml"
yml = File.new(filename, "r")
data = YAML.load_stream(yml)
yml.close

puts "Starting benchmark encoding #{filename} into YAML #{times} times\n\n"
Benchmark.bmbm do |x|
  x.report do
    puts "YAML.dump"
    times.times { YAML.dump(data, StringIO.new) }
  end
end