:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
chore(tests): speed up recorder-script bulk test and pytest session overhead (#70058)
Paul D'Ambra30629f432916
diff --git a/posthog/conftest.py b/posthog/conftest.pyindex cce939200a5f..a4d52b82bb52 100644--- a/posthog/conftest.py+++ b/posthog/conftest.py@@ -158,6 +158,26 @@ def reset_clickhouse_tables():# Using `ON CLUSTER` takes x20 more time to drop the tables: https://github.com/ClickHouse/ClickHouse/issues/15473.TABLES_TO_CREATE_DROP += [f"DROP TABLE {table[0]}" for table in kafka_tables]+ # Skip truncating tables ClickHouse reports as empty: each truncate costs a keeper+ # round-trip on replicated engines, and pure-Postgres sessions never write to these+ # tables at all. Rather than parsing table names out of the statements, construct the+ # expected statement from each empty table's name (the two forms our TRUNCATE_*_SQL+ # constants produce) and exact-match. Fail-safe: any statement that doesn't match —+ # ON CLUSTER clause, unexpected quoting, unknown total_rows (NULL for non-MergeTree+ # engines) — is kept and truncated as before.+ empty_table_truncates = {+ form+ for (name,) in sync_execute(+ "SELECT name FROM system.tables WHERE database = %(database)s AND total_rows = 0",+ {"database": settings.CLICKHOUSE_DATABASE},+ )+ for form in (+ f"TRUNCATE TABLE IF EXISTS {name}",+ f"TRUNCATE TABLE IF EXISTS `{settings.CLICKHOUSE_DATABASE}`.`{name}`",+ )+ }+ TABLES_TO_CREATE_DROP = [q for q in TABLES_TO_CREATE_DROP if q.strip() not in empty_table_truncates]+run_clickhouse_statement_in_parallel(TABLES_TO_CREATE_DROP)from posthog.clickhouse.schema import CREATE_DATA_QUERIESdiff --git a/posthog/management/commands/test/test_set_recorder_script.py b/posthog/management/commands/test/test_set_recorder_script.pyindex 3b02d9acc9da..879b3a24aa82 100644--- a/posthog/management/commands/test/test_set_recorder_script.py+++ b/posthog/management/commands/test/test_set_recorder_script.py@@ -1,6 +1,7 @@from io import StringIOfrom posthog.test.base import BaseTest+from unittest import mockfrom django.core.management import call_command@@ -139,12 +140,23 @@ class TestSetRecorderScriptCommand(BaseTest):)out = StringIO()- call_command(- "set_recorder_script",- "--script=test-recorder",- "--sample-rate=1.0",- stdout=out,- )+ # team.save() fans out to unrelated post_save receivers that each do per-team work:+ # the team-token cache write runs a full DRF serialization plus a cache write, and+ # hog function / hog flow refresh each run their own DB query. Irrelevant to this+ # command's batching behaviour, just 2500x avoidable overhead each — mock them out+ # like any other unrelated side effect that isn't under test. With these mocked,+ # each save is a single UPDATE, which is this test's floor.+ with (+ mock.patch("products.cdp.backend.tasks.hog_functions.refresh_affected_hog_functions.delay"),+ mock.patch("products.workflows.backend.tasks.hog_flows.refresh_affected_hog_flows.delay"),+ mock.patch("posthog.models.team.team.set_team_in_cache"),+ ):+ call_command(+ "set_recorder_script",+ "--script=test-recorder",+ "--sample-rate=1.0",+ stdout=out,+ )output = out.getvalue()assert "Updated 1000 teams so far..." in output