From ea2e3b76fbb78dba484337b7bf3d609de9fe3f3e Mon Sep 17 00:00:00 2001 From: loks0n <22452787+loks0n@users.noreply.github.com> Date: Fri, 26 Jun 2026 16:17:17 +0100 Subject: [PATCH 1/2] Refocus benchmark on database load, not password hashing The per-iteration account flow ran three CPU-bound password hashing operations (account create, session create, password update) that saturated CPU and masked database performance. Move authentication into setup(): pre-create one signed-in user per VU once (via setupApi, excluded from metrics) and reuse the session across iterations. The account flow now only exercises DB-bound account reads/writes, and the rows flow creates a configurable batch (APPWRITE_BENCHMARK_ROWS, default 5) so database requests dominate the measured mix. Co-Authored-By: Claude Opus 4.8 (1M context) --- tests/benchmarks/http.js | 110 +++++++++++++++++++++------------------ 1 file changed, 60 insertions(+), 50 deletions(-) diff --git a/tests/benchmarks/http.js b/tests/benchmarks/http.js index e809372a4f..1b340b1887 100644 --- a/tests/benchmarks/http.js +++ b/tests/benchmarks/http.js @@ -20,6 +20,7 @@ const PASSWORD = __ENV.APPWRITE_BENCHMARK_PASSWORD || 'Password123!'; const WORKER_TIMEOUT_MS = Number(__ENV.APPWRITE_WORKER_TIMEOUT_MS || 120000); const VUS = Number(__ENV.APPWRITE_BENCHMARK_VUS || 1); const DURATION = __ENV.APPWRITE_BENCHMARK_DURATION || '1m'; +const ROWS_PER_FLOW = Number(__ENV.APPWRITE_BENCHMARK_ROWS || 5); const SUMMARY_PATH = __ENV.APPWRITE_BENCHMARK_SUMMARY_PATH || '/tmp/appwrite-k6-summary.json'; const PREVIOUS_SUMMARY_PATH = __ENV.APPWRITE_BENCHMARK_PREVIOUS_SUMMARY_PATH || ''; const PREVIOUS_SUMMARY = PREVIOUS_SUMMARY_PATH ? loadPreviousSummary(PREVIOUS_SUMMARY_PATH) : null; @@ -172,6 +173,7 @@ export function setup() { }, apiHeaders, [201, 409], 'setup.project.platforms.web.create'); const tablesDb = setupTablesDb(apiHeaders); + const users = setupUsers(projectId, VUS); return { runId, @@ -181,10 +183,37 @@ export function setup() { tableId: tablesDb.tableId, consoleSessionHeaders, apiHeaders, + users, platformStatus: platform.status, }; } +// Pre-create one signed-in user per VU so password hashing happens once +// during setup (excluded from metrics) instead of in every iteration. +function setupUsers(projectId, count) { + const headers = projectHeaders(projectId); + const users = []; + + for (let index = 0; index < count; index++) { + const email = `bench-user-${unique('mail')}@example.com`; + setupApi('POST', '/account', { + userId: unique('user'), + email, + password: PASSWORD, + name: 'Benchmark User', + }, headers, [201], 'setup.account.create'); + + const session = setupApi('POST', '/account/sessions/email', { + email, + password: PASSWORD, + }, headers, [201], 'setup.account.session'); + + users.push({ sessionHeaders: { ...headers, Cookie: cookieHeader(session) } }); + } + + return users; +} + function setupTablesDb(apiHeaders) { const databaseId = unique('tdb'); const tableId = unique('tbl'); @@ -218,7 +247,8 @@ function setupTablesDb(apiHeaders) { } export function curatedFlows(data) { - const ctx = { ...data }; + const user = data.users[(__VU - 1) % data.users.length]; + const ctx = { ...data, sessionHeaders: user.sessionHeaders }; try { group('account flow', () => accountFlow(ctx)); @@ -242,62 +272,42 @@ export function teardown(data) { } function accountFlow(ctx) { - const userId = unique('user'); - const email = `bench-user-${unique('mail')}@example.com`; - const headers = projectHeaders(ctx.projectId); + requireSession(ctx, 'accountFlow'); - api('POST', '/account', { - userId, - email, - password: PASSWORD, - name: 'Benchmark User', - }, headers, [201], 'account.create'); - - const session = api('POST', '/account/sessions/email', { - email, - password: PASSWORD, - }, headers, [201], 'account.sessions.email.create'); - - const sessionHeaders = { - ...headers, - Cookie: cookieHeader(session), - }; - - ctx.userId = userId; - ctx.userEmail = email; - ctx.sessionHeaders = sessionHeaders; - - api('GET', '/account', null, sessionHeaders, [200], 'account.get'); - api('GET', '/account/logs', null, sessionHeaders, [200], 'account.logs.list'); - api('PATCH', '/account/prefs', { prefs: { benchmark: true, runId: ctx.runId } }, sessionHeaders, [200], 'account.prefs.update'); - api('PATCH', '/account/name', { name: 'Benchmark User Updated' }, sessionHeaders, [200], 'account.name.update'); - api('PATCH', '/account/password', { password: `${PASSWORD}2`, oldPassword: PASSWORD }, sessionHeaders, [200], 'account.password.update'); + api('GET', '/account', null, ctx.sessionHeaders, [200], 'account.get'); + api('GET', '/account/logs', null, ctx.sessionHeaders, [200], 'account.logs.list'); + api('PATCH', '/account/prefs', { prefs: { benchmark: true, runId: ctx.runId } }, ctx.sessionHeaders, [200], 'account.prefs.update'); + api('PATCH', '/account/name', { name: 'Benchmark User Updated' }, ctx.sessionHeaders, [200], 'account.name.update'); } function tablesDbFlow(ctx) { requireSession(ctx, 'tablesDbFlow'); - const databaseId = ctx.databaseId; - const tableId = ctx.tableId; - const rowId = unique('row'); + const { databaseId, tableId, sessionHeaders } = ctx; + const base = `/tablesdb/${databaseId}/tables/${tableId}/rows`; + const rowIds = []; - api('POST', `/tablesdb/${databaseId}/tables/${tableId}/rows`, { - rowId, - data: tablePayload(), - permissions: ITEM_PERMISSIONS, - }, ctx.sessionHeaders, [201], 'tablesdb.rows.create'); - api('GET', `/tablesdb/${databaseId}/tables/${tableId}/rows`, null, ctx.sessionHeaders, [200], 'tablesdb.rows.list'); - api('GET', `/tablesdb/${databaseId}/tables/${tableId}/rows/${rowId}`, null, ctx.sessionHeaders, [200], 'tablesdb.rows.get'); - api('PATCH', `/tablesdb/${databaseId}/tables/${tableId}/rows/${rowId}`, { - data: { title: 'Benchmark Row Updated' }, - }, ctx.sessionHeaders, [200], 'tablesdb.rows.update'); - api('PATCH', `/tablesdb/${databaseId}/tables/${tableId}/rows/${rowId}/quantity/increment`, { - value: 1, - }, ctx.sessionHeaders, [200], 'tablesdb.rows.increment'); - api('PATCH', `/tablesdb/${databaseId}/tables/${tableId}/rows/${rowId}/quantity/decrement`, { - value: 1, - }, ctx.sessionHeaders, [200], 'tablesdb.rows.decrement'); - api('DELETE', `/tablesdb/${databaseId}/tables/${tableId}/rows/${rowId}`, null, ctx.sessionHeaders, [204], 'tablesdb.rows.delete'); + for (let index = 0; index < ROWS_PER_FLOW; index++) { + const rowId = unique('row'); + rowIds.push(rowId); + api('POST', base, { + rowId, + data: tablePayload(), + permissions: ITEM_PERMISSIONS, + }, sessionHeaders, [201], 'tablesdb.rows.create'); + } + + api('GET', base, null, sessionHeaders, [200], 'tablesdb.rows.list'); + + for (const rowId of rowIds) { + api('GET', `${base}/${rowId}`, null, sessionHeaders, [200], 'tablesdb.rows.get'); + api('PATCH', `${base}/${rowId}`, { + data: { title: 'Benchmark Row Updated' }, + }, sessionHeaders, [200], 'tablesdb.rows.update'); + api('PATCH', `${base}/${rowId}/quantity/increment`, { value: 1 }, sessionHeaders, [200], 'tablesdb.rows.increment'); + api('PATCH', `${base}/${rowId}/quantity/decrement`, { value: 1 }, sessionHeaders, [200], 'tablesdb.rows.decrement'); + api('DELETE', `${base}/${rowId}`, null, sessionHeaders, [204], 'tablesdb.rows.delete'); + } } function storageFlow(ctx) { From e29146f9f66cf66ed70d5c610cd374121afd933b Mon Sep 17 00:00:00 2001 From: loks0n <22452787+loks0n@users.noreply.github.com> Date: Fri, 26 Jun 2026 16:31:40 +0100 Subject: [PATCH 2/2] Clear cookie jar between benchmark user setups setup() runs in a single iteration, so k6 does not reset the per-VU cookie jar between the users we create. The second user's session request carried the first user's session cookie, so the server rejected it with "session already active" (401). Clear the jar after capturing each user's cookie; flows authenticate via explicit Cookie headers, so clearing the jar is safe. Co-Authored-By: Claude Opus 4.8 (1M context) --- tests/benchmarks/http.js | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/tests/benchmarks/http.js b/tests/benchmarks/http.js index 1b340b1887..a41b4778df 100644 --- a/tests/benchmarks/http.js +++ b/tests/benchmarks/http.js @@ -209,6 +209,10 @@ function setupUsers(projectId, count) { }, headers, [201], 'setup.account.session'); users.push({ sessionHeaders: { ...headers, Cookie: cookieHeader(session) } }); + + // Drop the auto-stored session cookie so the next user can sign in; + // setup() runs in a single iteration, so the jar is not reset for us. + http.cookieJar().clear(ENDPOINT); } return users;