Skip to content

Instantly share code, notes, and snippets.

@devformatters
Created June 16, 2020 08:47
Show Gist options
  • Save devformatters/6967a9ab1d9f5b99cdf3acd5fd6b1c20 to your computer and use it in GitHub Desktop.
Save devformatters/6967a9ab1d9f5b99cdf3acd5fd6b1c20 to your computer and use it in GitHub Desktop.
import http from 'k6/http'
import { endpoint, getTokens, makeParams, rand, stages } from './shared.js'
/**
* Pay-to tests.
*
* Steps for run the script up:
* 0. make sure `k6` installed
* 1. specify `endpoint` in `shared.js`
* 2. assign article id
* 3. assign recipient id
* 4. assign users
* 5. assign shared paymentPassword
* 6. execute `k6 run payto.js`
*/
const articleId = ''
const recipientId = ''
const users = [
{ email: '', password: '' },
{ email: '', password: '' },
]
const paymentPassword = ''
export const options = {
stages: [
{ target: 2, duration: '3s' }
]
}
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(10, 100)).toFixed(2)
const query = `
mutation {
payTo (input: {
amount: ${amount},
currency: HKD,
purpose: donation,
recipientId: "${recipientId}",
targetId: "${articleId}",
password: "${paymentPassword}"
}) {
transaction {
id
state
}
redirectUrl
}
}
`
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