oj 2.18.4 → 2.18.5

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,185 +0,0 @@
1
-
2
- $: << File.expand_path("../../oj/ext", __FILE__)
3
- $: << File.expand_path("../../oj/lib", __FILE__)
4
-
5
- require 'rubygems'
6
- require 'sqlite3'
7
- require 'terminal-table'
8
- require 'oj'
9
- require 'rails/all'
10
- require 'benchmark'
11
- begin
12
- require 'benchmark/memory'
13
- rescue LoadError => e
14
- end
15
-
16
- OJ_1 = { mode: :object, use_as_json: false, float_precision: 16, bigdecimal_as_decimal: false, nan: :null }
17
- OJ_2 = { mode: :compat, use_as_json: false, float_precision: 16, bigdecimal_as_decimal: false, nan: :null, time_format: :xmlschema, second_precision: 3 }
18
- OJ_3 = { mode: :compat, use_as_json: true, float_precision: 16, bigdecimal_as_decimal: false, nan: :null, time_format: :xmlschema, second_precision: 3 }
19
-
20
- # test data
21
- class Colors
22
- include Enumerable
23
- def each
24
- yield 'red'
25
- yield 'green'
26
- yield 'blue'
27
- end
28
- end
29
-
30
- Struct.new('Customer', :name, :address)
31
-
32
- fork { exit 99 }
33
- Process.wait
34
-
35
- ActiveRecord::Base.logger = Logger.new(STDERR)
36
- ActiveRecord::Base.logger.level = 1
37
-
38
- ActiveRecord::Base.establish_connection(
39
- adapter: 'sqlite3',
40
- database:':memory:'
41
- )
42
-
43
- ActiveRecord::Migration.verbose = false
44
- ActiveRecord::Schema.define do
45
- create_table :users do |table|
46
- table.column :name, :string
47
- end
48
- end
49
-
50
- class User < ActiveRecord::Base
51
- validates :name, presence: true
52
- end
53
-
54
- user = User.new
55
- user.valid?
56
-
57
- TEST_DATA = {
58
- Regexp: /test/,
59
- FalseClass: false,
60
- NilClass: nil,
61
- Object: Object.new,
62
- TrueClass: true,
63
- String: 'abc',
64
- StringChinese: '二胡',
65
- Numeric: 1,
66
- Symbol: :sym,
67
- Time: Time.new,
68
- Array: [],
69
- Hash: {},
70
- HashNotEmpty: {a: 1},
71
- Date: Date.new,
72
- DateTime: DateTime.new,
73
- Enumerable: Colors.new,
74
- BigDecimal: '1'.to_d/3,
75
- BigDecimalInfinity: '0.5'.to_d/0,
76
- Struct: Struct::Customer.new('Dave', '123 Main'),
77
- Float: 1.0/3,
78
- FloatInfinity: 0.5/0,
79
- Range: (1..10),
80
- 'Process::Status': $?,
81
- 'ActiveSupport::TimeWithZone': Time.utc(2005,2,1,15,15,10).in_time_zone('Hawaii'),
82
- 'ActiveModel::Errors': user.errors,
83
- 'ActiveSupport::Duration': 1.month.ago,
84
- 'ActiveSupport::Multibyte::Chars': 'über'.mb_chars,
85
- 'ActiveRecord::Relation': User.where(name: 'aaa'),
86
-
87
- # 'ActionDispatch::Journey::GTG::TransitionTable': TODO,
88
- }
89
-
90
- # helper
91
- def compare(expected, result)
92
- if result.is_a? Exception
93
- '💀'
94
- else
95
- if expected != result
96
- puts "*** expected: #{expected}"
97
- puts "*** actual: #{result}"
98
- end
99
- expected == result ? '👌' : '❌'
100
- end
101
- end
102
-
103
- # def compare(expected, result)
104
- # if result.is_a? Exception
105
- # 'Error'
106
- # else
107
- # expected == result ? 'Ok' : 'Fail'
108
- # end
109
- # end
110
-
111
- # actual tests
112
- test_result = TEST_DATA.map do |key, val|
113
- to_json_result = val.to_json
114
-
115
- json_generate_result = begin
116
- JSON.generate(val)
117
- rescue JSON::GeneratorError => e
118
- e
119
- end
120
-
121
- Oj.default_options = OJ_1
122
- oj_dump_result_1 = begin
123
- if key == :DateTime
124
- Exception.new('Unknown error')
125
- else
126
- Oj.dump(val)
127
- end
128
- rescue NoMemoryError => e
129
- e
130
- end
131
-
132
- Oj.default_options = OJ_2
133
- oj_dump_result_2 = begin
134
- Oj.dump(val)
135
- rescue NoMemoryError => e
136
- e
137
- end
138
-
139
- Oj.default_options = OJ_3
140
- oj_dump_result_3 = begin
141
- Oj.dump(val)
142
- rescue NoMemoryError => e
143
- e
144
- end
145
-
146
- [key, {
147
- to_json_result: to_json_result,
148
- #json_generate: compare(to_json_result, json_generate_result),
149
- #oj_dump_1: compare(to_json_result, oj_dump_result_1),
150
- oj_dump_2: compare(to_json_result, oj_dump_result_2),
151
- #oj_dump_3: compare(to_json_result, oj_dump_result_3),
152
- }]
153
- end.to_h
154
-
155
- # format output
156
- rows = test_result.map do |key, val|
157
- [key, val[:json_generate], val[:oj_dump_1], val[:oj_dump_2], val[:oj_dump_3]]
158
- end
159
-
160
- puts "\nComparing Rails to_json with other JSON implementations\n"
161
- puts Terminal::Table.new headings: ['class', 'JSON.generate', 'Oj.dump (object)', 'Oj.dump (compat)', 'Oj.dump (compat, as_json)'], rows: rows
162
-
163
- Oj.default_options = OJ_3
164
-
165
- obj = TEST_DATA
166
-
167
- puts "\n"
168
-
169
- =begin
170
- if Benchmark.respond_to?(:memory)
171
- Benchmark.memory do |x|
172
- x.report('to_json:'){ 10_000.times { obj.to_json } }
173
- x.report('Oj:') { 10_000.times { Oj.dump(obj) } }
174
- x.compare!
175
- end
176
- puts "---------------------------------------------\n\n"
177
- end
178
-
179
- Benchmark.benchmark(Benchmark::CAPTION, 14, Benchmark::FORMAT) do |x|
180
- x.report('to_json:'){ 10_000.times { obj.to_json } }
181
- x.report('Oj:') { 10_000.times { Oj.dump(obj) } }
182
- end
183
- =end
184
- puts "\n"
185
-