init-load-test.ts 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. // tslint:disable-next-line:no-reference
  2. /// <reference path="../../core/typings.d.ts" />
  3. import { bootstrap, JobQueueService, Logger } from '@vendure/core';
  4. import { populate } from '@vendure/core/cli/populate';
  5. import { clearAllTables, populateCustomers } from '@vendure/testing';
  6. import stringify from 'csv-stringify';
  7. import fs from 'fs';
  8. import path from 'path';
  9. import { initialData } from '../../core/mock-data/data-sources/initial-data';
  10. import {
  11. getLoadTestConfig,
  12. getMysqlConnectionOptions,
  13. getPostgresConnectionOptions,
  14. getProductCount,
  15. getProductCsvFilePath,
  16. } from './load-test-config';
  17. // tslint:disable:no-console
  18. /**
  19. * A script used to populate a database with test data for load testing.
  20. */
  21. if (require.main === module) {
  22. // Running from command line
  23. const count = getProductCount();
  24. const databaseName = `vendure-load-testing-${count}`;
  25. isDatabasePopulated(databaseName)
  26. .then(isPopulated => {
  27. console.log(`isPopulated:`, isPopulated);
  28. if (!isPopulated) {
  29. const config = getLoadTestConfig('bearer', databaseName);
  30. const csvFile = getProductCsvFilePath();
  31. return clearAllTables(config, true)
  32. .then(() => {
  33. if (!fs.existsSync(csvFile)) {
  34. return generateProductsCsv(count);
  35. }
  36. })
  37. .then(() =>
  38. populate(
  39. () =>
  40. bootstrap(config).then(async app => {
  41. await app.get(JobQueueService).start();
  42. return app;
  43. }),
  44. path.join(__dirname, '../../create/assets/initial-data.json'),
  45. csvFile,
  46. ),
  47. )
  48. .then(async app => {
  49. console.log('populating customers...');
  50. await populateCustomers(app, 10, message => Logger.error(message));
  51. return app.close();
  52. });
  53. } else {
  54. console.log('Database is already populated!');
  55. }
  56. })
  57. .then(
  58. () => process.exit(0),
  59. err => {
  60. console.log(err);
  61. process.exit(1);
  62. },
  63. );
  64. }
  65. /**
  66. * Tests to see whether the load test database is already populated.
  67. */
  68. async function isDatabasePopulated(databaseName: string): Promise<boolean> {
  69. const isPostgres = process.env.DB === 'postgres';
  70. if (isPostgres) {
  71. console.log(`Checking whether data is populated (postgres)`);
  72. const pg = require('pg');
  73. const postgresConnectionOptions = getPostgresConnectionOptions(databaseName);
  74. const client = new pg.Client({
  75. host: postgresConnectionOptions.host,
  76. user: postgresConnectionOptions.username,
  77. database: postgresConnectionOptions.database,
  78. password: postgresConnectionOptions.password,
  79. port: postgresConnectionOptions.port,
  80. });
  81. await client.connect();
  82. try {
  83. const res = await client.query('SELECT COUNT(id) as prodCount FROM product');
  84. return true;
  85. } catch (e) {
  86. if (e.message === `relation "product" does not exist`) {
  87. return false;
  88. }
  89. throw e;
  90. }
  91. } else {
  92. const mysql = require('mysql');
  93. const mysqlConnectionOptions = getMysqlConnectionOptions(databaseName);
  94. const connection = mysql.createConnection({
  95. host: mysqlConnectionOptions.host,
  96. user: mysqlConnectionOptions.username,
  97. password: mysqlConnectionOptions.password,
  98. database: mysqlConnectionOptions.database,
  99. });
  100. return new Promise<boolean>((resolve, reject) => {
  101. connection.connect((error: any) => {
  102. if (error) {
  103. reject(error);
  104. return;
  105. }
  106. connection.query('SELECT COUNT(id) as prodCount FROM product', (err: any, results: any) => {
  107. if (err) {
  108. if (err.code === 'ER_NO_SUCH_TABLE') {
  109. resolve(false);
  110. return;
  111. }
  112. reject(err);
  113. return;
  114. }
  115. resolve(true);
  116. });
  117. });
  118. });
  119. }
  120. }
  121. /**
  122. * Generates a CSV file of test product data which can then be imported into Vendure.
  123. */
  124. function generateProductsCsv(productCount: number = 100): Promise<void> {
  125. const result: any[] = [];
  126. const stringifier = stringify({
  127. delimiter: ',',
  128. });
  129. const data: string[] = [];
  130. console.log(`Generating ${productCount} rows of test product data...`);
  131. stringifier.on('readable', () => {
  132. let row;
  133. // tslint:disable-next-line:no-conditional-assignment
  134. while ((row = stringifier.read())) {
  135. data.push(row);
  136. }
  137. });
  138. return new Promise((resolve, reject) => {
  139. const csvFile = getProductCsvFilePath();
  140. stringifier.on('error', (err: any) => {
  141. reject(err.message);
  142. });
  143. stringifier.on('finish', async () => {
  144. fs.writeFileSync(csvFile, data.join(''));
  145. console.log(`Done! Saved to ${csvFile}`);
  146. resolve();
  147. });
  148. generateMockData(productCount, row => stringifier.write(row));
  149. stringifier.end();
  150. });
  151. }
  152. function generateMockData(productCount: number, writeFn: (row: string[]) => void) {
  153. const headers: string[] = [
  154. 'name',
  155. 'slug',
  156. 'description',
  157. 'assets',
  158. 'facets',
  159. 'optionGroups',
  160. 'optionValues',
  161. 'sku',
  162. 'price',
  163. 'taxCategory',
  164. 'variantAssets',
  165. 'variantFacets',
  166. 'stockOnHand',
  167. 'trackInventory',
  168. ];
  169. writeFn(headers);
  170. const categories = getCategoryNames();
  171. for (let i = 1; i <= productCount; i++) {
  172. const outputRow = {
  173. name: `Product ${i}`,
  174. slug: `product-${i}`,
  175. description: generateProductDescription(),
  176. assets: 'product-image.jpg',
  177. facets: `category:${categories[i % categories.length]}`,
  178. optionGroups: '',
  179. optionValues: '',
  180. sku: `PRODID${i}`,
  181. price: (Math.random() * 1000).toFixed(2),
  182. taxCategory: 'standard',
  183. variantAssets: '',
  184. variantFacets: '',
  185. stockOnHand: '1000',
  186. trackInventory: 'false',
  187. };
  188. writeFn(Object.values(outputRow) as string[]);
  189. }
  190. }
  191. function getCategoryNames() {
  192. const allNames = new Set<string>();
  193. for (const collection of initialData.collections) {
  194. for (const filter of collection.filters || []) {
  195. filter.args.facetValueNames.forEach(name => allNames.add(name));
  196. }
  197. }
  198. return Array.from(new Set(allNames));
  199. }
  200. const parts = [
  201. `Now equipped with seventh-generation Intel Core processors`,
  202. `Laptop is snappier than ever`,
  203. `From daily tasks like launching apps and opening files to more advanced computing`,
  204. `You can power through your day thanks to faster SSDs and Turbo Boost processing up to 3.6GHz`,
  205. `Discover a truly immersive viewing experience with this monitor curved more deeply than any other`,
  206. `Wrapping around your field of vision the 1,800 R screencreates a wider field of view`,
  207. `This pc is optimised for gaming, and is also VR ready`,
  208. `The Intel Core-i7 CPU and High Performance GPU give the computer the raw power it needs to function at a high level`,
  209. `Boost your PC storage with this internal hard drive, designed just for desktop and all-in-one PCs`,
  210. `Let all your colleagues know that you are typing on this exclusive, colorful klicky-klacky keyboard`,
  211. `Solid conductors eliminate strand-interaction distortion and reduce jitter`,
  212. `As the surface is made of high-purity silver`,
  213. `the performance is very close to that of a solid silver cable`,
  214. `but priced much closer to solid copper cable`,
  215. `With its nostalgic design and simple point-and-shoot functionality`,
  216. `the Instant Camera is the perfect pick to get started with instant photography`,
  217. `This lens is a Di type lens using an optical system with improved multi-coating designed to function with digital SLR cameras as well as film cameras`,
  218. `Capture vivid, professional-style photographs with help from this lightweight tripod`,
  219. `The adjustable-height tripod makes it easy to achieve reliable stability`,
  220. `Just the right angle when going after that award-winning shot`,
  221. `Featuring a full carbon chassis - complete with cyclocross-specific carbon fork`,
  222. `It's got the low weight, exceptional efficiency and brilliant handling`,
  223. `You'll need to stay at the front of the pack`,
  224. `When you're working out you need a quality rope that doesn't tangle at every couple of jumps`,
  225. `Training gloves designed for optimum training`,
  226. `Our gloves promote proper punching technique because they are conformed to the natural shape of your fist`,
  227. `Dense, innovative two-layer foam provides better shock absorbency`,
  228. `Full padding on the front, back and wrist to promote proper punching technique`,
  229. `With tons of space inside (for max. 4 persons), full head height throughout`,
  230. `This tent offers you everything you need`,
  231. `Based on the 1970s iconic shape, but made to a larger 69cm size`,
  232. `These skateboards are great for beginners to learn the foot spacing required`,
  233. `Perfect for all-day cruising`,
  234. `This football features high-contrast graphics for high-visibility during play`,
  235. `Its machine-stitched tpu casing offers consistent performance`,
  236. `With its ultra-light, uber-responsive magic foam`,
  237. `The Running Shoe is ready to push you to victories both large and small`,
  238. `A spiky yet elegant house cactus`,
  239. `Perfect for the home or office`,
  240. `Origin and habitat: Probably native only to the Andes of Peru`,
  241. `Gloriously elegant`,
  242. `It can go along with any interior as it is a neutral color and the most popular Phalaenopsis overall`,
  243. `2 to 3 foot stems host large white flowers that can last for over 2 months`,
  244. `Excellent semi-evergreen bonsai`,
  245. `Indoors or out but needs some winter protection`,
  246. `All trees sent will leave the nursery in excellent condition and will be of equal quality or better than the photograph shown`,
  247. `Placing it at home or office can bring you fortune and prosperity`,
  248. `Guards your house and ward off ill fortune`,
  249. `Hand trowel for garden cultivating hammer finish epoxy-coated head`,
  250. `For improved resistance to rust, scratches, humidity and alkalines in the soil`,
  251. `A charming vintage white wooden chair`,
  252. `Featuring an extremely spherical pink balloon`,
  253. `The balloon may be detached and used for other purposes`,
  254. `This premium, tan-brown bonded leather seat is part of the 'chill' sofa range`,
  255. `The lever activated recline feature makes it easy to adjust to any position`,
  256. `This smart, bustle back design with rounded tight padded arms has been designed with your comfort in mind`,
  257. `This well-padded chair has foam pocket sprung seat cushions and fibre-filled back cushions`,
  258. `Modern tapered white polycotton pendant shade with a metallic silver chrome interior`,
  259. `For maximum light reflection`,
  260. `Reversible gimble so it can be used as a ceiling shade or as a lamp shade`,
  261. ];
  262. function generateProductDescription(): string {
  263. const take = Math.ceil(Math.random() * 4);
  264. return shuffle(parts).slice(0, take).join('. ');
  265. }
  266. /**
  267. * Returns new copy of array in random order.
  268. * https://stackoverflow.com/a/6274381/772859
  269. */
  270. function shuffle<T>(arr: T[]): T[] {
  271. const a = arr.slice();
  272. for (let i = a.length - 1; i > 0; i--) {
  273. const j = Math.floor(Math.random() * (i + 1));
  274. [a[i], a[j]] = [a[j], a[i]];
  275. }
  276. return a;
  277. }