oj 2.17.3 → 2.17.4
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +3 -8
- data/ext/oj/oj.c +11 -0
- data/lib/oj/mimic.rb +38 -2
- data/lib/oj/version.rb +1 -1
- data/test/aaa.rb +21 -0
- data/test/bug.rb +50 -37
- data/test/pact.rb +21 -0
- data/test/perf1.rb +64 -0
- data/test/perf2.rb +76 -0
- data/test/perf_obj_old.rb +213 -0
- data/test/test_bigd.rb +63 -0
- data/test/{mod.rb → test_range.rb} +9 -6
- data/test/test_various.rb +1 -1
- metadata +61 -66
- checksums.yaml +0 -7
- data/test/bug2.rb +0 -10
- data/test/bug3.rb +0 -46
- data/test/bug_fast.rb +0 -32
- data/test/bug_load.rb +0 -24
- data/test/crash.rb +0 -111
- data/test/example.rb +0 -11
- data/test/io.rb +0 -48
- data/test/isolated/test_mimic_rails_datetime.rb +0 -27
- data/test/russian.rb +0 -18
- data/test/struct.rb +0 -29
- data/test/test_serializer.rb +0 -59
- data/test/write_timebars.rb +0 -31
data/README.md
CHANGED
@@ -170,15 +170,10 @@ Oj.default_options = {:mode => :compat }
|
|
170
170
|
|
171
171
|
## Releases
|
172
172
|
|
173
|
-
**Release 2.17.
|
173
|
+
**Release 2.17.4**
|
174
174
|
|
175
|
-
-
|
176
|
-
|
177
|
-
|
178
|
-
**Release 2.17.2**
|
179
|
-
|
180
|
-
- Worked around a problem with DateTime and ActiveSupport that causes a hang
|
181
|
-
when hour, minute, second, and some other methods are called from C.
|
175
|
+
- Added the ascii_only option to JSON.generate when in mimic_JSON mode so that
|
176
|
+
it is consistent with the undocumented feature in the json gem.
|
182
177
|
|
183
178
|
[Older release notes](http://www.ohler.com/dev/oj_misc/release_notes.html).
|
184
179
|
|
data/ext/oj/oj.c
CHANGED
@@ -1760,6 +1760,14 @@ mimic_generate_core(int argc, VALUE *argv, Options copts) {
|
|
1760
1760
|
copts->dump_opts.array_size = (uint8_t)len;
|
1761
1761
|
copts->dump_opts.use = true;
|
1762
1762
|
}
|
1763
|
+
if (Qnil != (v = rb_hash_lookup(ropts, ascii_only_sym))) {
|
1764
|
+
// generate seems to assume anything except nil and false are true.
|
1765
|
+
if (Qfalse == v || Qnil == v) {
|
1766
|
+
copts->escape_mode = JSONEsc;
|
1767
|
+
} else {
|
1768
|
+
copts->escape_mode = ASCIIEsc;
|
1769
|
+
}
|
1770
|
+
}
|
1763
1771
|
// :allow_nan is not supported as Oj always allows_nan
|
1764
1772
|
// :max_nesting is always set to 100
|
1765
1773
|
}
|
@@ -2379,6 +2387,9 @@ void Init_oj() {
|
|
2379
2387
|
* @param [String] :space_before String placed before a : delimiter
|
2380
2388
|
* @param [String] :object_nl String placed after a JSON object
|
2381
2389
|
* @param [String] :array_nl String placed after a JSON array
|
2390
|
+
* @param [true|false] :ascii_only if not nil or false then use only ascii
|
2391
|
+
* characters in the output. Note JSON.generate does
|
2392
|
+
* support this even if it is not documented.
|
2382
2393
|
*/
|
2383
2394
|
/* Document-method: fast_generate
|
2384
2395
|
* call-seq: fast_generate(obj, opts=nil) -> String
|
data/lib/oj/mimic.rb
CHANGED
@@ -7,6 +7,28 @@ end
|
|
7
7
|
|
8
8
|
module Oj
|
9
9
|
|
10
|
+
# A bit hack-ish but does the trick. The JSON.dump_default_options is a Hash
|
11
|
+
# but in mimic we use a C struct to store defaults. This class creates a view
|
12
|
+
# onto that struct.
|
13
|
+
class MimicDumpOption < Hash
|
14
|
+
def initialize()
|
15
|
+
oo = Oj.default_options
|
16
|
+
self.store(:max_nesting, false)
|
17
|
+
self.store(:allow_nan, true)
|
18
|
+
self.store(:quirks_mode, oo[:quirks_mode])
|
19
|
+
self.store(:ascii_only, (:ascii == oo[:escape_mode]))
|
20
|
+
end
|
21
|
+
|
22
|
+
def []=(key, value)
|
23
|
+
case key
|
24
|
+
when :quirks_mode
|
25
|
+
Oj.default_options = {:quirks_mode => value}
|
26
|
+
when :ascii_only
|
27
|
+
Oj.default_options = {:ascii_only => value}
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
10
32
|
def self.mimic_loaded(mimic_paths=[])
|
11
33
|
$LOAD_PATH.each do |d|
|
12
34
|
next unless File.exist?(d)
|
@@ -122,5 +144,19 @@ module Oj
|
|
122
144
|
end
|
123
145
|
end
|
124
146
|
|
125
|
-
|
126
|
-
|
147
|
+
JSON.module_eval do
|
148
|
+
def self.dump_default_options
|
149
|
+
Oj::MimicDumpOption.new
|
150
|
+
end
|
151
|
+
|
152
|
+
def self.dump_default_options=(h)
|
153
|
+
m = Oj::MimicDumpOption.new
|
154
|
+
h.each do |k,v|
|
155
|
+
m[k] = v
|
156
|
+
end
|
157
|
+
end
|
158
|
+
end
|
159
|
+
|
160
|
+
end # self.mimic_loaded
|
161
|
+
|
162
|
+
end # Oj
|
data/lib/oj/version.rb
CHANGED
data/test/aaa.rb
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
# encoding: UTF-8
|
3
|
+
|
4
|
+
$VERBOSE = true
|
5
|
+
|
6
|
+
%w(lib ext test).each do |dir|
|
7
|
+
$LOAD_PATH.unshift File.expand_path("../../#{dir}", __FILE__)
|
8
|
+
end
|
9
|
+
|
10
|
+
#require 'json'
|
11
|
+
require 'oj'
|
12
|
+
Oj.mimic_JSON
|
13
|
+
|
14
|
+
JSON.dump_default_options[:ascii_only]=true
|
15
|
+
puts JSON.dump_default_options
|
16
|
+
|
17
|
+
puts JSON.dump(["a😬sdf"])
|
18
|
+
|
19
|
+
JSON.dump_default_options = {:ascii_only=>false}
|
20
|
+
|
21
|
+
puts JSON.dump_default_options
|
data/test/bug.rb
CHANGED
@@ -5,47 +5,60 @@ $: << File.dirname(__FILE__)
|
|
5
5
|
|
6
6
|
require 'helper'
|
7
7
|
|
8
|
-
|
9
|
-
|
10
|
-
@state = []
|
11
|
-
end
|
8
|
+
require 'oj'
|
9
|
+
require 'securerandom'
|
12
10
|
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
end
|
17
|
-
|
18
|
-
def
|
19
|
-
|
20
|
-
end
|
21
|
-
|
22
|
-
def hash_set(h,k,v)
|
23
|
-
h.store(k,v)
|
24
|
-
end
|
25
|
-
|
26
|
-
def array_start
|
27
|
-
@state << []
|
28
|
-
@state.last
|
11
|
+
class Handler
|
12
|
+
def hash_start() {} end
|
13
|
+
def hash_set(h,k,v) h.store(k,v) end
|
14
|
+
def array_start() [] end
|
15
|
+
def array_append(a,v) a << v end
|
16
|
+
def error(message, line, column)
|
17
|
+
raise Exception.new(message, line, column)
|
29
18
|
end
|
19
|
+
end
|
30
20
|
|
21
|
+
json = Oj.dump({"this"=>"object"})
|
31
22
|
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
23
|
+
if true
|
24
|
+
name = "/tmp/#{SecureRandom.uuid}"
|
25
|
+
`mkfifo #{name}`
|
26
|
+
if fork
|
27
|
+
open(name, 'r+') do |read_io|
|
28
|
+
p "start reading #{read_io.stat.ftype}"
|
29
|
+
Oj.sc_parse(Handler.new, read_io) {|v| p v}
|
30
|
+
p "stop reading"
|
31
|
+
end
|
32
|
+
else
|
33
|
+
open(name, 'w+') do |write_io|
|
34
|
+
p "start writing #{write_io.stat.ftype} autoclose: #{write_io.autoclose?}"
|
35
|
+
write_io.write json
|
36
|
+
write_io.write json
|
37
|
+
p "stop writing"
|
38
|
+
end
|
39
|
+
sleep(1) # make it obvious that there are two threads
|
40
|
+
open(name, 'w+') do |write_io|
|
41
|
+
p "start writing #{write_io.stat.ftype}"
|
42
|
+
write_io.write json
|
43
|
+
write_io.write json
|
44
|
+
p "stop writing"
|
45
|
+
end
|
38
46
|
end
|
39
|
-
|
40
|
-
|
41
|
-
|
47
|
+
else
|
48
|
+
IO.pipe do |read_io, write_io|
|
49
|
+
if fork
|
50
|
+
write_io.close
|
51
|
+
p "start reading #{read_io.stat.ftype}"
|
52
|
+
Oj.sc_parse(Handler.new, read_io) {|v| p v}
|
53
|
+
p "stop reading"
|
54
|
+
read_io.close
|
55
|
+
else
|
56
|
+
read_io.close
|
57
|
+
p "start writing #{write_io.stat.ftype}"
|
58
|
+
write_io.write json
|
59
|
+
write_io.write json
|
60
|
+
p "stop writing"
|
61
|
+
write_io.close
|
62
|
+
end
|
42
63
|
end
|
43
|
-
|
44
|
-
def error(message, line, column); p "ERROR: #{message}" end
|
45
64
|
end
|
46
|
-
|
47
|
-
$handler = Handler.new
|
48
|
-
|
49
|
-
IO.popen("cat tst") { |p| puts Oj.sc_parse($handler, p) }
|
50
|
-
|
51
|
-
#File.open('tst', 'r') { |file| Oj.sc_parse($handler, file) }
|
data/test/pact.rb
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
# encoding: UTF-8
|
3
|
+
|
4
|
+
$: << File.dirname(__FILE__)
|
5
|
+
['ext', 'lib'].each do |dir|
|
6
|
+
$: << File.join(File.dirname(File.expand_path(File.dirname(__FILE__))), dir)
|
7
|
+
end
|
8
|
+
|
9
|
+
#require 'json'
|
10
|
+
require 'oj'
|
11
|
+
|
12
|
+
Oj.mimic_JSON()
|
13
|
+
require 'pact'
|
14
|
+
|
15
|
+
puts "*** responds? #{Regexp.respond_to?(:json_create)}"
|
16
|
+
|
17
|
+
j = /1/.to_json
|
18
|
+
|
19
|
+
puts "json: #{j}"
|
20
|
+
|
21
|
+
puts "regexp: #{JSON.load(j)}"
|
data/test/perf1.rb
ADDED
@@ -0,0 +1,64 @@
|
|
1
|
+
#!/usr/bin/env ruby -wW1
|
2
|
+
# encoding: UTF-8
|
3
|
+
|
4
|
+
$: << File.join(File.dirname(__FILE__), "../lib")
|
5
|
+
$: << File.join(File.dirname(__FILE__), "../ext")
|
6
|
+
|
7
|
+
#require 'test/unit'
|
8
|
+
require 'optparse'
|
9
|
+
require 'oj'
|
10
|
+
require 'ox'
|
11
|
+
|
12
|
+
$indent = 2
|
13
|
+
|
14
|
+
opts = OptionParser.new
|
15
|
+
opts.on("-h", "--help", "Show this display") { puts opts; Process.exit!(0) }
|
16
|
+
files = opts.parse(ARGV)
|
17
|
+
|
18
|
+
iter = 100000
|
19
|
+
s = %{
|
20
|
+
{ "class": "Foo::Bar",
|
21
|
+
"attr1": [ true, [false, [12345, null], 3.967, ["something", false], null]],
|
22
|
+
"attr2": { "one": 1 }
|
23
|
+
}
|
24
|
+
}
|
25
|
+
#s = File.read('sample.json')
|
26
|
+
|
27
|
+
Oj.default_options = { :indent => 0 }
|
28
|
+
|
29
|
+
obj = Oj.load(s)
|
30
|
+
xml = Ox.dump(obj, :indent => 0)
|
31
|
+
|
32
|
+
puts xml
|
33
|
+
|
34
|
+
start = Time.now
|
35
|
+
iter.times do
|
36
|
+
Oj.load(s)
|
37
|
+
end
|
38
|
+
dt = Time.now - start
|
39
|
+
puts "%d Oj.load()s in %0.3f seconds or %0.1f loads/msec" % [iter, dt, iter/dt/1000.0]
|
40
|
+
|
41
|
+
start = Time.now
|
42
|
+
iter.times do
|
43
|
+
Ox.load(xml)
|
44
|
+
end
|
45
|
+
dt = Time.now - start
|
46
|
+
puts "%d Ox.load()s in %0.3f seconds or %0.1f loads/msec" % [iter, dt, iter/dt/1000.0]
|
47
|
+
|
48
|
+
puts
|
49
|
+
|
50
|
+
start = Time.now
|
51
|
+
iter.times do
|
52
|
+
Oj.dump(obj)
|
53
|
+
end
|
54
|
+
dt = Time.now - start
|
55
|
+
puts "%d Oj.dump()s in %0.3f seconds or %0.1f dumps/msec" % [iter, dt, iter/dt/1000.0]
|
56
|
+
|
57
|
+
start = Time.now
|
58
|
+
iter.times do
|
59
|
+
Ox.dump(obj)
|
60
|
+
end
|
61
|
+
dt = Time.now - start
|
62
|
+
puts "%d Ox.dump()s in %0.3f seconds or %0.1f dumps/msec" % [iter, dt, iter/dt/1000.0]
|
63
|
+
|
64
|
+
puts
|
data/test/perf2.rb
ADDED
@@ -0,0 +1,76 @@
|
|
1
|
+
#!/usr/bin/env ruby -wW1
|
2
|
+
# encoding: UTF-8
|
3
|
+
|
4
|
+
$: << File.join(File.dirname(__FILE__), "../lib")
|
5
|
+
$: << File.join(File.dirname(__FILE__), "../ext")
|
6
|
+
|
7
|
+
#require 'test/unit'
|
8
|
+
require 'optparse'
|
9
|
+
require 'yajl'
|
10
|
+
require 'oj'
|
11
|
+
|
12
|
+
$indent = 2
|
13
|
+
|
14
|
+
opts = OptionParser.new
|
15
|
+
opts.on("-h", "--help", "Show this display") { puts opts; Process.exit!(0) }
|
16
|
+
files = opts.parse(ARGV)
|
17
|
+
|
18
|
+
class Foo
|
19
|
+
def initialize()
|
20
|
+
@x = true
|
21
|
+
@y = 58
|
22
|
+
end
|
23
|
+
def to_json()
|
24
|
+
%{{"x":#{@x},"y":#{@y}}}
|
25
|
+
end
|
26
|
+
def to_hash()
|
27
|
+
{ 'x' => @x, 'y' => @y }
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
iter = 100000
|
32
|
+
s = %{
|
33
|
+
{ "class": "Foo::Bar",
|
34
|
+
"attr1": [ true, [false, [12345, null], 3.967, ["something", false], null]],
|
35
|
+
"attr2": { "one": 1 }
|
36
|
+
}
|
37
|
+
}
|
38
|
+
|
39
|
+
obj = Oj.load(s)
|
40
|
+
obj["foo"] = Foo.new()
|
41
|
+
|
42
|
+
Oj.default_options = { :indent => 0, :effort => :internal }
|
43
|
+
|
44
|
+
puts
|
45
|
+
|
46
|
+
start = Time.now
|
47
|
+
iter.times do
|
48
|
+
Oj.load(s)
|
49
|
+
end
|
50
|
+
dt = Time.now - start
|
51
|
+
puts "%d Oj.load()s in %0.3f seconds or %0.1f loads/msec" % [iter, dt, iter/dt/1000.0]
|
52
|
+
|
53
|
+
start = Time.now
|
54
|
+
iter.times do
|
55
|
+
Yajl::Parser.parse(s)
|
56
|
+
end
|
57
|
+
dt = Time.now - start
|
58
|
+
puts "%d Yajl::Parser.parse()s in %0.3f seconds or %0.1f parses/msec" % [iter, dt, iter/dt/1000.0]
|
59
|
+
|
60
|
+
puts
|
61
|
+
|
62
|
+
start = Time.now
|
63
|
+
iter.times do
|
64
|
+
Oj.dump(obj)
|
65
|
+
end
|
66
|
+
dt = Time.now - start
|
67
|
+
puts "%d Oj.dump()s in %0.3f seconds or %0.1f dumps/msec" % [iter, dt, iter/dt/1000.0]
|
68
|
+
|
69
|
+
start = Time.now
|
70
|
+
iter.times do
|
71
|
+
Yajl::Encoder.encode(obj)
|
72
|
+
end
|
73
|
+
dt = Time.now - start
|
74
|
+
puts "%d Yajl::Encoder.encode()s in %0.3f seconds or %0.1f encodes/msec" % [iter, dt, iter/dt/1000.0]
|
75
|
+
|
76
|
+
puts
|
@@ -0,0 +1,213 @@
|
|
1
|
+
#!/usr/bin/env ruby -wW1
|
2
|
+
|
3
|
+
$: << '.'
|
4
|
+
$: << '..'
|
5
|
+
$: << '../lib'
|
6
|
+
$: << '../ext'
|
7
|
+
|
8
|
+
if __FILE__ == $0
|
9
|
+
if (i = ARGV.index('-I'))
|
10
|
+
x,path = ARGV.slice!(i, 2)
|
11
|
+
$: << path
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
require 'optparse'
|
16
|
+
require 'ox'
|
17
|
+
require 'oj'
|
18
|
+
require 'perf'
|
19
|
+
require 'sample'
|
20
|
+
require 'files'
|
21
|
+
|
22
|
+
$verbose = 0
|
23
|
+
$circular = false
|
24
|
+
$indent = 0
|
25
|
+
|
26
|
+
do_sample = false
|
27
|
+
do_files = false
|
28
|
+
|
29
|
+
do_load = false
|
30
|
+
do_dump = false
|
31
|
+
do_read = false
|
32
|
+
do_write = false
|
33
|
+
$iter = 1000
|
34
|
+
|
35
|
+
opts = OptionParser.new
|
36
|
+
opts.on("-v", "increase verbosity") { $verbose += 1 }
|
37
|
+
|
38
|
+
opts.on("-c", "circular options") { $circular = true }
|
39
|
+
|
40
|
+
opts.on("-s", "load and dump as sample Ruby object") { do_sample = true }
|
41
|
+
opts.on("-f", "load and dump as files Ruby object") { do_files = true }
|
42
|
+
|
43
|
+
opts.on("-l", "load") { do_load = true }
|
44
|
+
opts.on("-d", "dump") { do_dump = true }
|
45
|
+
opts.on("-r", "read") { do_read = true }
|
46
|
+
opts.on("-w", "write") { do_write = true }
|
47
|
+
opts.on("-a", "load, dump, read and write") { do_load = true; do_dump = true; do_read = true; do_write = true }
|
48
|
+
|
49
|
+
opts.on("-i", "--iterations [Int]", Integer, "iterations") { |i| $iter = i }
|
50
|
+
|
51
|
+
opts.on("-h", "--help", "Show this display") { puts opts; Process.exit!(0) }
|
52
|
+
files = opts.parse(ARGV)
|
53
|
+
|
54
|
+
if files.empty?
|
55
|
+
data = []
|
56
|
+
obj = do_sample ? sample_doc(2) : files('..')
|
57
|
+
mars = Marshal.dump(obj)
|
58
|
+
xml = Ox.dump(obj, :indent => $indent, circular: $circular)
|
59
|
+
json = Oj.dump(obj, :indent => $indent, circular: $circular)
|
60
|
+
File.open('sample.xml', 'w') { |f| f.write(xml) }
|
61
|
+
File.open('sample.json', 'w') { |f| f.write(json) }
|
62
|
+
File.open('sample.marshal', 'w') { |f| f.write(mars) }
|
63
|
+
data << { :file => 'sample.xml', :obj => obj, :xml => xml, :marshal => mars, :json => json }
|
64
|
+
else
|
65
|
+
puts "loading and parsing #{files}\n\n"
|
66
|
+
# TBD change to allow xml and json
|
67
|
+
data = files.map do |f|
|
68
|
+
xml = File.read(f)
|
69
|
+
obj = Ox.load(xml);
|
70
|
+
mars = Marshal.dump(obj)
|
71
|
+
json = Oj.dump(obj, :indent => $indent, circular: $circular)
|
72
|
+
{ :file => f, :obj => obj, :xml => xml, :marshal => mars, :json => json }
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
76
|
+
$ox_load_time = 0
|
77
|
+
$mars_load_time = 0
|
78
|
+
$ox_dump_time = 0
|
79
|
+
$oj_dump_time = 0
|
80
|
+
$mars_dump_time = 0
|
81
|
+
|
82
|
+
def perf_load(d)
|
83
|
+
filename = d[:file]
|
84
|
+
marshal_filename = 'sample.marshal'
|
85
|
+
xml = d[:xml]
|
86
|
+
mars = d[:marshal]
|
87
|
+
json = d[:json]
|
88
|
+
|
89
|
+
if 0 < $verbose
|
90
|
+
obj = Ox.load(xml, :mode => :object, :trace => $verbose)
|
91
|
+
return
|
92
|
+
end
|
93
|
+
start = Time.now
|
94
|
+
(1..$iter).each do
|
95
|
+
obj = Ox.load(xml, :mode => :object)
|
96
|
+
end
|
97
|
+
$ox_load_time = Time.now - start
|
98
|
+
puts "Parsing #{$iter} times with Ox took #{$ox_load_time} seconds."
|
99
|
+
|
100
|
+
start = Time.now
|
101
|
+
(1..$iter).each do
|
102
|
+
obj = Oj.load(json, :mode => :object)
|
103
|
+
end
|
104
|
+
$oj_load_time = Time.now - start
|
105
|
+
puts "Parsing #{$iter} times with Oj took #{$oj_load_time} seconds."
|
106
|
+
|
107
|
+
start = Time.now
|
108
|
+
(1..$iter).each do
|
109
|
+
obj = Marshal.load(mars)
|
110
|
+
end
|
111
|
+
$mars_load_time = Time.now - start
|
112
|
+
puts "Marshalling #{$iter} times took #{$mars_load_time} seconds."
|
113
|
+
puts ">>> Ox is %0.1f faster than Marshal loading.\n\n" % [$mars_load_time/$ox_load_time]
|
114
|
+
end
|
115
|
+
|
116
|
+
def perf_dump(d)
|
117
|
+
obj = d[:obj]
|
118
|
+
|
119
|
+
start = Time.now
|
120
|
+
(1..$iter).each do
|
121
|
+
xml = Ox.dump(obj, :indent => $indent, :circular => $circular)
|
122
|
+
#puts "*** ox:\n#{xml}"
|
123
|
+
end
|
124
|
+
$ox_dump_time = Time.now - start
|
125
|
+
puts "Ox dumping #{$iter} times with ox took #{$ox_dump_time} seconds."
|
126
|
+
|
127
|
+
Oj.default_options = {:indent => $indent}
|
128
|
+
start = Time.now
|
129
|
+
(1..$iter).each do
|
130
|
+
json = Oj.dump(obj)
|
131
|
+
end
|
132
|
+
$oj_dump_time = Time.now - start
|
133
|
+
puts "Oj dumping #{$iter} times with oj took #{$oj_dump_time} seconds."
|
134
|
+
|
135
|
+
obj = d[:obj]
|
136
|
+
start = Time.now
|
137
|
+
(1..$iter).each do
|
138
|
+
m = Marshal.dump(obj)
|
139
|
+
end
|
140
|
+
$mars_dump_time = Time.now - start
|
141
|
+
puts "Marshal dumping #{$iter} times took #{$mars_dump_time} seconds."
|
142
|
+
puts ">>> Ox is %0.1f faster than Marshal dumping.\n\n" % [$mars_dump_time/$ox_dump_time]
|
143
|
+
end
|
144
|
+
|
145
|
+
def perf_read(d)
|
146
|
+
ox_read_time = 0
|
147
|
+
mars_read_time = 0
|
148
|
+
|
149
|
+
filename = d[:file]
|
150
|
+
marshal_filename = 'sample.marshal'
|
151
|
+
xml = d[:xml]
|
152
|
+
mars = d[:marshal]
|
153
|
+
|
154
|
+
# now load from the file
|
155
|
+
start = Time.now
|
156
|
+
(1..$iter).each do
|
157
|
+
obj = Ox.load_file(filename, :mode => :object)
|
158
|
+
end
|
159
|
+
ox_read_time = Time.now - start
|
160
|
+
puts "Loading and parsing #{$iter} times with ox took #{ox_read_time} seconds."
|
161
|
+
|
162
|
+
start = Time.now
|
163
|
+
(1..$iter).each do
|
164
|
+
m = File.read(marshal_filename)
|
165
|
+
obj = Marshal.load(m)
|
166
|
+
end
|
167
|
+
mars_read_time = Time.now - start
|
168
|
+
puts "Reading and marshalling #{$iter} times took #{mars_read_time} seconds."
|
169
|
+
puts ">>> Ox is %0.1f faster than Marshal loading and parsing.\n\n" % [mars_read_time/ox_read_time]
|
170
|
+
|
171
|
+
end
|
172
|
+
|
173
|
+
def perf_write(d)
|
174
|
+
ox_write_time = 0
|
175
|
+
mars_write_time = 0
|
176
|
+
|
177
|
+
ox_filename = 'out.xml'
|
178
|
+
marshal_filename = 'out.marshal'
|
179
|
+
obj = d[:obj]
|
180
|
+
|
181
|
+
start = Time.now
|
182
|
+
(1..$iter).each do
|
183
|
+
xml = Ox.to_file(ox_filename, obj, :indent => $indent)
|
184
|
+
end
|
185
|
+
ox_write_time = Time.now - start
|
186
|
+
puts "Ox dumping #{$iter} times with ox took #{ox_write_time} seconds."
|
187
|
+
|
188
|
+
start = Time.now
|
189
|
+
(1..$iter).each do
|
190
|
+
m = Marshal.dump(obj, circular: $circular)
|
191
|
+
File.open(marshal_filename, "w") { |f| f.write(m) }
|
192
|
+
end
|
193
|
+
mars_write_time = Time.now - start
|
194
|
+
puts "Marshal dumping and writing #{$iter} times took #{mars_write_time} seconds."
|
195
|
+
puts ">>> Ox is %0.1f faster than Marshal dumping.\n\n" % [mars_write_time/ox_write_time]
|
196
|
+
|
197
|
+
end
|
198
|
+
|
199
|
+
#if do_sample or do_files
|
200
|
+
data.each do |d|
|
201
|
+
puts "Using file #{d[:file]}."
|
202
|
+
|
203
|
+
perf_load(d) if do_load
|
204
|
+
perf_dump(d) if do_dump
|
205
|
+
if do_load and do_dump
|
206
|
+
puts ">>> Ox is %0.1f faster than Marshal dumping and loading.\n\n" % [($mars_load_time + $mars_dump_time)/($ox_load_time + $ox_dump_time)] unless 0 == $mars_load_time
|
207
|
+
end
|
208
|
+
|
209
|
+
perf_read(d) if do_read
|
210
|
+
perf_write(d) if do_write
|
211
|
+
|
212
|
+
end
|
213
|
+
#end
|