very-large-order2.js 1.2 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: [{ duration: '1m', target: 1 }],
  8. };
  9. export function setup() {
  10. const searchResult = searchQuery.post();
  11. const items = searchResult.data.search.items;
  12. return items;
  13. }
  14. /**
  15. * Continuously adds random items to a single order for the duration of the test.
  16. * Just like very-large-order.js but adds hundreds of items each time, and runs for only 1 minute
  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() * 500);
  25. const result = addItemToOrderMutation.post({ id: variantId, qty });
  26. check(result.data, {
  27. 'Product added to cart': r =>
  28. !!r.addItemToOrder.lines.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. }