-
-
Save devformatters/ec9f1fbf6c91d1572b565741e14e9183 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import http from 'k6/http' | |
import { endpoint, getTokens, makeParams, rand, stages } from './shared.js' | |
/** | |
* Appreciation tests. | |
* | |
* Steps for run the script up: | |
* 0. make sure `k6` installed | |
* 1. specify `endpoint` in `shared.js` | |
* 2. assign aricle id | |
* 3. assign users | |
* 4. execute `k6 run appreciation.js` | |
*/ | |
const articleId = '' | |
const users = [ | |
{ email: '', password: '' }, | |
{ email: '', password: '' }, | |
] | |
export const options = { | |
stages: [ | |
{ target: 2, duration: '1s' } | |
] | |
} | |
export const setup = () => getTokens(endpoint, users) | |
export default ({ tokens }) => { | |
// make multiple requests randomly | |
const requests = tokens.reduce((params, token) => { | |
[...Array(rand(4, 15)).keys()].map(() => { | |
const amount = rand(1, 4) | |
const query = ` | |
mutation { | |
appreciateArticle (input: { id: "${articleId}", amount: ${amount}, token: "token" }) { | |
id | |
} | |
} | |
` | |
params.push(makeParams(endpoint, query, token)) | |
}) | |
return params | |
}, []) | |
// send requests in parallel | |
const responses = http.batch(requests) | |
responses.map(response => { | |
if (response.status === 200) { | |
const { data } = JSON.parse(response.body) | |
console.log(JSON.stringify(data)) | |
} | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment