import os
import random
import math
def random_color():
r = random.randint(0, 255)
g = random.randint(0, 255)
b = random.randint(0, 255)
a = random.uniform(0.1, 0.9)
return f"rgba({r},{g},{b},{a:.2f})"
def random_transform():
# Disabled transforms to avoid animation effects during rendering
return ""
def generate_path():
x = random.randint(0, 950)
y = random.randint(0, 950)
# Random path type
path_type = random.choice(['diamond', 'triangle', 'star', 'zigzag', 'curve'])
if path_type == 'diamond':
size = random.randint(5, 30)
d = f"M{x} {y} L{x+size} {y+size} L{x} {y+2*size} L{x-size} {y+size} Z"
elif path_type == 'triangle':
size = random.randint(5, 40)
d = f"M{x} {y} L{x+size} {y+size*1.7} L{x-size} {y+size*1.7} Z"
elif path_type == 'star':
size = random.randint(5, 25)
points = []
for i in range(5):
angle_outer = math.radians(i * 72 - 90)
angle_inner = math.radians(i * 72 - 90 + 36)
points.append(f"{x + size * math.cos(angle_outer):.1f} {y + size * math.sin(angle_outer):.1f}")
points.append(f"{x + size * 0.4 * math.cos(angle_inner):.1f} {y + size * 0.4 * math.sin(angle_inner):.1f}")
d = f"M{points[0]} L" + " L".join(points[1:]) + " Z"
elif path_type == 'zigzag':
segments = random.randint(3, 8)
points = [f"M{x} {y}"]
for i in range(segments):
dx = random.randint(10, 30)
dy = random.randint(-20, 20)
x += dx
y += dy
points.append(f"L{x} {y}")
d = " ".join(points)
else: # curve
cx1 = x + random.randint(20, 50)
cy1 = y + random.randint(-30, 30)
cx2 = x + random.randint(50, 80)
cy2 = y + random.randint(-30, 30)
ex = x + random.randint(60, 100)
ey = y + random.randint(-20, 20)
d = f"M{x} {y} C{cx1} {cy1} {cx2} {cy2} {ex} {ey}"
stroke_width = random.uniform(0.1, 2.0)
return f'\n'
def generate_circle():
cx = random.randint(20, 980)
cy = random.randint(20, 980)
r = random.randint(3, 40)
stroke_width = random.uniform(0.1, 2.0)
return f'\n'
def generate_rect():
x = random.randint(0, 950)
y = random.randint(0, 950)
w = random.randint(5, 50)
h = random.randint(5, 50)
rx = random.randint(0, min(w, h) // 2)
stroke_width = random.uniform(0.1, 2.0)
return f'\n'
def generate_ellipse():
cx = random.randint(30, 970)
cy = random.randint(30, 970)
rx = random.randint(5, 40)
ry = random.randint(5, 40)
stroke_width = random.uniform(0.1, 2.0)
return f'\n'
def generate_polygon():
cx = random.randint(50, 950)
cy = random.randint(50, 950)
sides = random.randint(3, 8)
radius = random.randint(10, 40)
points = []
for i in range(sides):
angle = math.radians(i * 360 / sides - 90)
px = cx + radius * math.cos(angle)
py = cy + radius * math.sin(angle)
points.append(f"{px:.1f},{py:.1f}")
stroke_width = random.uniform(0.1, 2.0)
return f'\n'
def generate_line():
x1 = random.randint(0, 1000)
y1 = random.randint(0, 1000)
x2 = random.randint(0, 1000)
y2 = random.randint(0, 1000)
stroke_width = random.uniform(0.5, 3.0)
return f'\n'
def generate_random_element():
generators = [
(generate_path, 40), # 40% paths
(generate_circle, 15), # 15% circles
(generate_rect, 15), # 15% rectangles
(generate_ellipse, 10), # 10% ellipses
(generate_polygon, 10), # 10% polygons
(generate_line, 10), # 10% lines
]
total = sum(w for _, w in generators)
r = random.randint(1, total)
cumulative = 0
for gen, weight in generators:
cumulative += weight
if r <= cumulative:
return gen()
return generate_path()
def create_heavy_svg(filename, target_bytes, seed=None):
if seed is not None:
random.seed(seed)
header = ''
with open(filename, 'w') as f:
f.write(header)
current_size = len(header) + len(footer)
while current_size < target_bytes:
element = generate_random_element()
f.write(element)
current_size += len(element)
f.write(footer)
# Setup directory
output_dir = "perf_test_files"
if not os.path.exists(output_dir):
os.makedirs(output_dir)
print("Starting generation...")
# Use a base seed for reproducibility
base_seed = 42
# 1. Generate 1KB to 100KB in increments of 1KB (100 files)
for k in range(1, 101):
size_bytes = k * 1024
create_heavy_svg(f"{output_dir}/test_{k}KB.svg", size_bytes, seed=base_seed + k)
# 2. Generate 200KB to 10MB in increments of 100KB (99 files)
for m in range(200, 10100, 100):
size_bytes = m * 1024
label = f"{m}KB" if m < 1000 else f"{m/1000:.1f}MB"
create_heavy_svg(f"{output_dir}/test_{label}.svg", size_bytes, seed=base_seed + m)
print(f"Done! Created 199 files in /{output_dir}")