|
| 1 | +#!/usr/bin/env python3 |
| 2 | +"""Generate a large XML file for SAX streaming stress testing. |
| 3 | +
|
| 4 | +Usage: python3 generate_stress_test.py [num_records] [output_path] |
| 5 | +
|
| 6 | +Default: 1,000,000 records (~382MB) written to test/xml/sax_stress_test.xml |
| 7 | +
|
| 8 | +The generated file contains varied record content designed to exercise: |
| 9 | +- UTF-8 characters (Cyrillic, Japanese, Turkish, French) |
| 10 | +- XML special characters (&, <, >, ") |
| 11 | +- Multiple data types (INTEGER, DOUBLE, BOOLEAN, DATE, VARCHAR) |
| 12 | +- Attributes on record elements (id, sku) |
| 13 | +- 10 rotating categories including one with & (food & beverage) |
| 14 | +""" |
| 15 | +import os |
| 16 | +import sys |
| 17 | + |
| 18 | +def generate(num_records=1000000, output_path=None): |
| 19 | + if output_path is None: |
| 20 | + output_path = os.path.join(os.path.dirname(__file__), "sax_stress_test.xml") |
| 21 | + |
| 22 | + categories = [ |
| 23 | + "electronics", "clothing", "food & beverage", "toys", "books & media", |
| 24 | + "health", "automotive", "sports", "home & garden", "office", |
| 25 | + ] |
| 26 | + |
| 27 | + special_names = [ |
| 28 | + 'Standard Item', |
| 29 | + 'Item with "quotes"', |
| 30 | + "Item with <brackets>", |
| 31 | + 'Item with & ampersand', |
| 32 | + 'Таймаут продукт', |
| 33 | + '日本語の製品', |
| 34 | + 'Ürün açıklaması', |
| 35 | + 'Produit français', |
| 36 | + ] |
| 37 | + |
| 38 | + with open(output_path, "w", encoding="utf-8") as f: |
| 39 | + f.write('<?xml version="1.0" encoding="UTF-8"?>\n') |
| 40 | + f.write("<catalog>\n") |
| 41 | + |
| 42 | + for i in range(1, num_records + 1): |
| 43 | + cat = categories[i % len(categories)] |
| 44 | + name_base = special_names[i % len(special_names)] |
| 45 | + price = round(0.01 + (i % 99999) * 0.01, 2) |
| 46 | + quantity = i % 10000 |
| 47 | + month = (i % 12) + 1 |
| 48 | + day = (i % 28) + 1 |
| 49 | + active = "true" if i % 3 != 0 else "false" |
| 50 | + rating = round(1.0 + (i % 50) * 0.1, 1) |
| 51 | + |
| 52 | + name = f"{name_base} #{i}" |
| 53 | + name = name.replace("&", "&").replace("<", "<").replace(">", ">").replace('"', """) |
| 54 | + cat_escaped = cat.replace("&", "&") |
| 55 | + desc = f"This is the description for product {i}. It contains enough text to make each record larger." |
| 56 | + |
| 57 | + f.write(f' <product id="{i}" sku="SKU-{i:07d}">\n') |
| 58 | + f.write(f" <name>{name}</name>\n") |
| 59 | + f.write(f" <category>{cat_escaped}</category>\n") |
| 60 | + f.write(f" <price>{price}</price>\n") |
| 61 | + f.write(f" <quantity>{quantity}</quantity>\n") |
| 62 | + f.write(f" <date>2024-{month:02d}-{day:02d}</date>\n") |
| 63 | + f.write(f" <active>{active}</active>\n") |
| 64 | + f.write(f" <rating>{rating}</rating>\n") |
| 65 | + f.write(f" <description>{desc}</description>\n") |
| 66 | + f.write(" </product>\n") |
| 67 | + |
| 68 | + if i % 200000 == 0: |
| 69 | + print(f" Generated {i}/{num_records} records...", file=sys.stderr) |
| 70 | + |
| 71 | + f.write("</catalog>\n") |
| 72 | + |
| 73 | + size_mb = os.path.getsize(output_path) / (1024 * 1024) |
| 74 | + print(f"Generated {output_path}: {size_mb:.1f} MB, {num_records} records") |
| 75 | + |
| 76 | + |
| 77 | +if __name__ == "__main__": |
| 78 | + n = int(sys.argv[1]) if len(sys.argv) > 1 else 1000000 |
| 79 | + p = sys.argv[2] if len(sys.argv) > 2 else None |
| 80 | + generate(n, p) |
0 commit comments