Module: Performance::DataGenerator
- Defined in:
- lib/tasks/performance_report.rb
Constant Summary collapse
- DEFAULT_ITEMS =
Performance::DEFAULT_ITEMS
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
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/tasks/performance_report.rb', line 64 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
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 145 146 |
# File 'lib/tasks/performance_report.rb', line 81 def generate_html(items: DEFAULT_ITEMS, with_scripts: false, with_tables: false) scripts = if with_scripts <<~HTML <script type="text/javascript"> 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_count = [(items / 5), 2].max nav_items = items.times.first(nav_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
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 |
# File 'lib/tasks/performance_report.rb', line 148 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
170 171 172 173 |
# File 'lib/tasks/performance_report.rb', line 170 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
56 57 58 59 60 61 62 |
# File 'lib/tasks/performance_report.rb', line 56 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
166 167 168 |
# File 'lib/tasks/performance_report.rb', line 166 def generate_yaml(items: DEFAULT_ITEMS) JSON.parse(generate_json(items: items)).to_yaml end |