|
| 1 | +import http from 'k6/http'; |
| 2 | +import exec from 'k6/execution'; |
| 3 | +import { check } from 'k6'; |
| 4 | +import { Rate, Trend } from 'k6/metrics'; |
| 5 | + |
| 6 | +const BASE_URL = __ENV.BASE_URL || 'http://localhost:8080'; |
| 7 | +const USER_PREFIX = __ENV.USER_PREFIX || 'perfdel'; |
| 8 | +const USER_DOMAIN = __ENV.USER_DOMAIN || 'test.com'; |
| 9 | +const USER_PASSWORD = __ENV.USER_PASSWORD || 'Testtest1'; |
| 10 | +const USER_COUNT = Number(__ENV.USER_COUNT || 50); |
| 11 | +const DOCS_PER_USER = Number(__ENV.DOCS_PER_USER || 3); |
| 12 | +const RUN_ID = __ENV.RUN_ID; |
| 13 | + |
| 14 | +if (!RUN_ID) { |
| 15 | + throw new Error('RUN_ID is required. Use the same RUN_ID that was used in seed_dataset.js'); |
| 16 | +} |
| 17 | + |
| 18 | +const TOTAL_DOCS = USER_COUNT * DOCS_PER_USER; |
| 19 | + |
| 20 | +export const options = { |
| 21 | + scenarios: { |
| 22 | + delete_only: { |
| 23 | + executor: 'shared-iterations', |
| 24 | + vus: Number(__ENV.DELETE_VUS || 30), |
| 25 | + iterations: TOTAL_DOCS, |
| 26 | + maxDuration: __ENV.DELETE_MAX_DURATION || '20m', |
| 27 | + }, |
| 28 | + }, |
| 29 | + thresholds: { |
| 30 | + http_req_failed: ['rate<0.02'], |
| 31 | + delete_failed: ['rate<0.02'], |
| 32 | + op_doc_delete_ms: ['p(95)<3000'], |
| 33 | + }, |
| 34 | +}; |
| 35 | + |
| 36 | +const deleteFailed = new Rate('delete_failed'); |
| 37 | +const tLogin = new Trend('op_login_ms'); |
| 38 | +const tSearch = new Trend('op_doc_search_ms'); |
| 39 | +const tDelete = new Trend('op_doc_delete_ms'); |
| 40 | + |
| 41 | +function pad3(n) { |
| 42 | + return String(n).padStart(3, '0'); |
| 43 | +} |
| 44 | + |
| 45 | +function userEmail(userNo) { |
| 46 | + return `${USER_PREFIX}_u${pad3(userNo)}@${USER_DOMAIN}`; |
| 47 | +} |
| 48 | + |
| 49 | +function headers(cookie) { |
| 50 | + return { |
| 51 | + headers: { |
| 52 | + 'Content-Type': 'application/json', |
| 53 | + Cookie: cookie, |
| 54 | + }, |
| 55 | + }; |
| 56 | +} |
| 57 | + |
| 58 | +function ensureStatus(res, statuses, op) { |
| 59 | + const ok = check(res, { |
| 60 | + [`${op} status`]: (r) => statuses.includes(r.status), |
| 61 | + }); |
| 62 | + deleteFailed.add(!ok, { op }); |
| 63 | + if (!ok) { |
| 64 | + const snippet = typeof res.body === 'string' ? res.body.slice(0, 240) : ''; |
| 65 | + throw new Error(`${op} failed status=${res.status}, body=${snippet}`); |
| 66 | + } |
| 67 | +} |
| 68 | + |
| 69 | +function parseJson(res, op) { |
| 70 | + try { |
| 71 | + return res.json(); |
| 72 | + } catch (_e) { |
| 73 | + throw new Error(`${op} invalid JSON`); |
| 74 | + } |
| 75 | +} |
| 76 | + |
| 77 | +function login(email) { |
| 78 | + const res = http.post( |
| 79 | + `${BASE_URL}/api/user/login`, |
| 80 | + JSON.stringify({ email, password: USER_PASSWORD }), |
| 81 | + { headers: { 'Content-Type': 'application/json' }, tags: { op: 'delete_login' } }, |
| 82 | + ); |
| 83 | + tLogin.add(res.timings.duration); |
| 84 | + ensureStatus(res, [200], 'delete_login'); |
| 85 | + |
| 86 | + const jsession = res.cookies.JSESSIONID && res.cookies.JSESSIONID[0]; |
| 87 | + if (!jsession || !jsession.value) { |
| 88 | + throw new Error('delete_login missing JSESSIONID'); |
| 89 | + } |
| 90 | + return `JSESSIONID=${jsession.value}`; |
| 91 | +} |
| 92 | + |
| 93 | +function findDocIdByTitle(cookie, title) { |
| 94 | + const url = `${BASE_URL}/api/document/search?keyword=${encodeURIComponent(title)}&page=0&size=10&sort=updatedAt&order=desc`; |
| 95 | + const res = http.get(url, { ...headers(cookie), tags: { op: 'delete_doc_search' } }); |
| 96 | + tSearch.add(res.timings.duration); |
| 97 | + ensureStatus(res, [200], 'delete_doc_search'); |
| 98 | + |
| 99 | + const body = parseJson(res, 'delete_doc_search'); |
| 100 | + const content = Array.isArray(body.content) ? body.content : []; |
| 101 | + const exact = content.find((d) => d && d.title === title); |
| 102 | + if (!exact || !exact.id) { |
| 103 | + throw new Error(`target doc not found by title=${title}`); |
| 104 | + } |
| 105 | + return exact.id; |
| 106 | +} |
| 107 | + |
| 108 | +function deleteDoc(cookie, docId) { |
| 109 | + const res = http.del( |
| 110 | + `${BASE_URL}/api/document/${docId}`, |
| 111 | + null, |
| 112 | + { ...headers(cookie), tags: { op: 'delete_doc' } }, |
| 113 | + ); |
| 114 | + tDelete.add(res.timings.duration); |
| 115 | + ensureStatus(res, [204], 'delete_doc'); |
| 116 | +} |
| 117 | + |
| 118 | +export default function () { |
| 119 | + const it = exec.scenario.iterationInTest; |
| 120 | + const userNo = Math.floor(it / DOCS_PER_USER) + 1; |
| 121 | + const docNo = (it % DOCS_PER_USER) + 1; |
| 122 | + |
| 123 | + const email = userEmail(userNo); |
| 124 | + const cookie = login(email); |
| 125 | + |
| 126 | + const key = `${RUN_ID}u${pad3(userNo)}d${pad3(docNo)}`; |
| 127 | + const title = `PDEL-${key}`; |
| 128 | + |
| 129 | + const docId = findDocIdByTitle(cookie, title); |
| 130 | + deleteDoc(cookie, docId); |
| 131 | +} |
| 132 | + |
| 133 | +export function handleSummary(data) { |
| 134 | + return { |
| 135 | + stdout: `\n[delete-only] run_id=${RUN_ID}, users=${USER_COUNT}, docs_per_user=${DOCS_PER_USER}, total_docs=${TOTAL_DOCS}\n`, |
| 136 | + 'perf/delete/results/delete_only_summary.json': JSON.stringify(data, null, 2), |
| 137 | + }; |
| 138 | +} |
0 commit comments