@@ -35,6 +35,7 @@ if RUBY_VERSION >= "2.7"
35
35
RubyMemcheck ::RSpec ::RakeTask . new ( valgrind : :compile )
36
36
end
37
37
38
+ # rubocop:disable Metrics/BlockLength
38
39
task bench : :compile do
39
40
require "benchmark"
40
41
require "benchmark/ips"
@@ -48,25 +49,26 @@ if RUBY_VERSION >= "2.7"
48
49
require "json_scanner"
49
50
50
51
json_str = File . read ( "spec/graphql_response.json" )
51
- json_path = [ : data, : search, : searchResult, : paginationV2, : maxPage]
52
+ json_path = %i[ data search searchResult paginationV2 maxPage ]
52
53
json_selector = JsonScanner ::Config . new ( [ json_path ] )
53
54
54
55
# TODO: better title display
55
56
puts "\n \n \n "
56
- title = "========= JSON string size: #{ ActiveSupport ::NumberHelper . number_to_human_size ( json_str . bytesize ) } ========="
57
+ json_str_size = ActiveSupport ::NumberHelper . number_to_human_size ( json_str . bytesize )
58
+ title = "========= JSON string size: #{ json_str_size } ========="
57
59
puts Rainbow ( title ) . color ( 136 , 17 , 2 )
58
- page_size_with_json = -> do
60
+ page_size_with_json = lambda do
59
61
JSON . parse ( json_str , symbolize_names : true ) . dig ( *json_path )
60
62
end
61
- page_size_with_oj = -> do
63
+ page_size_with_oj = lambda do
62
64
Oj . load ( json_str , symbolize_names : true , mode : :object ) . dig ( *json_path )
63
65
end
64
- page_size_with_json_scanner_scan = -> do
66
+ page_size_with_json_scanner_scan = lambda do
65
67
JsonScanner . scan ( json_str , json_selector ) . first . first . then do |begin_pos , end_pos , _type |
66
68
JSON . parse ( json_str . byteslice ( begin_pos ...end_pos ) , quirks_mode : true )
67
69
end
68
70
end
69
- page_size_with_json_scanner_parse = -> do
71
+ page_size_with_json_scanner_parse = lambda do
70
72
JsonScanner . parse ( json_str , json_selector , symbolize_path_keys : true ) . dig ( *json_path )
71
73
end
72
74
@@ -76,9 +78,10 @@ if RUBY_VERSION >= "2.7"
76
78
page_size_with_json_scanner_scan . call ,
77
79
page_size_with_json_scanner_parse . call ,
78
80
]
79
- puts Rainbow ( "path #{ json_path . map ( &:inspect ) . join ( ", " ) } ; extracted values: #{ results } " ) . send ( results . uniq . size == 1 ? :green : :red )
81
+ results_report = "path #{ json_path . map ( &:inspect ) . join ( ", " ) } ; extracted values: #{ results } "
82
+ puts Rainbow ( results_report ) . send ( results . uniq . size == 1 ? :green : :red )
80
83
81
- benchmark = -> ( type , x ) do
84
+ benchmark = lambda do | type , x |
82
85
# not supported by 'memory'
83
86
# x.config(:stats => :bootstrap, :confidence => 95)
84
87
@@ -94,6 +97,7 @@ if RUBY_VERSION >= "2.7"
94
97
puts Rainbow ( "=" * title . size ) . color ( 136 , 17 , 2 )
95
98
puts "\n \n \n "
96
99
end
100
+ # rubocop:enable Metrics/BlockLength
97
101
end
98
102
99
103
# ========= JSON string size: 463 KB =========
137
141
# json: 1987269 allocated - 5400.19x more
138
142
# ============================================
139
143
140
-
141
-
142
144
# ========= JSON string size: 463 KB =========
143
145
# path :data, :search, :searchResult, :paginationV2, :maxPage; extracted values: [8, 8, 8, 8]
144
146
# ruby 3.2.2 (2023-03-30 revision e51014f9c0) [x86_64-linux]
180
182
# oj: 1440774 allocated - 3915.15x more
181
183
# ============================================
182
184
183
-
184
-
185
185
# ========= JSON string size: 463 KB =========
186
186
# path :data, :search, :searchResult, :paginationV2, :maxPage; extracted values: [8, 8, 8, 8]
187
187
# ruby 3.4.2 (2025-02-15 revision d2930f8e7a) +PRISM [x86_64-linux]
0 commit comments