-
-
Notifications
You must be signed in to change notification settings - Fork 76
/
perf_obj.rb
executable file
·124 lines (103 loc) · 3.38 KB
/
perf_obj.rb
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
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#!/usr/bin/env ruby
$: << '.'
$: << '../lib'
$: << '../ext'
if __FILE__ == $0
if (i = ARGV.index('-I'))
x = ARGV.slice!(i, 2)
$: << x[1]
end
end
require 'optparse'
require 'ox'
require 'oj'
require 'perf'
require 'sample'
require 'files'
$circular = false
$indent = 0
do_sample = false
do_files = false
do_load = false
do_dump = false
do_read = false
do_write = false
$iter = 1000
opts = OptionParser.new
opts.on("-c", "circular options") { $circular = true }
opts.on("-s", "load and dump as sample Ruby object") { do_sample = true }
opts.on("-f", "load and dump as files Ruby object") { do_files = true }
opts.on("-l", "load") { do_load = true }
opts.on("-d", "dump") { do_dump = true }
opts.on("-r", "read") { do_read = true }
opts.on("-w", "write") { do_write = true }
opts.on("-a", "load, dump, read and write") { do_load = true; do_dump = true; do_read = true; do_write = true }
opts.on("-i", "--iterations [Int]", Integer, "iterations") { |it| $iter = it }
opts.on("-h", "--help", "Show this display") { puts opts; Process.exit!(0) }
files = opts.parse(ARGV)
$obj = nil
$xml = nil
$mars = nil
$json = nil
unless do_load || do_dump || do_read || do_write
do_load = true
do_dump = true
do_read = true
do_write = true
end
# prepare all the formats for input
if files.empty?
$obj = do_sample ? sample_doc(2) : files('..')
$mars = Marshal.dump($obj)
$xml = Ox.dump($obj, :indent => $indent, :circular => $circular)
$json = Oj.dump($obj, :indent => $indent, :circular => $circular)
File.open('sample.xml', 'w') { |f| f.write($xml) }
File.open('sample.json', 'w') { |f| f.write($json) }
File.open('sample.marshal', 'w') { |f| f.write($mars) }
else
puts "loading and parsing #{files}\n\n"
# TBD change to allow xml and json
data = files.map do |f|
$xml = File.read(f)
$obj = Ox.load($xml);
$mars = Marshal.dump($obj)
$json = Oj.dump($obj, :indent => $indent, :circular => $circular)
end
end
Oj.default_options = { :mode => :object, :indent => $indent }
if do_load
puts '-' * 80
puts "Load Performance"
perf = Perf.new()
perf.add('Ox', 'load') { Ox.load($xml, :mode => :object) }
perf.add('Oj', 'load') { Oj.load($json) }
perf.add('Marshal', 'load') { Marshal.load($mars) }
perf.run($iter)
end
if do_dump
puts '-' * 80
puts "Dump Performance"
perf = Perf.new()
perf.add('Ox', 'dump') { Ox.dump($obj, :indent => $indent, :circular => $circular) }
perf.add('Oj', 'dump') { Oj.dump($obj) }
perf.add('Marshal', 'dump') { Marshal.dump($obj) }
perf.run($iter)
end
if do_read
puts '-' * 80
puts "Read from file Performance"
perf = Perf.new()
perf.add('Ox', 'load_file') { Ox.load_file('sample.xml', :mode => :object) }
perf.add('Oj', 'load') { Oj.load_file('sample.json') }
perf.add('Marshal', 'load') { Marshal.load(File.new('sample.marshal')) }
perf.run($iter)
end
if do_write
puts '-' * 80
puts "Write to file Performance"
perf = Perf.new()
perf.add('Ox', 'to_file') { Ox.to_file('sample.xml', $obj, :indent => $indent, :circular => $circular) }
perf.add('Oj', 'to_file') { Oj.to_file('sample.json', $obj) }
perf.add('Marshal', 'dump') { Marshal.dump($obj, File.new('sample.marshal', 'w')) }
perf.run($iter)
end