default-search-plugin.bench.ts 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /* eslint-disable @typescript-eslint/no-non-null-assertion */
  2. import { DefaultJobQueuePlugin, DefaultSearchPlugin, mergeConfig } from '@vendure/core';
  3. import { createTestEnvironment, registerInitializer, SqljsInitializer } from '@vendure/testing';
  4. import path from 'path';
  5. import { Bench } from 'tinybench';
  6. import { afterAll, beforeAll, describe, expect, it } from 'vitest';
  7. import { initialData } from '../../../e2e-common/e2e-initial-data';
  8. import { testConfig, TEST_SETUP_TIMEOUT_MS } from '../../../e2e-common/test-config';
  9. import {
  10. SearchProductsShopQuery,
  11. SearchProductsShopQueryVariables,
  12. } from './graphql/generated-e2e-shop-types';
  13. import { SEARCH_PRODUCTS_SHOP } from './graphql/shop-definitions';
  14. import { awaitRunningJobs } from './utils/await-running-jobs';
  15. registerInitializer('sqljs', new SqljsInitializer(path.join(__dirname, '__data__'), 1000));
  16. interface SearchProductsShopQueryVariablesExt extends SearchProductsShopQueryVariables {
  17. input: SearchProductsShopQueryVariables['input'] & {
  18. // This input field is dynamically added only when the `indexStockStatus` init option
  19. // is set to `true`, and therefore not included in the generated type. Therefore
  20. // we need to manually patch it here.
  21. inStock?: boolean;
  22. };
  23. }
  24. const { server, adminClient, shopClient } = createTestEnvironment(
  25. mergeConfig(testConfig(), {
  26. plugins: [DefaultSearchPlugin.init({ indexStockStatus: true }), DefaultJobQueuePlugin],
  27. }),
  28. );
  29. let marginFactor = 1; // Defaults to 1, will be adjusted during test
  30. let cpuFactor = 1; // Defaults to 1, will be adjusted during test
  31. const fibonacci = (i: number): number => {
  32. if (i <= 1) return i;
  33. return fibonacci(i - 1) + fibonacci(i - 2);
  34. };
  35. beforeAll(async () => {
  36. await server.init({
  37. initialData,
  38. productsCsvPath: path.join(__dirname, 'fixtures/e2e-products-default-search.csv'),
  39. customerCount: 1,
  40. });
  41. await adminClient.asSuperAdmin();
  42. await awaitRunningJobs(adminClient);
  43. }, TEST_SETUP_TIMEOUT_MS);
  44. afterAll(async () => {
  45. await server.destroy();
  46. });
  47. const isDevelopment = process.env.NODE_ENV === 'development';
  48. describe.skipIf(isDevelopment)('Default search plugin - benchmark', () => {
  49. it('defines benchmark cpu and margin factor', async () => {
  50. const bench = new Bench({
  51. warmupTime: 0,
  52. warmupIterations: 1,
  53. time: 0,
  54. iterations: 10,
  55. });
  56. bench.add('measure time to calcualate fibonacci', () => {
  57. fibonacci(41); // If this task would take 1000 ms the cpuFactor would be 1.
  58. });
  59. const tasks = await bench.run();
  60. // console.table(bench.table());
  61. tasks.forEach(task => {
  62. expect(task.result?.rme).toBeDefined();
  63. expect(task.result?.mean).toBeDefined();
  64. if (task.result?.rme && task.result?.mean) {
  65. marginFactor = 1 + task.result.rme / 100;
  66. cpuFactor = 1000 / task.result.mean;
  67. }
  68. });
  69. });
  70. it('performs when grouped by product', async () => {
  71. const bench = new Bench({
  72. warmupTime: 0,
  73. warmupIterations: 3,
  74. time: 0,
  75. iterations: 1000,
  76. });
  77. bench.add('group by product', async () => {
  78. await shopClient.query<SearchProductsShopQuery, SearchProductsShopQueryVariablesExt>(
  79. SEARCH_PRODUCTS_SHOP,
  80. {
  81. input: {
  82. groupByProduct: true,
  83. },
  84. },
  85. );
  86. });
  87. const tasks = await bench.run();
  88. // console.table(bench.table());
  89. tasks.forEach(task => {
  90. expect(task.result?.mean).toBeDefined();
  91. if (task.result?.mean) {
  92. expect(task.result.mean * cpuFactor).toBeLessThan(6.835 * marginFactor);
  93. }
  94. });
  95. });
  96. });