Class: FFI_Yajl::Benchmark::Parse

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

Instance Method Summary collapse

Instance Method Details

#runObject



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
# File 'lib/ffi_yajl/benchmark/parse.rb', line 26

def run
  filename = File.expand_path(File.join(File.dirname(__FILE__), "subjects", "item.json"))
  json = File.new(filename, "r")
  json_str = json.read

  times = ARGV[1] ? ARGV[1].to_i : 10_000
  puts "Starting benchmark parsing #{File.size(filename)} bytes of JSON data #{times} times\n\n"
  ::Benchmark.bmbm do |x|
    x.report do
      puts "FFI_Yajl::Parser.parse (from a String)"
      times.times { FFI_Yajl::Parser.parse(json_str) }
    end
    #      ffi_parser = FFI_Yajl::Parser.new
    #      x.report {
    #        puts "FFI_Yajl::Parser#parse (from a String)"
    #        times.times {
    #          json.rewind
    #          ffi_parser.parse(json.read)
    #        }
    #      }
    if defined?(Yajl::Parser)
      x.report do
        puts "Yajl::Parser.parse (from a String)"
        times.times { Yajl::Parser.parse(json_str) }
      end
      io_parser = Yajl::Parser.new
      io_parser.on_parse_complete = ->(obj) {} if times > 1
      x.report do
        puts "Yajl::Parser#parse (from an IO)"
        times.times do
          json.rewind
          io_parser.parse(json)
        end
      end
      string_parser = Yajl::Parser.new
      string_parser.on_parse_complete = ->(obj) {} if times > 1
      x.report do
        puts "Yajl::Parser#parse (from a String)"
        times.times do
          json.rewind
          string_parser.parse(json_str)
        end
      end
    end
    if defined?(Oj)
      x.report do
        puts "Oj.load"
        times.times do
          json.rewind
          Oj.load(json.read)
        end
      end
    end
    if defined?(JSON)
      x.report do
        puts "JSON.parse"
        times.times do
          json.rewind
          JSON.parse(json.read, max_nesting: false)
        end
      end
    end
  end
  json.close
end