-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbenchmark_comparison.rb
More file actions
144 lines (130 loc) · 3.56 KB
/
benchmark_comparison.rb
File metadata and controls
144 lines (130 loc) · 3.56 KB
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# frozen_string_literal: true
# gem install benchmark-ips json-repair json_mend
# ruby -Ilib benchmark_comparison.rb
require 'benchmark/ips'
require 'json'
# --- Load Libraries ---
begin
require 'json_mend'
rescue LoadError
abort "❌ Could not load 'json_mend'. Make sure you are in the gem root or have it installed."
end
begin
require 'json/repair'
rescue LoadError
puts "❌ Could not load 'json-repair'. Benchmarks for it will be skipped."
end
puts '========================================================='
puts ' 🚀 JSON Repair Benchmark (IPS) '
puts " JsonMend (v#{JsonMend::VERSION}) vs json-repair-rb (v#{JSON::Repair::VERSION})"
puts '========================================================='
puts
# --- Test Data ---
json_object = '{"id": 1, "name": "Test", "active": true, "tags": ["a", "b"]}'
TEST_CASES = {
valid_single: {
label: 'Valid Single JSON',
input: json_object
},
concatenated: {
label: 'Concatenated JSON (x10)',
input: json_object * 10
},
simple_fix: {
label: 'Simple Fix (Missing Quotes)',
input: '{name: "Alice", age: 30, city: "Wonderland"}'
},
trailing_comma: {
label: 'Trailing Commas',
input: '{"items": [1, 2, 3,], "active": true,}'
},
comments: {
label: 'Comments (// and #)',
input: <<~JSON
{
"key": "value", // This is a comment
"config": {
"timeout": 100 # Another comment
}
}
JSON
},
complex: {
label: 'Complex & Mixed Errors',
input: <<~JSON
{
name: "Broken",
"nested": [
{id: 1,},
{id: 2}
],
"dangling": [1, 2, 3
JSON
},
garbage: {
label: 'Heavy Garbage / Hallucinations',
input: 'Here is the JSON: ```json {"a": 1} ``` and some other text.'
},
python_style: {
label: 'Python Literals (True/None)',
input: '{"is_valid": True, "missing": None, "wrong_bool": False}'
},
single_quotes: {
label: 'Single Quotes (JS Style)',
input: "{'id': 123, 'status': 'pending', 'meta': {'active': true}}"
},
deep_nesting: {
label: 'Deeply Nested (Stack Test)',
input: "#{'{"a":' * 50}1#{'}' * 50}"
},
unbalanced: {
label: 'Truncated / Unbalanced',
input: '{"users": [{"id": 1, "name": "Alice"}, {"id": 2'
},
unescaped_control: {
label: 'Unescaped Newlines/Tabs',
input: "{\"bio\": \"This is a \n multi-line string \t with tabs.\"}"
},
large_array: {
label: 'Large Single Array (Throughput)',
input: "[#{(1..1000).map { |i| %({"id": #{i}, "val": "item_#{i}"}) }.join(',')}]"
},
concatenated_complex: {
label: 'Concatenated + Broken (LLM Stream)',
input: '{"part": 1} {part: 2, "broken": true} {"part": 3}'
}
}.freeze
# Helper to check if a library supports the input before benchmarking
def supported?(library_proc, input)
library_proc.call(input)
true
rescue StandardError
false
end
# --- Run Benchmarks ---
TEST_CASES.each_value do |data|
puts "\n\n🔸 Scenario: #{data[:label]}"
puts '-' * 40
Benchmark.ips do |x|
x.config(time: 2, warmup: 1) # Short duration for quick checks
# 1. JsonMend
if supported?(->(i) { JsonMend.repair(i) }, data[:input])
x.report('JsonMend') do
JsonMend.repair(data[:input])
end
else
puts ' JsonMend: ❌ Not Supported'
end
# 2. json-repair
if defined?(JSON::Repair)
if supported?(->(i) { JSON.repair(i) }, data[:input])
x.report('json-repair') do
JSON.repair(data[:input])
end
else
puts ' json-repair: ❌ Not Supported'
end
end
x.compare!
end
end