Module: BenchmarkRunner::DataGenerator
- Defined in:
- lib/tasks/benchmark_runner.rb
Overview
Test data generators
Class Method Summary collapse
- .build_xml_element(items, depth, prefix, with_attrs, ns_decl) ⇒ Object
- .generate_html(items: DEFAULT_ITEMS, with_scripts: false, with_tables: false) ⇒ Object
- .generate_json(items: DEFAULT_ITEMS) ⇒ Object
- .generate_large_xml(items: 500) ⇒ Object
- .generate_xml(items: DEFAULT_ITEMS, depth: 1, with_namespaces: false, with_attributes: true) ⇒ Object
- .generate_yaml(items: DEFAULT_ITEMS) ⇒ Object
Class Method Details
.build_xml_element(items, depth, prefix, with_attrs, ns_decl) ⇒ Object
277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 |
# File 'lib/tasks/benchmark_runner.rb', line 277 def build_xml_element(items, depth, prefix, with_attrs, ns_decl) attrs = with_attrs ? " id=\"#{rand(1000)}\" status=\"active\"" : "" ns_attr = ns_decl.empty? ? "" : " #{ns_decl}" if depth <= 1 children = Array.new(items) do |i| inner_attrs = with_attrs ? " index=\"#{i}\"" : "" "<#{prefix}item#{inner_attrs}>Item #{i} content with some text</#{prefix}item>" end.join "<#{prefix}root#{ns_attr}#{attrs}>#{children}</#{prefix}root>" else child = build_xml_element(items / 2, depth - 1, prefix, with_attrs, "") "<#{prefix}root#{ns_attr}#{attrs}>#{child}</#{prefix}root>" end end |
.generate_html(items: DEFAULT_ITEMS, with_scripts: false, with_tables: false) ⇒ Object
294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 |
# File 'lib/tasks/benchmark_runner.rb', line 294 def generate_html(items: DEFAULT_ITEMS, with_scripts: false, with_tables: false) scripts = if with_scripts <<~HTML <script type="text/javascript"> // Some inline script function test() { return true; } </script> <style> body { margin: 0; } </style> HTML else "" end tables = if with_tables rows = Array.new(items) do |i| "<tr><td>Cell #{i}A</td><td>Cell #{i}B</td></tr>" end.join "<table>#{rows}</table>" else "" end list_items = Array.new(items) do |i| "<li class=\"item-#{i}\">List item #{i} with bold text</li>" end.join("\n ") nav_item_count = [(items / 5), 2].max nav_items = items.times.first(nav_item_count).map do |i| "<li class=\"nav-#{i}\">Nav #{i}</li>" end.join("\n ") <<~HTML <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Benchmark Document</title> #{scripts} </head> <body> <header> <h1>Benchmark Test Document</h1> <nav> <ul> #{nav_items} </ul> </nav> </header> <main> <section id="content"> <p>This is a paragraph with emphasized and strong text.</p> <ul> #{list_items} </ul> #{tables} </section> </main> <footer> <p>Copyright 2024</p> </footer> </body> </html> HTML end |
.generate_json(items: DEFAULT_ITEMS) ⇒ Object
362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 |
# File 'lib/tasks/benchmark_runner.rb', line 362 def generate_json(items: DEFAULT_ITEMS) data = { metadata: { version: "1.0", generated: Time.now.iso8601 }, items: Array.new(items) do |i| { id: i, name: "Item #{i}", value: rand * 1000, tags: ["tag1", "tag2", "tag#{i % 10}"], nested: { level1: { level2: { data: "deeply nested value #{i}" } }, }, } end, } JSON.generate(data) end |
.generate_large_xml(items: 500) ⇒ Object
385 386 387 388 |
# File 'lib/tasks/benchmark_runner.rb', line 385 def generate_large_xml(items: 500) generate_xml(items: items, depth: 3, with_namespaces: true, with_attributes: true) end |
.generate_xml(items: DEFAULT_ITEMS, depth: 1, with_namespaces: false, with_attributes: true) ⇒ Object
269 270 271 272 273 274 275 |
# File 'lib/tasks/benchmark_runner.rb', line 269 def generate_xml(items: DEFAULT_ITEMS, depth: 1, with_namespaces: false, with_attributes: true) ns = with_namespaces ? 'xmlns:ns="http://example.org"' : "" prefix = with_namespaces ? "ns:" : "" build_xml_element(items, depth, prefix, with_attributes, ns) end |
.generate_yaml(items: DEFAULT_ITEMS) ⇒ Object
380 381 382 383 |
# File 'lib/tasks/benchmark_runner.rb', line 380 def generate_yaml(items: DEFAULT_ITEMS) data = JSON.parse(generate_json(items: items)) data.to_yaml end |