Skip to content

: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.

chore(ci): balance node.js serial jest shards by duration (#128491)

Georges-Antoine Assiadae189d1f67
diff --git a/.github/workflows/ci-nodejs.yml b/.github/workflows/ci-nodejs.yml
index 9fb3445843c1..7cffa742899f 100644
--- a/.github/workflows/ci-nodejs.yml
+++ b/.github/workflows/ci-nodejs.yml
@@ -180,6 +180,8 @@ jobs:
- '.github/scripts/jest-retries.cjs'
- 'patches/[email protected]'
- 'nodejs/jest*.config.js'
+ - 'nodejs/jest.serial.sequencer.js'
+ - 'nodejs/jest.serial.durations.json'
- 'nodejs/package.json'
- 'nodejs/tsconfig*.json'
- 'pnpm-lock.yaml'
diff --git a/nodejs/jest.serial.config.js b/nodejs/jest.serial.config.js
index 69ebe26ea6d7..8819e4f81f03 100644
--- a/nodejs/jest.serial.config.js
+++ b/nodejs/jest.serial.config.js
@@ -3,4 +3,5 @@ const config = require('./jest.config.shared')
module.exports = {
...config,
testMatch: ['<rootDir>/tests/**/*.serial.test.ts', '<rootDir>/src/**/*.serial.test.ts'],
+ testSequencer: '<rootDir>/jest.serial.sequencer.js',
}
diff --git a/nodejs/jest.serial.durations.json b/nodejs/jest.serial.durations.json
new file mode 100644
index 000000000000..8ced41b98837
--- /dev/null
+++ b/nodejs/jest.serial.durations.json
@@ -0,0 +1,21 @@
+{
+ "src/cdp/cdp-api.serial.test.ts": 32,
+ "src/cdp/cdp-dlq-replay-e2e.serial.test.ts": 39,
+ "src/cdp/cdp-e2e.serial.test.ts": 16,
+ "src/cdp/rerun/rerun-e2e.serial.test.ts": 9,
+ "src/cdp/services/cohort-membership/sweeper.service.serial.test.ts": 1,
+ "src/cdp/services/hogflows/realtime-cohort-audience.service.serial.test.ts": 0,
+ "src/cdp/workflows-e2e.serial.test.ts": 96,
+ "src/common/kafka/consumer/consumer-v2.rebalance.integration.serial.test.ts": 28,
+ "src/ingestion/ingestion-consumer.serial.test.ts": 16,
+ "src/logs/logs-ingestion-thread-relief.serial.test.ts": 2,
+ "src/logs/transformations/benchmarks/hogvm-log-bench.serial.test.ts": 1,
+ "src/logs/transformations/benchmarks/transformer-log-bench.serial.test.ts": 1,
+ "tests/ingestion-general-server.serial.test.ts": 5,
+ "tests/ingestion-logs-server.serial.test.ts": 2,
+ "tests/ingestion-session-replay-server.serial.test.ts": 2,
+ "tests/ingestion-traces-server.serial.test.ts": 2,
+ "tests/ingestion/person-state-batch.serial.test.ts": 15,
+ "tests/server.serial.test.ts": 3,
+ "tests/session-replay/recording-api/recording-api.integration.serial.test.ts": 2
+}
diff --git a/nodejs/jest.serial.sequencer.js b/nodejs/jest.serial.sequencer.js
new file mode 100644
index 000000000000..5bc544828e6d
--- /dev/null
+++ b/nodejs/jest.serial.sequencer.js
@@ -0,0 +1,63 @@
+const Sequencer = require('@jest/test-sequencer').default
+const path = require('path')
+
+function loadDurations() {
+ try {
+ return require('./jest.serial.durations.json')
+ } catch {
+ return null
+ }
+}
+
+function relativePath(test) {
+ return path.relative(test.context.config.rootDir, test.path).split(path.sep).join('/')
+}
+
+function median(values) {
+ if (values.length === 0) {
+ return 1
+ }
+ const sorted = [...values].sort((a, b) => a - b)
+ return sorted[Math.floor(sorted.length / 2)]
+}
+
+function lightestShard(totals) {
+ let lightest = 0
+ for (let i = 1; i < totals.length; i++) {
+ if (totals[i] < totals[lightest]) {
+ lightest = i
+ }
+ }
+ return lightest
+}
+
+function balancedShard(tests, shardIndex, shardCount, durations) {
+ const fallback = median(Object.values(durations))
+ const weighted = tests
+ .map((test) => {
+ const rel = relativePath(test)
+ return { test, rel, duration: durations[rel] ?? fallback }
+ })
+ .sort((a, b) => b.duration - a.duration || (a.rel < b.rel ? -1 : a.rel > b.rel ? 1 : 0))
+
+ const totals = new Array(shardCount).fill(0)
+ const shards = Array.from({ length: shardCount }, () => [])
+ for (const { test, duration } of weighted) {
+ const target = lightestShard(totals)
+ shards[target].push(test)
+ totals[target] += duration
+ }
+ return shards[shardIndex - 1]
+}
+
+class DurationBalancedSequencer extends Sequencer {
+ shard(tests, options) {
+ const durations = loadDurations()
+ if (!durations) {
+ return super.shard(tests, options)
+ }
+ return balancedShard(tests, options.shardIndex, options.shardCount, durations)
+ }
+}
+
+module.exports = DurationBalancedSequencer
diff --git a/nodejs/package.json b/nodejs/package.json
index ad8e54855e7f..cf1b02cc17d9 100644
--- a/nodejs/package.json
+++ b/nodejs/package.json
@@ -182,6 +182,7 @@
"@types/node-schedule": "^2.1.0",
"@types/nodemailer": "^8.0.0",
"@types/pg": "~8.6.6",
+ "@jest/test-sequencer": "^30.0.5",
"@types/redlock": "^4.0.1",
"@types/snowflake-sdk": "^1.5.1",
"@types/supertest": "^6.0.2",
diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml
index 4a7c205ee397..0a5f8b50a0d1 100644
--- a/pnpm-lock.yaml
+++ b/pnpm-lock.yaml
@@ -1880,6 +1880,9 @@ importers:
'@eslint-community/eslint-plugin-eslint-comments':
specifier: ^4.4.1
+ '@jest/test-sequencer':
+ specifier: ^30.0.5
+ version: 30.0.5
'@segment/actions-core':
specifier: ^3.151.0
version: 3.153.0([email protected])