Skip to content

Instantly share code, notes, and snippets.

@sjelfull
Created February 26, 2020 11:24
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save sjelfull/1a7e0dc0150ad4d8add402a811accd8d to your computer and use it in GitHub Desktop.
Load testing a webhook with k6
import http from 'k6/http';
import { check } from "k6";
import { Rate } from "k6/metrics";
export let errorRate = new Rate("errors");
export let options = {
thresholds: {
errors: ["rate<0.1"] // <10% errors
}
};
export default function() {
var url = process.env.URL || 'https://localhost/stripe/webhook-test';
var payload = {"id":"evt_1GFJhQIJe0nt8WUnpue1Lr2J","object":"event","api_version":"2018-11-08","created":1582461404,"data":{"object":{"id":"cus_GmtUuwAE6JjJfY","object":"customer","account_balance":0,"address":null,"balance":0,"created":1582461403,"currency":null,"default_source":"card_1GFJhPIJe0nt8WUnTG9PNKwr","delinquent":false,"description":"(created by Stripe CLI)","discount":null,"email":null,"invoice_prefix":"BC672661","invoice_settings":{"custom_fields":null,"default_payment_method":null,"footer":null},"livemode":false,"metadata":{},"name":null,"phone":null,"preferred_locales":[],"shipping":null,"sources":{"object":"list","data":[{"id":"card_1GFJhPIJe0nt8WUnTG9PNKwr","object":"card","address_city":null,"address_country":null,"address_line1":null,"address_line1_check":null,"address_line2":null,"address_state":null,"address_zip":null,"address_zip_check":null,"brand":"Visa","country":"US","customer":"cus_GmtUuwAE6JjJfY","cvc_check":null,"dynamic_last4":null,"exp_month":2,"exp_year":2021,"fingerprint":"FLkhV4ooOEvCMzLU","funding":"credit","last4":"4242","metadata":{},"name":null,"tokenization_method":null}],"has_more":false,"total_count":1,"url":"\/v1\/customers\/cus_GmtUuwAE6JjJfY\/sources"},"subscriptions":{"object":"list","data":[],"has_more":false,"total_count":0,"url":"\/v1\/customers\/cus_GmtUuwAE6JjJfY\/subscriptions"},"tax_exempt":"none","tax_ids":{"object":"list","data":[],"has_more":false,"total_count":0,"url":"\/v1\/customers\/cus_GmtUuwAE6JjJfY\/tax_ids"},"tax_info":null,"tax_info_verification":null}},"livemode":false,"pending_webhooks":2,"request":{"id":"req_GqSfBDHJIVvHaf","idempotency_key":null},"type":"customer.created"}
var params = {
headers: {
'Content-Type': 'application/json',
'Stripe-Signature': '<signature>',
},
};
const res = http.post(url, payload, params);
const result = check(res, {
"status is 200": r => r.status == 200
});
errorRate.add(!result);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment