|
| 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 || 'perfuser'; |
| 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 || 5); |
| 12 | +const RUN_ID = __ENV.RUN_ID; |
| 13 | +const RESULT_DIR = __ENV.RESULT_DIR || 'perf/read/results'; |
| 14 | +const TITLE_PREFIX = __ENV.TITLE_PREFIX || 'PDEL'; |
| 15 | +const DOC_LIST_PAGE_SIZE = Number(__ENV.DOC_LIST_PAGE_SIZE || Math.max(DOCS_PER_USER * 2, 20)); |
| 16 | +const DOC_LIST_MAX_PAGES = Number(__ENV.DOC_LIST_MAX_PAGES || 20); |
| 17 | + |
| 18 | +if (!RUN_ID) { |
| 19 | + throw new Error('RUN_ID is required. Use the same RUN_ID that was used for the graph dataset seed'); |
| 20 | +} |
| 21 | + |
| 22 | +export const options = { |
| 23 | + summaryTrendStats: ['min', 'med', 'avg', 'p(90)', 'p(95)', 'p(99)', 'max'], |
| 24 | + scenarios: { |
| 25 | + graph_read: { |
| 26 | + executor: 'constant-vus', |
| 27 | + vus: Number(__ENV.GRAPH_VUS || 10), |
| 28 | + duration: __ENV.GRAPH_DURATION || '30s', |
| 29 | + exec: 'runGraphRead', |
| 30 | + }, |
| 31 | + }, |
| 32 | + thresholds: { |
| 33 | + http_req_failed: ['rate<0.02'], |
| 34 | + graph_failed: ['rate<0.02'], |
| 35 | + }, |
| 36 | +}; |
| 37 | + |
| 38 | +const graphFailed = new Rate('graph_failed'); |
| 39 | +const tLogin = new Trend('graph_login_ms'); |
| 40 | +const tGraph = new Trend('op_doc_graph_ms'); |
| 41 | +const tGraphPayload = new Trend('op_doc_graph_payload_bytes'); |
| 42 | +const tGraphCommitCount = new Trend('op_doc_graph_commit_count'); |
| 43 | +const tGraphEdgeCount = new Trend('op_doc_graph_edge_count'); |
| 44 | +const tGraphBranchCount = new Trend('op_doc_graph_branch_count'); |
| 45 | + |
| 46 | +const cookieCache = new Map(); |
| 47 | + |
| 48 | +function pad3(n) { |
| 49 | + return String(n).padStart(3, '0'); |
| 50 | +} |
| 51 | + |
| 52 | +function userEmail(userNo) { |
| 53 | + return `${USER_PREFIX}_u${pad3(userNo)}@${USER_DOMAIN}`; |
| 54 | +} |
| 55 | + |
| 56 | +function expectedTitlePrefix(userNo) { |
| 57 | + if (TITLE_PREFIX === 'PDEL') { |
| 58 | + return `PDEL-${RUN_ID}u${pad3(userNo)}d`; |
| 59 | + } |
| 60 | + return `PERF-${RUN_ID}-u${pad3(userNo)}-d`; |
| 61 | +} |
| 62 | + |
| 63 | +function headers(cookie) { |
| 64 | + return { |
| 65 | + headers: { |
| 66 | + Cookie: cookie, |
| 67 | + }, |
| 68 | + }; |
| 69 | +} |
| 70 | + |
| 71 | +function ensureStatus(res, statuses, op) { |
| 72 | + const ok = check(res, { |
| 73 | + [`${op} status`]: (r) => statuses.includes(r.status), |
| 74 | + }); |
| 75 | + graphFailed.add(!ok, { op }); |
| 76 | + if (!ok) { |
| 77 | + const snippet = typeof res.body === 'string' ? res.body.slice(0, 240) : ''; |
| 78 | + throw new Error(`${op} failed status=${res.status}, body=${snippet}`); |
| 79 | + } |
| 80 | +} |
| 81 | + |
| 82 | +function parseJson(res, op) { |
| 83 | + try { |
| 84 | + return res.json(); |
| 85 | + } catch (_e) { |
| 86 | + throw new Error(`${op} invalid JSON`); |
| 87 | + } |
| 88 | +} |
| 89 | + |
| 90 | +function login(email) { |
| 91 | + const cached = cookieCache.get(email); |
| 92 | + if (cached) { |
| 93 | + return cached; |
| 94 | + } |
| 95 | + |
| 96 | + http.cookieJar().clear(BASE_URL); |
| 97 | + const res = http.post( |
| 98 | + `${BASE_URL}/api/user/login`, |
| 99 | + JSON.stringify({ email, password: USER_PASSWORD }), |
| 100 | + { headers: { 'Content-Type': 'application/json' }, tags: { op: 'graph_login' } }, |
| 101 | + ); |
| 102 | + tLogin.add(res.timings.duration); |
| 103 | + ensureStatus(res, [200], 'graph_login'); |
| 104 | + |
| 105 | + const jsession = res.cookies.JSESSIONID && res.cookies.JSESSIONID[0]; |
| 106 | + if (!jsession || !jsession.value) { |
| 107 | + throw new Error('graph_login missing JSESSIONID'); |
| 108 | + } |
| 109 | + |
| 110 | + const cookie = `JSESSIONID=${jsession.value}`; |
| 111 | + cookieCache.set(email, cookie); |
| 112 | + return cookie; |
| 113 | +} |
| 114 | + |
| 115 | +function requestGraph(cookie, docId) { |
| 116 | + const res = http.get( |
| 117 | + `${BASE_URL}/api/document/${docId}/graph`, |
| 118 | + { ...headers(cookie), tags: { op: 'doc_graph' } }, |
| 119 | + ); |
| 120 | + tGraph.add(res.timings.duration); |
| 121 | + tGraphPayload.add(typeof res.body === 'string' ? res.body.length : 0); |
| 122 | + ensureStatus(res, [200], 'doc_graph'); |
| 123 | + |
| 124 | + const body = parseJson(res, 'doc_graph'); |
| 125 | + const valid = check(body, { |
| 126 | + 'doc_graph has branches': (r) => Array.isArray(r.branches) && r.branches.length > 0, |
| 127 | + 'doc_graph has commits': (r) => Array.isArray(r.commits), |
| 128 | + 'doc_graph has edges': (r) => Array.isArray(r.edges), |
| 129 | + }); |
| 130 | + graphFailed.add(!valid, { op: 'doc_graph_shape' }); |
| 131 | + if (!valid) { |
| 132 | + throw new Error('doc_graph invalid graph response'); |
| 133 | + } |
| 134 | + |
| 135 | + tGraphBranchCount.add(body.branches.length); |
| 136 | + tGraphCommitCount.add(body.commits.length); |
| 137 | + tGraphEdgeCount.add(body.edges.length); |
| 138 | +} |
| 139 | + |
| 140 | +function resolveDocsForUser(cookie, userNo) { |
| 141 | + const docs = []; |
| 142 | + const titlePrefix = expectedTitlePrefix(userNo); |
| 143 | + |
| 144 | + for (let page = 0; page < DOC_LIST_MAX_PAGES && docs.length < DOCS_PER_USER; page += 1) { |
| 145 | + const res = http.get( |
| 146 | + `${BASE_URL}/api/document?page=${page}&size=${DOC_LIST_PAGE_SIZE}&sort=updatedAt&order=desc`, |
| 147 | + { ...headers(cookie), tags: { op: 'graph_resolve_docs' } }, |
| 148 | + ); |
| 149 | + ensureStatus(res, [200], 'graph_resolve_docs'); |
| 150 | + |
| 151 | + const body = parseJson(res, 'graph_resolve_docs'); |
| 152 | + if (!body || !Array.isArray(body.content)) { |
| 153 | + throw new Error('graph_resolve_docs invalid page response'); |
| 154 | + } |
| 155 | + |
| 156 | + for (const item of body.content) { |
| 157 | + if (item.title && item.title.startsWith(titlePrefix)) { |
| 158 | + docs.push({ |
| 159 | + userNo, |
| 160 | + docId: item.id, |
| 161 | + title: item.title, |
| 162 | + }); |
| 163 | + } |
| 164 | + } |
| 165 | + |
| 166 | + if (body.last === true || body.content.length === 0) { |
| 167 | + break; |
| 168 | + } |
| 169 | + } |
| 170 | + |
| 171 | + if (docs.length !== DOCS_PER_USER) { |
| 172 | + throw new Error(`graph_resolve_docs expected ${DOCS_PER_USER} docs for userNo=${userNo}, got ${docs.length}`); |
| 173 | + } |
| 174 | + |
| 175 | + return docs; |
| 176 | +} |
| 177 | + |
| 178 | +export function setup() { |
| 179 | + const docs = []; |
| 180 | + const cookies = {}; |
| 181 | + for (let userNo = 1; userNo <= USER_COUNT; userNo += 1) { |
| 182 | + const cookie = login(userEmail(userNo)); |
| 183 | + cookies[userNo] = cookie; |
| 184 | + docs.push(...resolveDocsForUser(cookie, userNo)); |
| 185 | + } |
| 186 | + |
| 187 | + return { docs, cookies }; |
| 188 | +} |
| 189 | + |
| 190 | +export function runGraphRead(data) { |
| 191 | + const index = exec.scenario.iterationInTest % data.docs.length; |
| 192 | + const doc = data.docs[index]; |
| 193 | + const cookie = data.cookies[doc.userNo]; |
| 194 | + if (!cookie) { |
| 195 | + throw new Error(`graph missing cookie for userNo=${doc.userNo}`); |
| 196 | + } |
| 197 | + requestGraph(cookie, doc.docId); |
| 198 | +} |
| 199 | + |
| 200 | +export function handleSummary(data) { |
| 201 | + return { |
| 202 | + stdout: `\n[doc-graph-benchmark] run_id=${RUN_ID}, users=${USER_COUNT}, docs_per_user=${DOCS_PER_USER}\n`, |
| 203 | + [`${RESULT_DIR}/doc_graph_benchmark_${RUN_ID}.json`]: JSON.stringify(data, null, 2), |
| 204 | + }; |
| 205 | +} |
0 commit comments