-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest.rb
executable file
·70 lines (59 loc) · 1.61 KB
/
test.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
#!/usr/bin/env ruby
require 'bundler'
require 'json' unless defined?(JSON)
Bundler.require
data = {
'post-office-box' => '12345',
'locality' => 'Broomfield',
'region' => 'CO',
'country-name' => 'U.S.A'
}
schema = {
'description' => 'An Address following the convention of http://microformats.org/wiki/hcard',
'type' => 'object',
'properties' => {
"post-office-box" => { 'type' => 'string' },
"extended-address" => { 'type' => 'string' },
"street-address" => { 'type' => 'string' },
"locality" => { 'type' => 'string' },
'region' => { 'type' => 'string' },
"postal-code" => { 'type' => 'string' },
"country-name" => { 'type' => 'string' }
},
'required' => ['locality', 'region', 'country-name'],
'dependencies' => {
'post-office-box' => 'street-address',
'extended-address' => 'street-address'
}
}
schema_file = 'schema.json'
File.open('schema.json','w') do |f|
f << JSON.dump(schema)
end unless File.exist?(schema_file)
def warmup(&block)
return unless RUBY_ENGINE == 'jruby'
count = 500_000
slice_size = 5_000
p = ProgressBar.create(format: '(%E) %w (%R rate)', total: count)
count.times.each_slice(slice_size) do |slice|
slice.each(&block)
p.progress += slice.count
end
p.finish
end
warmup do
JSON::Validator.validate(schema, data)
end
warmup do
JSON::Validator.validate(schema_file, data)
end
Benchmark.ips do |x|
x.config time: 30, warmup: 90
x.report('json-schema from file') do
JSON::Validator.validate(schema_file, data)
end
x.report('json-schema from hash') do
JSON::Validator.validate(schema, data)
end
x.compare!
end