ffi-yajl 2.2.0 → 2.2.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,17 +1,12 @@
1
1
  require 'rubygems'
2
2
  require 'benchmark'
3
- require 'yaml'
4
3
  require 'yajl'
5
4
  require 'ffi_yajl'
6
5
  if !defined?(RUBY_ENGINE) || RUBY_ENGINE !~ /jruby/
7
- if ENV['FORCE_FFI_YAJL'] != 'ext'
8
- begin
9
- require 'yajl'
10
- rescue Exception
11
- puts "INFO: yajl-ruby not installed"
12
- end
13
- else
14
- puts "INFO: skipping yajl-ruby because we're using the C extension"
6
+ begin
7
+ require 'yajl'
8
+ rescue LoadError
9
+ puts "INFO: yajl-ruby not installed"
15
10
  end
16
11
  else
17
12
  puts "INFO: skipping yajl-ruby on jruby"
@@ -20,127 +15,79 @@ begin
20
15
  require 'json'
21
16
  rescue LoadError
22
17
  end
23
- begin
24
- require 'psych'
25
- rescue LoadError
26
- end
27
- begin
28
- require 'active_support'
29
- rescue LoadError
30
- end
31
18
  begin
32
19
  require 'oj'
33
20
  rescue LoadError
34
21
  end
35
22
 
36
- class FFI_Yajl::Benchmark::Parse
37
-
38
- def run
39
- filename = File.expand_path(File.join(File.dirname(__FILE__), "subjects", "item.json"))
40
- json = File.new(filename, 'r')
41
- json_str = json.read
23
+ module FFI_Yajl
24
+ class Benchmark
25
+ class Parse
26
+ def run
27
+ filename = File.expand_path(File.join(File.dirname(__FILE__), "subjects", "item.json"))
28
+ json = File.new(filename, 'r')
29
+ json_str = json.read
42
30
 
43
- times = ARGV[1] ? ARGV[1].to_i : 10_000
44
- puts "Starting benchmark parsing #{File.size(filename)} bytes of JSON data #{times} times\n\n"
45
- Benchmark.bmbm { |x|
46
- x.report {
47
- puts "FFI_Yajl::Parser.parse (from a String)"
48
- times.times {
49
- FFI_Yajl::Parser.parse(json_str)
50
- }
51
- }
52
- # ffi_parser = FFI_Yajl::Parser.new
53
- # x.report {
54
- # puts "FFI_Yajl::Parser#parse (from a String)"
55
- # times.times {
56
- # json.rewind
57
- # ffi_parser.parse(json.read)
58
- # }
59
- # }
60
- if defined?(Yajl::Parser)
61
- x.report {
62
- puts "Yajl::Parser.parse (from a String)"
63
- times.times {
64
- Yajl::Parser.parse(json_str)
65
- }
66
- }
67
- io_parser = Yajl::Parser.new
68
- io_parser.on_parse_complete = lambda {|obj|} if times > 1
69
- x.report {
70
- puts "Yajl::Parser#parse (from an IO)"
71
- times.times {
72
- json.rewind
73
- io_parser.parse(json)
74
- }
75
- }
76
- string_parser = Yajl::Parser.new
77
- string_parser.on_parse_complete = lambda {|obj|} if times > 1
78
- x.report {
79
- puts "Yajl::Parser#parse (from a String)"
80
- times.times {
81
- json.rewind
82
- string_parser.parse(json_str)
83
- }
84
- }
31
+ times = ARGV[1] ? ARGV[1].to_i : 10_000
32
+ puts "Starting benchmark parsing #{File.size(filename)} bytes of JSON data #{times} times\n\n"
33
+ ::Benchmark.bmbm do |x|
34
+ x.report do
35
+ puts "FFI_Yajl::Parser.parse (from a String)"
36
+ times.times { FFI_Yajl::Parser.parse(json_str) }
37
+ end
38
+ # ffi_parser = FFI_Yajl::Parser.new
39
+ # x.report {
40
+ # puts "FFI_Yajl::Parser#parse (from a String)"
41
+ # times.times {
42
+ # json.rewind
43
+ # ffi_parser.parse(json.read)
44
+ # }
45
+ # }
46
+ if defined?(Yajl::Parser)
47
+ x.report do
48
+ puts "Yajl::Parser.parse (from a String)"
49
+ times.times { Yajl::Parser.parse(json_str) }
50
+ end
51
+ io_parser = Yajl::Parser.new
52
+ io_parser.on_parse_complete = ->(obj) {} if times > 1
53
+ x.report do
54
+ puts "Yajl::Parser#parse (from an IO)"
55
+ times.times do
56
+ json.rewind
57
+ io_parser.parse(json)
58
+ end
59
+ end
60
+ string_parser = Yajl::Parser.new
61
+ string_parser.on_parse_complete = ->(obj) {} if times > 1
62
+ x.report do
63
+ puts "Yajl::Parser#parse (from a String)"
64
+ times.times do
65
+ json.rewind
66
+ string_parser.parse(json_str)
67
+ end
68
+ end
69
+ end
70
+ if defined?(Oj)
71
+ x.report do
72
+ puts "Oj.load"
73
+ times.times do
74
+ json.rewind
75
+ Oj.load(json.read)
76
+ end
77
+ end
78
+ end
79
+ if defined?(JSON)
80
+ x.report do
81
+ puts "JSON.parse"
82
+ times.times do
83
+ json.rewind
84
+ JSON.parse(json.read, max_nesting: false)
85
+ end
86
+ end
87
+ end
88
+ end
89
+ json.close
85
90
  end
86
- if defined?(Oj)
87
- x.report {
88
- puts "Oj.load"
89
- times.times {
90
- json.rewind
91
- Oj.load(json.read)
92
- }
93
- }
94
- end
95
- if defined?(JSON)
96
- x.report {
97
- puts "JSON.parse"
98
- times.times {
99
- json.rewind
100
- JSON.parse(json.read, :max_nesting => false)
101
- }
102
- }
103
- end
104
- if defined?(ActiveSupport::JSON)
105
- x.report {
106
- puts "ActiveSupport::JSON.decode"
107
- times.times {
108
- json.rewind
109
- ActiveSupport::JSON.decode(json.read)
110
- }
111
- }
112
- end
113
- x.report {
114
- puts "YAML.load (from an IO)"
115
- times.times {
116
- json.rewind
117
- YAML.load(json)
118
- }
119
- }
120
- x.report {
121
- puts "YAML.load (from a String)"
122
- times.times {
123
- YAML.load(json_str)
124
- }
125
- }
126
- if defined?(Psych)
127
- x.report {
128
- puts "Psych.load (from an IO)"
129
- times.times {
130
- json.rewind
131
- Psych.load(json)
132
- }
133
- }
134
- x.report {
135
- puts "Psych.load (from a String)"
136
- times.times {
137
- Psych.load(json_str)
138
- }
139
- }
140
- end
141
- }
142
- json.close
143
-
91
+ end
144
92
  end
145
93
  end
146
-
@@ -19,32 +19,32 @@ hash = {}
19
19
 
20
20
  times = ARGV[0] ? ARGV[0].to_i : 1000
21
21
  puts "Starting benchmark parsing #{File.size(filename)} bytes of JSON data #{times} times\n\n"
22
- Benchmark.bmbm { |x|
23
- x.report {
22
+ Benchmark.bmbm do |x|
23
+ x.report do
24
24
  puts "Yajl::Parser#parse"
25
25
  yajl = Yajl::Parser.new
26
- yajl.on_parse_complete = lambda {|obj|} if times > 1
27
- times.times {
26
+ yajl.on_parse_complete = ->(obj) {} if times > 1
27
+ times.times do
28
28
  json.rewind
29
29
  hash = yajl.parse(json)
30
- }
31
- }
30
+ end
31
+ end
32
32
  if defined?(JSON)
33
- x.report {
33
+ x.report do
34
34
  puts "JSON.parse"
35
- times.times {
35
+ times.times do
36
36
  json.rewind
37
- JSON.parse(json.read, :max_nesting => false)
38
- }
39
- }
37
+ JSON.parse(json.read, max_nesting: false)
38
+ end
39
+ end
40
40
  end
41
- x.report {
41
+ x.report do
42
42
  puts "Marshal.load"
43
- times.times {
43
+ times.times do
44
44
  marshal_file.rewind
45
45
  Marshal.load(marshal_file)
46
- }
47
- }
48
- }
46
+ end
47
+ end
48
+ end
49
49
  json.close
50
- marshal_file.close
50
+ marshal_file.close
@@ -16,26 +16,26 @@ json = File.new(filename, 'r')
16
16
 
17
17
  times = ARGV[0] ? ARGV[0].to_i : 1000
18
18
  puts "Starting benchmark parsing #{File.size(filename)} bytes of JSON data #{times} times\n\n"
19
- Benchmark.bmbm { |x|
19
+ Benchmark.bmbm do |x|
20
20
  parser = Yajl::Parser.new
21
- parser.on_parse_complete = lambda {|obj|} if times > 1
22
- x.report {
21
+ parser.on_parse_complete = ->(obj) {} if times > 1
22
+ x.report do
23
23
  puts "Yajl::Parser#parse"
24
- times.times {
24
+ times.times do
25
25
  json.rewind
26
26
  parser.parse(json)
27
- }
28
- }
27
+ end
28
+ end
29
29
  if defined?(JSON)
30
- x.report {
30
+ x.report do
31
31
  puts "JSON.parse"
32
- times.times {
32
+ times.times do
33
33
  json.rewind
34
- JSON.parse(json.read, :max_nesting => false)
35
- }
36
- }
34
+ JSON.parse(json.read, max_nesting: false)
35
+ end
36
+ end
37
37
  end
38
- }
38
+ end
39
39
  json.close
40
40
 
41
41
  # YAML section
@@ -43,13 +43,13 @@ filename = 'benchmark/subjects/ohai.yml'
43
43
  yaml = File.new(filename, 'r')
44
44
 
45
45
  puts "Starting benchmark parsing #{File.size(filename)} bytes of YAML data #{times} times\n\n"
46
- Benchmark.bmbm { |x|
47
- x.report {
46
+ Benchmark.bmbm do |x|
47
+ x.report do
48
48
  puts "YAML.load_stream"
49
- times.times {
49
+ times.times do
50
50
  yaml.rewind
51
51
  YAML.load(yaml)
52
- }
53
- }
54
- }
55
- yaml.close
52
+ end
53
+ end
54
+ end
55
+ yaml.close
@@ -5,7 +5,7 @@ require 'rubygems'
5
5
  require 'ffi_yajl'
6
6
  begin
7
7
  require 'perftools'
8
- rescue Exception
8
+ rescue LoadError
9
9
  puts "INFO: perftools.rb gem not installed"
10
10
  end
11
11
 
@@ -14,24 +14,20 @@ ENV['CPUPROFILE_FREQUENCY'] = "4000"
14
14
  module FFI_Yajl
15
15
  class Benchmark
16
16
  class ParseProfile
17
-
18
17
  def run
19
- if defined?(PerfTools)
20
- filename = File.expand_path(File.join(File.dirname(__FILE__), "subjects", "ohai.json"))
21
- json = File.new(filename, 'r').read
18
+ return if defined?(PerfTools)
19
+
20
+ filename = File.expand_path(File.join(File.dirname(__FILE__), "subjects", "ohai.json"))
21
+ json = File.new(filename, 'r').read
22
22
 
23
- times = 1000
24
- puts "Starting profiling encoding #{filename} #{times} times\n\n"
23
+ times = 1000
24
+ puts "Starting profiling encoding #{filename} #{times} times\n\n"
25
25
 
26
- PerfTools::CpuProfiler.start("/tmp/ffi_yajl_encode_profile.out") do
27
- times.times {
28
- output = FFI_Yajl::Parser.parse(json)
29
- }
30
- end
31
- system("pprof.rb --text /tmp/ffi_yajl_encode_profile.out")
26
+ PerfTools::CpuProfiler.start("/tmp/ffi_yajl_encode_profile.out") do
27
+ times.times { FFI_Yajl::Parser.parse(json) }
32
28
  end
29
+ system("pprof.rb --text /tmp/ffi_yajl_encode_profile.out")
33
30
  end
34
-
35
31
  end
36
32
  end
37
33
  end
@@ -7,33 +7,28 @@ require 'ffi_yajl'
7
7
  module FFI_Yajl
8
8
  class Benchmark
9
9
  class ParseProfileRubyProf
10
-
11
10
  def run
12
11
  begin
13
12
  require 'ruby-prof'
14
- rescue Exception
13
+ rescue LoadError
15
14
  puts "INFO: perftools.rb gem not installed"
16
15
  end
17
16
 
18
- if defined?(RubyProf)
19
- filename = File.expand_path(File.join(File.dirname(__FILE__), "subjects", "ohai.json"))
20
- json = File.new(filename, 'r').read
21
-
22
- times = 1000
23
- puts "Starting profiling encoding #{filename} #{times} times\n\n"
17
+ return if defined?(RubyProf)
24
18
 
25
- result = RubyProf.profile do
26
- times.times {
27
- output = FFI_Yajl::Parser.parse(json)
28
- }
29
- end
19
+ filename = File.expand_path(File.join(File.dirname(__FILE__), "subjects", "ohai.json"))
20
+ json = File.new(filename, 'r').read
30
21
 
31
- printer = RubyProf::GraphPrinter.new(result)
32
- printer.print(STDOUT, {})
22
+ times = 1000
23
+ puts "Starting profiling encoding #{filename} #{times} times\n\n"
33
24
 
25
+ result = RubyProf.profile do
26
+ times.times { FFI_Yajl::Parser.parse(json) }
34
27
  end
35
- end
36
28
 
29
+ printer = RubyProf::GraphPrinter.new(result)
30
+ printer.print(STDOUT, {})
31
+ end
37
32
  end
38
33
  end
39
34
  end
@@ -18,37 +18,37 @@ json = File.new(filename, 'r')
18
18
 
19
19
  times = ARGV[0] ? ARGV[0].to_i : 100
20
20
  puts "Starting benchmark parsing JSON stream (#{File.size(filename)} bytes of JSON data with 430 JSON separate strings) #{times} times\n\n"
21
- Benchmark.bmbm { |x|
21
+ Benchmark.bmbm do |x|
22
22
  parser = Yajl::Parser.new
23
- parser.on_parse_complete = lambda {|obj|}
24
- x.report {
23
+ parser.on_parse_complete = ->(obj) {}
24
+ x.report do
25
25
  puts "Yajl::Parser#parse"
26
- times.times {
26
+ times.times do
27
27
  json.rewind
28
28
  parser.parse(json)
29
- }
30
- }
29
+ end
30
+ end
31
31
  if defined?(JSON)
32
- x.report {
32
+ x.report do
33
33
  puts "JSON.parse"
34
- times.times {
34
+ times.times do
35
35
  json.rewind
36
36
  while chunk = json.gets
37
- JSON.parse(chunk, :max_nesting => false)
37
+ JSON.parse(chunk, max_nesting: false)
38
38
  end
39
- }
40
- }
39
+ end
40
+ end
41
41
  end
42
42
  if defined?(ActiveSupport::JSON)
43
- x.report {
43
+ x.report do
44
44
  puts "ActiveSupport::JSON.decode"
45
- times.times {
45
+ times.times do
46
46
  json.rewind
47
47
  while chunk = json.gets
48
48
  ActiveSupport::JSON.decode(chunk)
49
49
  end
50
- }
51
- }
50
+ end
51
+ end
52
52
  end
53
- }
54
- json.close
53
+ end
54
+ json.close
@@ -25,4 +25,3 @@ require 'ffi_yajl/benchmark/encode_profile.rb'
25
25
  require 'ffi_yajl/benchmark/parse.rb'
26
26
  require 'ffi_yajl/benchmark/parse_profile.rb'
27
27
  require 'ffi_yajl/benchmark/parse_profile_ruby_prof.rb'
28
-
@@ -41,7 +41,7 @@ module FFI_Yajl
41
41
  # call either the ext or ffi hook
42
42
  str = do_yajl_encode(obj, yajl_gen_opts, opts)
43
43
  # we can skip cleaning the whole string for utf-8 issues if we have yajl validate as we go
44
- str.encode!("utf-8", "binary", :undef => :replace) unless yajl_gen_opts[:yajl_gen_validate_utf8]
44
+ str.encode!("utf-8", "binary", undef: :replace) unless yajl_gen_opts[:yajl_gen_validate_utf8]
45
45
  str
46
46
  end
47
47
 
@@ -54,9 +54,9 @@ module FFI_Yajl
54
54
  @opts ||= {}
55
55
  end
56
56
 
57
- def self.raise_error_for_status(status, token=nil)
57
+ def self.raise_error_for_status(status, token = nil)
58
58
  # scrub token to valid utf-8 since we may be issuing an exception on an invalid utf-8 token
59
- token = token.to_s.encode("utf-8", "binary", :undef => :replace)
59
+ token = token.to_s.encode("utf-8", "binary", undef: :replace)
60
60
  case status
61
61
  when 1 # yajl_gen_keys_must_be_strings
62
62
  raise FFI_Yajl::EncodeError, "YAJL internal error: attempted use of non-string object as key"