very-large-order.js 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. // @ts-check
  2. import {check} from 'k6';
  3. import {ShopApiRequest} from '../utils/api-request.js';
  4. const searchQuery = new ShopApiRequest('shop/search.graphql');
  5. const addItemToOrderMutation = new ShopApiRequest('shop/add-to-order.graphql');
  6. export let options = {
  7. stages: [
  8. { duration: '4m', target: 1 },
  9. ],
  10. };
  11. export function setup() {
  12. const searchResult = searchQuery.post();
  13. return searchResult.data.search.items;
  14. }
  15. /**
  16. * Continuously adds random items to a single order for the duration of the test.
  17. */
  18. export default function(products) {
  19. for (let i = 0; i < 10000; i ++) {
  20. addToCart(randomItem(products).productVariantId);
  21. }
  22. }
  23. function addToCart(variantId) {
  24. const qty = Math.ceil(Math.random() * 4);
  25. const result = addItemToOrderMutation.post({ id: variantId, qty });
  26. check(result.data, {
  27. 'Product added to cart': r => !!r.addItemToOrder.lines
  28. .find(l => l.productVariant.id === variantId && l.quantity >= qty),
  29. });
  30. }
  31. function randomItem(items) {
  32. return items[Math.floor(Math.random() * items.length)];
  33. }