hammer.js32 lines, 1.2 KB
| 1 | // One client hammers the API: a single account posts as fast as the API answers. |
| 2 | import http from 'k6/http'; |
| 3 | import { Counter } from 'k6/metrics'; |
| 4 | |
| 5 | import { handleSummary as fourNumbers } from './summary.js'; |
| 6 | |
| 7 | export const options = { |
| 8 | vus: Number(__ENV.VUS || 100), // one client, and this many connections |
| 9 | duration: '20s', |
| 10 | summaryTrendStats: ['med', 'p(99)'], |
| 11 | }; |
| 12 | |
| 13 | const made = new Counter('posts_made'); // 201: the post is in |
| 14 | const refused = new Counter('refused'); // 429: too many requests |
| 15 | |
| 16 | const post = JSON.stringify({ body: 'FREE FOLLOWERS, CLICK HERE' }); |
| 17 | const headers = { Authorization: 'Bearer seed-token-16726', 'Content-Type': 'application/json' }; |
| 18 | |
| 19 | export default function () { |
| 20 | const res = http.post(`${__ENV.BASE_URL}/posts`, post, { headers }); |
| 21 | made.add(res.status === 201 ? 1 : 0); |
| 22 | refused.add(res.status === 429 ? 1 : 0); |
| 23 | } |
| 24 | |
| 25 | // The usual four numbers, and then what this one account got out of it. |
| 26 | export function handleSummary(data) { |
| 27 | const out = fourNumbers(data); |
| 28 | const count = (name) => data.metrics[name].values.count; |
| 29 | out.stdout += `posts made ${count('posts_made')}\n`; |
| 30 | out.stdout += `refused with a 429 ${count('refused')}\n\n`; |
| 31 | return out; |
| 32 | } |