Permalink
Browse files

added tests

  • Loading branch information...
1 parent 57afd74 commit 924f2067b83ecbaa26aff5ab82784371e3309bfe Peter Ohler committed Feb 20, 2012
Showing with 146 additions and 13 deletions.
  1. +9 −1 ext/oj/dump.c
  2. +0 −1 oj.gemspec
  3. +99 −10 test/perf_simple.rb
  4. +37 −0 test/sample.rb
  5. +1 −1 test/simple.rb
View
@@ -84,6 +84,7 @@ static void dump_cstr(const char *str, int cnt, Out out);
static void dump_hex(u_char c, Out out);
static void dump_str(VALUE obj, Out out);
static void dump_sym(VALUE obj, Out out);
+static void dump_class(VALUE obj, Out out);
static void dump_array(VALUE obj, int depth, Out out);
static void dump_hash(VALUE obj, int depth, Out out);
@@ -336,6 +337,13 @@ dump_sym(VALUE obj, Out out) {
}
static void
+dump_class(VALUE obj, Out out) {
+ const char *s = rb_class2name(obj);
+
+ dump_cstr(s, (int)strlen(s), out);
+}
+
+static void
dump_array(VALUE a, int depth, Out out) {
VALUE *np = RARRAY_PTR(a);
size_t size = 2;
@@ -424,9 +432,9 @@ dump_val(VALUE obj, int depth, Out out) {
case T_SYMBOL: dump_sym(obj, out); break;
case T_ARRAY: dump_array(obj, depth, out); break;
case T_HASH: dump_hash(obj, depth, out); break;
+ case T_CLASS: dump_class(obj, out); break;
case T_OBJECT:
case T_REGEXP:
- case T_CLASS:
case T_DATA: // for Time
// TBD
rb_raise(rb_eNotImpError, "Failed to dump '%s' Object (%02x)\n",
View
@@ -12,7 +12,6 @@ Gem::Specification.new do |s|
s.summary = "A fast JSON parser and serializer."
s.description = %{The fastest JSON parser and object serializer. }
-# s.files = Dir["{lib,ext,test}/**/*.{rb,h,c}"] + ['LICENSE', 'README.md']
s.files = Dir["{lib,ext,test}/**/*.{rb,h,c}"] + ['LICENSE', 'README.md']
s.extensions = ["ext/oj/extconf.rb"]
View
@@ -7,6 +7,9 @@
#require 'test/unit'
require 'optparse'
require 'yajl'
+require 'json'
+require 'json/pure'
+require 'json/ext'
require 'oj'
$indent = 2
@@ -22,44 +25,130 @@
"attr2": { "one": 1 }
}
}
+#s = File.read('sample.json')
Oj.default_options = { :indent => 0 }
puts
+parse_results = { :oj => 0.0, :yajl => 0.0, :pure => 0.0, :ext => 0.0 }
+
start = Time.now
iter.times do
Oj.load(s)
end
-oj_dt = Time.now - start
-puts "%d Oj.load()s in %0.3f seconds or %0.1f loads/msec" % [iter, oj_dt, iter/oj_dt/1000.0]
+dt = Time.now - start
+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
-yajl_dt = Time.now - start
-puts "%d Yajl::Parser.parse()s in %0.3f seconds or %0.1f parses/msec" % [iter, yajl_dt, iter/yajl_dt/1000.0]
+dt = Time.now - start
+base = dt
+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
+ base = dt
+ base_name = 'JSON::Ext'
+ 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
-puts "Oj is %0.1f times faster than YAJL at parsing." % [yajl_dt / oj_dt]
+begin
+ JSON.parser = JSON::Pure::Parser
+ start = Time.now
+ iter.times do
+ JSON.parse(s)
+ end
+ dt = Time.now - start
+ base = dt
+ base_name = 'JSON::Pure'
+ 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
+
+puts "Parser results:"
+puts "gem seconds parses/msec X faster than #{base_name}"
+parse_results.each do |name,dt|
+ puts "%-4s %6.3f %5.1f %4.1f" % [name, dt, iter/dt/1000.0, base/dt]
+end
puts
+dump_results = { :oj => 0.0, :yajl => 0.0, :pure => 0.0, :ext => 0.0 }
obj = Oj.load(s)
+
start = Time.now
iter.times do
Oj.dump(obj)
end
-oj_dt = Time.now - start
-puts "%d Oj.dump()s in %0.3f seconds or %0.1f dumps/msec" % [iter, oj_dt, iter/oj_dt/1000.0]
+dt = Time.now - start
+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
-yajl_dt = Time.now - start
-puts "%d Yajl::Encoder.encode()s in %0.3f seconds or %0.1f encodes/msec" % [iter, yajl_dt, iter/yajl_dt/1000.0]
+dt = Time.now - start
+base = dt
+base_name = 'YAJL'
+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
+ base = dt
+ base_name = 'JSON::Ext'
+ 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
-puts "Oj is %0.1f times faster than YAJL at dumping." % [yajl_dt / oj_dt]
+begin
+ JSON.parser = JSON::Pure::Parser
+ start = Time.now
+ iter.times do
+ JSON.generate(obj)
+ end
+ dt = Time.now - start
+ base = dt
+ base_name = 'JSON::Pure'
+ 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
+
+puts "Parser results:"
+puts "gem seconds dumps/msec X faster than #{base_name}"
+parse_results.each do |name,dt|
+ if 0.0 == dt
+ puts "#{name} failed to generate JSON"
+ next
+ end
+ puts "%-4s %6.3f %5.1f %4.1f" % [name, dt, iter/dt/1000.0, base/dt]
+end
puts
View
@@ -0,0 +1,37 @@
+#!/usr/bin/env ruby -wW2
+
+if $0 == __FILE__
+ $: << '.'
+ $: << '..'
+ $: << '../lib'
+ $: << '../ext'
+end
+
+require 'pp'
+require 'oj'
+
+def sample_json(size=3)
+ colors = [ :black, :gray, :white, :red, :blue, :yellow, :green, :purple, :orange ]
+ container = []
+ size.times do |i|
+ box = {
+ 'color' => colors[i % colors.size],
+ 'fragile' => (0 == (i % 2)),
+ 'width' => i,
+ 'height' => i,
+ 'depth' => i,
+ 'weight' => i * 1.3,
+ 'address' => {
+ 'street' => "#{i} Main Street",
+ 'city' => 'Sity',
+ 'state' => nil
+ }
+ }
+ container << box
+ end
+ container
+end
+
+if $0 == __FILE__
+ File.open('sample.json', "w") { |f| f.write(Oj.dump(sample_json(3), :indent => 2)) }
+end
View
@@ -62,7 +62,7 @@ def test_fixnum
def test_float
dump_and_load(0.0, false)
dump_and_load(12345.6789, false)
- dump_and_load(-54321.012, true)
+ dump_and_load(-54321.012, false)
dump_and_load(2.48e16, false)
end

0 comments on commit 924f206

Please sign in to comment.