very-large-order3.js 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  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. const adjustOrderLineMutation = new ShopApiRequest('shop/adjust-order-line.graphql');
  7. export let options = {
  8. stages: [{ duration: '1m', target: 1 }],
  9. };
  10. export function setup() {
  11. const searchResult = searchQuery.post();
  12. const items = searchResult.data.search.items;
  13. return items;
  14. }
  15. /**
  16. * Continuously adds random items to a single order for the duration of the test.
  17. * Just like very-large-order.js but adds 999 items each time, and runs for only 1 minute.
  18. * Both addItemToOrder and adjustOrderLine are tested as the latter needs the first and tends to be more complex/slower.
  19. */
  20. export default function (products) {
  21. const orderLineId = addToCart(randomItem(products).productVariantId, 999);
  22. adjustOrderLine(orderLineId, 1);
  23. adjustOrderLine(orderLineId, 999);
  24. adjustOrderLine(orderLineId, 0);
  25. }
  26. function addToCart(variantId, qty) {
  27. const result = addItemToOrderMutation.post({ id: variantId, qty });
  28. check(result.data, {
  29. 'Product added to cart': r =>
  30. !!r.addItemToOrder.lines.find(l => l.productVariant.id === variantId && l.quantity === qty),
  31. });
  32. return result.data.addItemToOrder.lines.find(l => l.productVariant.id === variantId).id;
  33. }
  34. function adjustOrderLine(orderLineId, qty) {
  35. const result = adjustOrderLineMutation.post({ id: orderLineId, qty });
  36. check(result.data, {
  37. 'Product quantity adjusted': r => r.adjustOrderLine.totalQuantity === qty,
  38. });
  39. }
  40. function randomItem(items) {
  41. return items[Math.floor(Math.random() * items.length)];
  42. }