|
180 | 180 | end |
181 | 181 | end |
182 | 182 |
|
| 183 | + describe '.write_json' do |
| 184 | + it 'writes JSON file with metadata and raw data' do |
| 185 | + Dir.mktmpdir do |dir| |
| 186 | + output_path = File.join(dir, 'output_001') |
| 187 | + ruby_descriptions = { |
| 188 | + 'ruby-base' => 'ruby 3.3.0', |
| 189 | + 'ruby-yjit' => 'ruby 3.3.0 +YJIT' |
| 190 | + } |
| 191 | + bench_data = { |
| 192 | + 'ruby-base' => { 'fib' => { 'time' => 1.5 } }, |
| 193 | + 'ruby-yjit' => { 'fib' => { 'time' => 1.0 } } |
| 194 | + } |
| 195 | + |
| 196 | + result_path = BenchmarkRunner.write_json(output_path, ruby_descriptions, bench_data) |
| 197 | + |
| 198 | + expected_path = File.join(dir, 'output_001.json') |
| 199 | + assert_equal expected_path, result_path |
| 200 | + assert File.exist?(expected_path) |
| 201 | + |
| 202 | + json_content = JSON.parse(File.read(expected_path)) |
| 203 | + assert_equal ruby_descriptions, json_content['metadata'] |
| 204 | + assert_equal bench_data, json_content['raw_data'] |
| 205 | + end |
| 206 | + end |
| 207 | + |
| 208 | + it 'returns the JSON file path' do |
| 209 | + Dir.mktmpdir do |dir| |
| 210 | + output_path = File.join(dir, 'output_test') |
| 211 | + result_path = BenchmarkRunner.write_json(output_path, {}, {}) |
| 212 | + |
| 213 | + assert_equal File.join(dir, 'output_test.json'), result_path |
| 214 | + end |
| 215 | + end |
| 216 | + |
| 217 | + it 'handles empty metadata and bench data' do |
| 218 | + Dir.mktmpdir do |dir| |
| 219 | + output_path = File.join(dir, 'output_empty') |
| 220 | + |
| 221 | + result_path = BenchmarkRunner.write_json(output_path, {}, {}) |
| 222 | + |
| 223 | + assert File.exist?(result_path) |
| 224 | + json_content = JSON.parse(File.read(result_path)) |
| 225 | + assert_equal({}, json_content['metadata']) |
| 226 | + assert_equal({}, json_content['raw_data']) |
| 227 | + end |
| 228 | + end |
| 229 | + |
| 230 | + it 'handles nested benchmark data structures' do |
| 231 | + Dir.mktmpdir do |dir| |
| 232 | + output_path = File.join(dir, 'output_nested') |
| 233 | + ruby_descriptions = { 'ruby' => 'ruby 3.3.0' } |
| 234 | + bench_data = { |
| 235 | + 'ruby' => { |
| 236 | + 'benchmark1' => { |
| 237 | + 'time' => 1.5, |
| 238 | + 'rss' => 12345, |
| 239 | + 'iterations' => [1.4, 1.5, 1.6] |
| 240 | + } |
| 241 | + } |
| 242 | + } |
| 243 | + |
| 244 | + result_path = BenchmarkRunner.write_json(output_path, ruby_descriptions, bench_data) |
| 245 | + |
| 246 | + json_content = JSON.parse(File.read(result_path)) |
| 247 | + assert_equal bench_data, json_content['raw_data'] |
| 248 | + end |
| 249 | + end |
| 250 | + |
| 251 | + it 'overwrites existing JSON file' do |
| 252 | + Dir.mktmpdir do |dir| |
| 253 | + output_path = File.join(dir, 'output_overwrite') |
| 254 | + |
| 255 | + # Write first version |
| 256 | + BenchmarkRunner.write_json(output_path, { 'v' => '1' }, { 'd' => '1' }) |
| 257 | + |
| 258 | + # Write second version |
| 259 | + new_metadata = { 'v' => '2' } |
| 260 | + new_data = { 'd' => '2' } |
| 261 | + result_path = BenchmarkRunner.write_json(output_path, new_metadata, new_data) |
| 262 | + |
| 263 | + json_content = JSON.parse(File.read(result_path)) |
| 264 | + assert_equal new_metadata, json_content['metadata'] |
| 265 | + assert_equal new_data, json_content['raw_data'] |
| 266 | + end |
| 267 | + end |
| 268 | + end |
| 269 | + |
183 | 270 | describe '.render_graph' do |
184 | 271 | it 'delegates to GraphRenderer and returns calculated png_path' do |
185 | 272 | Dir.mktmpdir do |dir| |
|
0 commit comments