mix.js135 lines, 4.4 KB
| 1 | // A realistic mix: thousands of people online, and about a hundred reads for every write. |
| 2 | import http from 'k6/http'; |
| 3 | import exec from 'k6/execution'; |
| 4 | |
| 5 | const BASE = __ENV.BASE_URL; |
| 6 | // How many different people are online during the test. Everybody else is away. |
| 7 | const ONLINE = 20000; |
| 8 | const TIME = __ENV.TIME || '30s'; |
| 9 | |
| 10 | // Open: RATE requests start every second, whether the last ones were answered or not. |
| 11 | const open = { |
| 12 | executor: 'constant-arrival-rate', |
| 13 | rate: Number(__ENV.RATE || 500), |
| 14 | timeUnit: '1s', |
| 15 | duration: TIME, |
| 16 | preAllocatedVUs: 2000, |
| 17 | }; |
| 18 | // Closed, like every test before this one: VUS connections, and each of them sends |
| 19 | // its next request when the last one was answered. |
| 20 | const closed = { executor: 'constant-vus', vus: Number(__ENV.VUS), duration: TIME }; |
| 21 | |
| 22 | const scenarios = { mix: __ENV.VUS ? closed : open }; |
| 23 | // SPIKE=500: twenty seconds in, 500 posts a second arrive on top of the mix, for ten seconds. |
| 24 | if (__ENV.SPIKE) { |
| 25 | scenarios.spike = { |
| 26 | executor: 'constant-arrival-rate', |
| 27 | exec: 'post', |
| 28 | startTime: '20s', |
| 29 | rate: Number(__ENV.SPIKE), |
| 30 | timeUnit: '1s', |
| 31 | duration: '10s', |
| 32 | preAllocatedVUs: 500, |
| 33 | }; |
| 34 | } |
| 35 | // STAR=400: twenty seconds in, user 1 posts. Then a crowd likes that post, 400 times |
| 36 | // a second, for fifteen seconds. |
| 37 | if (__ENV.STAR) { |
| 38 | scenarios.star = { executor: 'per-vu-iterations', exec: 'star', startTime: '20s', vus: 1, iterations: 1 }; |
| 39 | scenarios.crowd = { |
| 40 | executor: 'constant-arrival-rate', |
| 41 | exec: 'fan', |
| 42 | startTime: '22s', |
| 43 | rate: Number(__ENV.STAR), |
| 44 | timeUnit: '1s', |
| 45 | duration: '15s', |
| 46 | preAllocatedVUs: 500, |
| 47 | }; |
| 48 | } |
| 49 | |
| 50 | export const options = { scenarios, summaryTrendStats: ['med', 'p(99)'] }; |
| 51 | |
| 52 | // Ids 2 to 20,001. User 1 is the celebrity: it stays out of the crowd, and it only posts when STAR says so. |
| 53 | const someone = () => 2 + Math.floor(Math.random() * ONLINE); |
| 54 | const as = (id) => ({ Authorization: `Bearer seed-token-${id}` }); |
| 55 | const json = (id) => ({ ...as(id), 'Content-Type': 'application/json' }); |
| 56 | |
| 57 | // Out of a thousand requests, 990 read and 10 write. |
| 58 | const mix = [ |
| 59 | [600, feed], |
| 60 | [250, unread], |
| 61 | [100, profile], |
| 62 | [40, inbox], |
| 63 | [5, like], |
| 64 | [3, post], |
| 65 | [2, follow], |
| 66 | ]; |
| 67 | |
| 68 | export default function () { |
| 69 | let n = Math.random() * 1000; |
| 70 | for (const [share, request] of mix) { |
| 71 | if ((n -= share) < 0) return request(); |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | function feed() { |
| 76 | http.get(`${BASE}/feed`, { headers: as(someone()) }); |
| 77 | } |
| 78 | |
| 79 | function unread() { |
| 80 | http.get(`${BASE}/notifications/unread`, { headers: as(someone()) }); |
| 81 | } |
| 82 | |
| 83 | // A tag gives every address with an id in it one name, or k6 keeps its numbers per URL. |
| 84 | function profile() { |
| 85 | http.get(`${BASE}/users/user${someone()}/posts`, { tags: { name: 'profile' } }); |
| 86 | } |
| 87 | |
| 88 | function inbox() { |
| 89 | http.get(`${BASE}/notifications`, { headers: as(someone()) }); |
| 90 | } |
| 91 | |
| 92 | function like() { |
| 93 | const id = 1 + Math.floor(Math.random() * 2000000); |
| 94 | http.post(`${BASE}/posts/${id}/like`, null, { headers: as(someone()), tags: { name: 'like' } }); |
| 95 | } |
| 96 | |
| 97 | export function post() { |
| 98 | const body = JSON.stringify({ body: 'a post from the mix' }); |
| 99 | http.post(`${BASE}/posts`, body, { headers: json(someone()) }); |
| 100 | } |
| 101 | |
| 102 | function follow() { |
| 103 | const other = `user${1 + Math.floor(Math.random() * 100000)}`; |
| 104 | http.post(`${BASE}/users/${other}/follow`, null, { headers: as(someone()), tags: { name: 'follow' } }); |
| 105 | } |
| 106 | |
| 107 | // The biggest account posts once. |
| 108 | export function star() { |
| 109 | http.post(`${BASE}/posts`, JSON.stringify({ body: 'big news' }), { headers: json(1) }); |
| 110 | } |
| 111 | |
| 112 | // A fan opens user 1's page, and likes the newest post on it. Every fan is a different person. |
| 113 | export function fan() { |
| 114 | const page = http.get(`${BASE}/users/user1/posts`, { tags: { name: 'profile' } }); |
| 115 | const newest = page.json('posts.0.id'); |
| 116 | const headers = as(2 + exec.scenario.iterationInTest); |
| 117 | http.post(`${BASE}/posts/${newest}/like`, null, { headers, tags: { name: 'like' } }); |
| 118 | } |
| 119 | |
| 120 | // The mix ends with one line, and not with five. So several runs make a table. |
| 121 | export function handleSummary(data) { |
| 122 | const m = data.metrics; |
| 123 | const ms = (stat) => `${m.http_req_duration.values[stat].toFixed(1)} ms`.padStart(9); |
| 124 | const n = (v, width) => String(v).padStart(width); |
| 125 | const dropped = m.dropped_iterations ? m.dropped_iterations.values.count : 0; |
| 126 | const line = [ |
| 127 | `asked ${n(__ENV.VUS ? '-' : __ENV.RATE || 500, 4)}`, |
| 128 | `done ${n(m.http_reqs.values.rate.toFixed(0), 4)}`, |
| 129 | `p50 ${ms('med')}`, |
| 130 | `p99 ${ms('p(99)')}`, |
| 131 | `failed ${n(m.http_req_failed.values.passes, 5)}`, |
| 132 | `dropped ${n(dropped, 5)}`, |
| 133 | ]; |
| 134 | return { stdout: `${line.join(' ')}\n` }; |
| 135 | } |