Skip to content

Commit e9cbf1a

Browse files
committed
Add synthetic data generator for performance testing
Add a new 'generate_perf_data' management command that allows us to generate projects, series and patches for testing purposes. Signed-off-by: Stephen Finucane <stephen@that.guru> Assisted-by: OpenAI gpt-6.1-sol (via pi)
1 parent 3a88d8d commit e9cbf1a

3 files changed

Lines changed: 266 additions & 0 deletions

File tree

‎docs/development/index.rst‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,4 @@ developer-focussed usage information, is provided in the below docs.
1515
/development/releasing
1616
/development/api
1717
/development/assets
18+
/development/performance

‎docs/development/performance.rst‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
Performance testing
2+
===================
3+
4+
Patchwork provides the ``generate_perf_data`` management command to create
5+
synthetic projects, submitters, series, patches and replies for local performance
6+
testing. You can use this with a disposable database, configured like the
7+
database backend of the instance you want to measure. You can use the command
8+
like so:
9+
10+
.. code-block:: shell
11+
12+
python manage.py migrate
13+
python manage.py loaddata default_states
14+
python manage.py generate_perf_data --projects 12 --patches-per-project 100000
15+
16+
The defaults create 1.2 million patches in batches of 1,000, with up to ten
17+
patches per series and 100 shared submitters. Each patch has between 1 and 10
18+
replies (roughly 6.6 million replies with the defaults), with randomly chosen
19+
submitters and dates after the patch. Reply counts and contents are reproducible
20+
with ``--seed``. Patch dates span a year. All patches contain the same small
21+
diff: this is a query-performance dataset, not a
22+
realistic simulation of database storage requirements or mail parsing.
23+
24+
Generated projects have names such as ``perf-1``. The command refuses to reuse
25+
existing project names or generated submitter addresses. Use a different prefix
26+
for another dataset, or recreate the disposable database. It never deletes data.
27+
Batches are committed independently, so an interrupted run leaves partial data.
28+
Save hooks and signals are bypassed: events, tags, checks and cover letters are
29+
not generated.
Lines changed: 236 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,236 @@
1+
# Patchwork - automated patch tracking system
2+
# SPDX-License-Identifier: GPL-2.0-or-later
3+
4+
import datetime
5+
import random
6+
import re
7+
8+
from django.conf import settings
9+
from django.core.management.base import BaseCommand
10+
from django.core.management.base import CommandError
11+
from django.db import reset_queries
12+
from django.db import transaction
13+
14+
from patchwork.hasher import hash_diff
15+
from patchwork.models import Patch
16+
from patchwork.models import PatchComment
17+
from patchwork.models import Person
18+
from patchwork.models import Project
19+
from patchwork.models import Series
20+
from patchwork.models import State
21+
22+
23+
DIFF = '''diff --git a/example.txt b/example.txt
24+
--- a/example.txt
25+
+++ b/example.txt
26+
@@ -1 +1 @@
27+
-before
28+
+after
29+
'''
30+
31+
32+
class Command(BaseCommand):
33+
help = 'Generate synthetic projects and patches for performance testing'
34+
35+
def add_arguments(self, parser):
36+
parser.add_argument('--projects', type=int, default=12)
37+
parser.add_argument('--patches-per-project', type=int, default=100000)
38+
parser.add_argument('--batch-size', type=int, default=1000)
39+
parser.add_argument('--max-series-size', type=int, default=10)
40+
parser.add_argument('--open-series-percent', type=int, default=20)
41+
parser.add_argument('--archived-percent', type=int, default=10)
42+
parser.add_argument('--seed', type=int, default=42)
43+
parser.add_argument('--prefix', default='perf')
44+
45+
def handle(self, *args, **options):
46+
for option in (
47+
'projects',
48+
'patches_per_project',
49+
'batch_size',
50+
'max_series_size',
51+
):
52+
if options[option] < 1:
53+
raise CommandError(f'{option.replace("_", "-")} must be > 0')
54+
if options['max_series_size'] > 32767:
55+
raise CommandError('max-series-size must be <= 32767')
56+
for option in ('open_series_percent', 'archived_percent'):
57+
if not 0 <= options[option] <= 100:
58+
raise CommandError(
59+
f'{option.replace("_", "-")} must be between 0 and 100'
60+
)
61+
prefix = options['prefix']
62+
if not re.fullmatch(r'[a-zA-Z0-9_-]{1,64}', prefix):
63+
raise CommandError('prefix must be a slug of 1 to 64 characters')
64+
65+
names = [f'{prefix}-{i + 1}' for i in range(options['projects'])]
66+
emails = [f'{prefix}-{i + 1}@example.invalid' for i in range(100)]
67+
if Project.objects.filter(linkname__in=names).exists() or (
68+
Project.objects.filter(name__in=names).exists()
69+
):
70+
raise CommandError(
71+
'Generated projects already exist; use a new prefix'
72+
)
73+
if Person.objects.filter(email__in=emails).exists():
74+
raise CommandError(
75+
'Generated people already exist; use a new prefix'
76+
)
77+
open_state = State.objects.filter(action_required=True).first()
78+
closed_state = State.objects.filter(action_required=False).first()
79+
if not open_state or not closed_state:
80+
raise CommandError(
81+
'An actionable and a non-actionable state are required. '
82+
'Load default_states first.'
83+
)
84+
85+
rng = random.Random(options['seed']) # noqa: S311
86+
Person.objects.bulk_create(
87+
[
88+
Person(email=email, name=f'Test submitter {i + 1}')
89+
for i, email in enumerate(emails)
90+
]
91+
)
92+
# Query IDs explicitly: not every backend returns them on bulk insert.
93+
people = list(
94+
Person.objects.filter(email__in=emails).order_by('email')
95+
)
96+
total_series = 0
97+
total_replies = 0
98+
for name in names:
99+
project = Project.objects.create(
100+
linkname=name,
101+
name=name,
102+
listid=f'{name}.example.invalid',
103+
listemail=f'{name}@example.invalid',
104+
use_tags=False,
105+
)
106+
count, replies = self.generate_project(
107+
project, people, open_state, closed_state, rng, options
108+
)
109+
total_series += count
110+
total_replies += replies
111+
self.stdout.write(
112+
self.style.SUCCESS(
113+
f'Created {len(names)} projects, {total_series} series, '
114+
f'{len(names) * options["patches_per_project"]} patches and '
115+
f'{total_replies} replies.'
116+
)
117+
)
118+
119+
def generate_project(
120+
self, project, people, open_state, closed_state, rng, options
121+
):
122+
remaining = options['patches_per_project']
123+
series_count = 0
124+
patch_count = 0
125+
reply_count = 0
126+
diff_hash = hash_diff(DIFF)
127+
# A fixed anchor makes dates reproducible as well as relationships.
128+
anchor = datetime.datetime(2025, 1, 1)
129+
if settings.USE_TZ:
130+
anchor = anchor.replace(tzinfo=datetime.timezone.utc)
131+
while remaining:
132+
series_batch = []
133+
patches = []
134+
while remaining and len(patches) < options['batch_size']:
135+
size = min(
136+
rng.randint(1, options['max_series_size']),
137+
remaining,
138+
options['batch_size'] - len(patches),
139+
)
140+
series_count += 1
141+
date = anchor - datetime.timedelta(
142+
seconds=rng.randrange(365 * 24 * 60 * 60)
143+
)
144+
series = Series(
145+
project=project,
146+
name=f'Synthetic series {series_count}',
147+
date=date,
148+
submitter=rng.choice(people),
149+
total=size,
150+
)
151+
series_batch.append(series)
152+
is_open = rng.randrange(100) < options['open_series_percent']
153+
# Guarantee one match for open series, at a varied position.
154+
actionable = rng.randint(1, size) if is_open else None
155+
for number in range(1, size + 1):
156+
archived = rng.randrange(100) < options['archived_percent']
157+
state = rng.choice((open_state, closed_state))
158+
if number == actionable:
159+
archived, state = False, open_state
160+
elif not is_open and not archived:
161+
state = closed_state
162+
patch_count += 1
163+
patches.append(
164+
Patch(
165+
project=project,
166+
series=series,
167+
number=number,
168+
name=f'[PATCH {number}/{size}] {series.name}',
169+
msgid=f'<{project.linkname}-{patch_count}@example.invalid>',
170+
date=date,
171+
submitter=series.submitter,
172+
state=state,
173+
archived=archived,
174+
content='Synthetic performance test patch.\n',
175+
diff=DIFF,
176+
hash=diff_hash,
177+
)
178+
)
179+
remaining -= size
180+
181+
# Bulk inserts intentionally bypass save hooks, tags and events.
182+
with transaction.atomic():
183+
Series.objects.bulk_create(series_batch)
184+
saved_series = {
185+
series.name: series
186+
for series in Series.objects.filter(
187+
project=project,
188+
name__in=[series.name for series in series_batch],
189+
)
190+
}
191+
for patch in patches:
192+
patch.series = saved_series[patch.series.name]
193+
Patch.objects.bulk_create(patches)
194+
# As with series, query IDs for backends that don't return
195+
# primary keys after bulk inserts.
196+
patch_ids = dict(
197+
Patch.objects.filter(
198+
project=project,
199+
msgid__in=[patch.msgid for patch in patches],
200+
).values_list('msgid', 'pk')
201+
)
202+
replies = []
203+
for patch in patches:
204+
date = patch.date
205+
for number in range(1, rng.randint(1, 10) + 1):
206+
date += datetime.timedelta(
207+
minutes=rng.randint(1, 24 * 60)
208+
)
209+
replies.append(
210+
PatchComment(
211+
patch_id=patch_ids[patch.msgid],
212+
msgid=f'<reply-{number}-{patch.msgid[1:]}',
213+
date=date,
214+
submitter=rng.choice(people),
215+
headers=(
216+
f'In-Reply-To: {patch.msgid}\n'
217+
f'References: {patch.msgid}\n'
218+
),
219+
content='Synthetic performance test reply.\n',
220+
)
221+
)
222+
reply_count += 1
223+
if len(replies) >= options['batch_size']:
224+
PatchComment.objects.bulk_create(replies)
225+
replies = []
226+
if replies:
227+
PatchComment.objects.bulk_create(replies)
228+
# Development settings enable DEBUG; don't retain millions of SQL
229+
# queries in memory while generating a large dataset.
230+
reset_queries()
231+
if options['verbosity']:
232+
self.stdout.write(
233+
f'{project.linkname}: {patch_count}/'
234+
f'{options["patches_per_project"]} patches'
235+
)
236+
return series_count, reply_count

0 commit comments

Comments
 (0)