Skip to content

Commit e1e9332

Browse files
committed
perf: reduce logging and optimize edge worker performance tests (#496)
# Reduce logging verbosity and improve performance test output This PR makes several improvements to the edge worker's logging and test output: 1. Changes the default log level from `debug` to `warn` in the environment configuration 2. Adds a `debug` flag to worker payloads to make logging optional 3. Improves the test output in CI environments by: - Only showing progress bars in TTY environments - Adding percentage-based progress reporting for non-TTY environments - Including performance metrics (jobs/second) in test output 4. Reduces the performance test message count from 20,000 to 2,000 for faster test runs These changes make the tests more efficient and provide better visibility into performance metrics while reducing unnecessary log output.
1 parent 8a52fc3 commit e1e9332

5 files changed

Lines changed: 64 additions & 31 deletions

File tree

pkgs/edge-worker/supabase/functions/.env

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ API_URL="http://127.0.0.1:50321"
22
DB_URL="postgresql://postgres:postgres@127.0.0.1:50322/postgres"
33
DB_POOL_URL="postgresql://postgres.pooler-dev:postgres@pooler:6543/postgres"
44
DB_POOL_EXTERNAL_URL="postgresql://postgres.pooler-dev:postgres@localhost:50329/postgres"
5-
EDGE_WORKER_LOG_LEVEL=debug
5+
EDGE_WORKER_LOG_LEVEL=warn
66

77
DB_PORT=50322
88
DB_POOL_PORT=50329

pkgs/edge-worker/supabase/functions/cpu_intensive/index.ts

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,27 @@ import { EdgeWorker } from '@pgflow/edge-worker';
22
import { crypto } from 'jsr:@std/crypto';
33
import { sql } from '../utils.ts';
44

5-
async function cpuIntensiveTask() {
5+
async function cpuIntensiveTask(payload: { debug?: boolean }) {
66
let data = new TextEncoder().encode('burn');
77
const timeId = `cpu_intensive_${Math.random()}`;
8-
console.time(timeId);
8+
9+
if (payload.debug) {
10+
console.time(timeId);
11+
}
12+
913
for (let i = 0; i < 10000; i++) {
1014
data = new Uint8Array(await crypto.subtle.digest('SHA-256', data));
1115
}
12-
console.timeEnd(timeId);
1316

14-
console.log(
15-
'[cpu_intensive] last_val = ',
16-
await sql`SELECT nextval('test_seq')`
17-
);
17+
if (payload.debug) {
18+
console.timeEnd(timeId);
19+
console.log(
20+
'[cpu_intensive] last_val = ',
21+
await sql`SELECT nextval('test_seq')`
22+
);
23+
} else {
24+
await sql`SELECT nextval('test_seq')`;
25+
}
1826
}
1927

2028
EdgeWorker.start(cpuIntensiveTask, { queueName: 'cpu_intensive' });

pkgs/edge-worker/supabase/functions/max_concurrency/index.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
11
import { EdgeWorker } from '@pgflow/edge-worker';
22
import { sleep, sql } from '../utils.ts';
33

4-
async function incrementSeq() {
4+
async function incrementSeq(payload: { debug?: boolean }) {
55
await sleep(50);
66

7-
console.log(
8-
'[max_concurrency] last_val =',
9-
await sql`SELECT nextval('test_seq')`
10-
);
7+
if (payload.debug) {
8+
console.log(
9+
'[max_concurrency] last_val =',
10+
await sql`SELECT nextval('test_seq')`
11+
);
12+
} else {
13+
await sql`SELECT nextval('test_seq')`;
14+
}
1115
}
1216

1317
EdgeWorker.start(incrementSeq, {

pkgs/edge-worker/tests/e2e/_helpers.ts

Lines changed: 37 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,16 @@ import ProgressBar from 'jsr:@deno-library/progress';
44
import { dim } from 'https://deno.land/std@0.224.0/fmt/colors.ts';
55
import { e2eConfig } from '../config.ts';
66

7+
const DEBUG = Deno.env.get('DEBUG') === '1' || Deno.env.get('VERBOSE') === '1';
8+
79
interface WaitForOptions {
810
pollIntervalMs?: number;
911
timeoutMs?: number;
1012
description?: string;
1113
}
1214

1315
export function log(message: string, ...args: unknown[]) {
14-
console.log(dim(` -> ${message}`), ...args);
16+
if (DEBUG) console.log(dim(` -> ${message}`), ...args);
1517
}
1618

1719
export async function waitFor<T>(
@@ -41,14 +43,15 @@ export async function waitFor<T>(
4143
}
4244
}
4345

44-
export async function sendBatch(count: number, queueName: string) {
46+
export async function sendBatch(count: number, queueName: string, debug = false) {
4547
const sql = createSql();
48+
const payload = JSON.stringify({ debug });
4649
try {
4750
return await sql`
4851
SELECT pgmq.send_batch(
4952
${queueName},
5053
ARRAY(
51-
SELECT '{}'::jsonb
54+
SELECT ${payload}::jsonb
5255
FROM generate_series(1, ${count}::integer)
5356
)
5457
)`;
@@ -87,28 +90,41 @@ export async function waitForSeqToIncrementBy(
8790
options: WaitForSeqValueOptions = {}
8891
): Promise<number> {
8992
const { seqName = 'test_seq' } = options;
93+
const isTTY = Deno.stdout.isTerminal();
9094

91-
const perSecond = 0;
92-
93-
const progress = new ProgressBar({
94-
title: `${seqName} (${perSecond}/s)`,
95-
total: value,
96-
width: 20,
97-
display: dim(
98-
` -> incrementing "${seqName}": :completed/:total (:eta left) [:bar] :percent`
99-
),
100-
prettyTime: true,
101-
});
95+
const progress = isTTY
96+
? new ProgressBar({
97+
title: `${seqName}`,
98+
total: value,
99+
width: 20,
100+
display: dim(
101+
` -> incrementing "${seqName}": :completed/:total (:eta left) [:bar] :percent`
102+
),
103+
prettyTime: true,
104+
})
105+
: null;
102106

103107
const startVal = await seqLastValue(seqName);
108+
const startTime = Date.now();
104109
let lastVal = startVal;
110+
let lastReportedPercent = 0;
105111

106112
try {
107113
return await waitFor(
108114
async () => {
109115
lastVal = await seqLastValue(seqName);
110-
progress.render(lastVal);
111116
const incrementedBy = lastVal - startVal;
117+
const percent = Math.floor((incrementedBy / value) * 100);
118+
119+
if (progress) {
120+
progress.render(incrementedBy);
121+
} else if (percent >= lastReportedPercent + 10) {
122+
// In CI, report every 10% with average speed
123+
const elapsedSec = (Date.now() - startTime) / 1000;
124+
const avgPerSec = Math.round(incrementedBy / elapsedSec);
125+
console.log(dim(` -> ${seqName}: ${percent}% (${incrementedBy}/${value}) - ${avgPerSec}/s avg`));
126+
lastReportedPercent = percent;
127+
}
112128

113129
return incrementedBy >= value ? lastVal : false;
114130
},
@@ -118,7 +134,12 @@ export async function waitForSeqToIncrementBy(
118134
}
119135
);
120136
} finally {
121-
progress.end();
137+
const totalSec = (Date.now() - startTime) / 1000;
138+
const avgPerSec = Math.round(value / totalSec);
139+
if (progress) {
140+
progress.end();
141+
}
142+
console.log(dim(` -> ${seqName}: 100% complete in ${totalSec.toFixed(1)}s (${avgPerSec}/s avg)`));
122143
}
123144
}
124145

pkgs/edge-worker/tests/e2e/performance.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@ import {
77
log,
88
} from './_helpers.ts';
99

10-
const MESSAGES_TO_SEND = 20000;
10+
const MESSAGES_TO_SEND = 2000;
1111
const WORKER_NAME = 'max_concurrency';
1212

1313
Deno.test(
1414
{
15-
name: 'worker can handle tens of thousands of jobs queued at once',
15+
name: 'worker can handle thousands of jobs queued at once',
1616
sanitizeOps: false, // Progress bar uses async writes that don't complete before test ends
1717
sanitizeResources: false,
1818
},

0 commit comments

Comments
 (0)