exapsy

Untitled

Oct 18th, 2024
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import fs from 'fs';
  2. import console from 'console';
  3. import { Session } from 'inspector/promises';
  4.  
  5. import path from 'path';
  6.  
  7. async function profileFunction(func, args) {
  8.   const cpuProfileFilename = path.resolve(`bench2/${func.name}.cpuprofile`);
  9.   const heapSnapshotFilename = path.resolve(`bench2/${func.name}.heapsnapshot`);
  10.   const session = new Session();
  11.  
  12.   const fd = fs.openSync(heapSnapshotFilename, 'w');
  13.  
  14.   session.on('HeapProfiler.addHeapSnapshotChunk', (m) => {
  15.     fs.writeSync(fd, m.params.chunk);
  16.   });
  17.  
  18.   session.connect();
  19.   await session.post('Profiler.enable');
  20.   await session.post('Profiler.start');
  21.  
  22.   // @ts-expect-error unknown
  23.   func(...args);
  24.  
  25.   const { profile } = await session.post('Profiler.stop');
  26.   fs.writeFileSync(cpuProfileFilename, JSON.stringify(profile));
  27.  
  28.   await session.post('HeapProfiler.takeHeapSnapshot');
  29.   fs.closeSync(fd);
  30.  
  31.   session.disconnect();
  32.  
  33.   console.log(`\t- CPU Profile: ${cpuProfileFilename}`);
  34.   console.log(`\t- Heap snapshot Profile: ${cpuProfileFilename}`);
  35. }
  36.  
  37. let searchEntries = [];
  38. let excludeTags = [];
  39. let terms = [];
  40. const validOffers = [];
  41. const validRestaurantsIds = [];
  42. const opts = {
  43.   showBadge: true,
  44.   showPrice: true,
  45.   showPriceDiscount: true,
  46.   showPriceFull: true,
  47.   showPriceMode: true,
  48.   showPriceSize: true,
  49.   showPriceMetric: true,
  50.   showTitle: true,
  51.   showDescription: true,
  52.   showLogo: true,
  53.   showRestaurantId: true,
  54.   showImages: true
  55. };
  56.  
  57. const generateOfferBadgeSimple = (opts, offer) => {
  58.   let badge = '';
  59.   if (opts.showBadge) {
  60.     badge = offer.badge;
  61.   }
  62.   return badge;
  63. };
  64.  
  65. const populateData = () => {
  66.   console.log('Populating data...');
  67.   searchEntries = [];
  68.   excludeTags = [];
  69.   terms = [];
  70.  
  71.   const se = {
  72.   isOpen: () => true,
  73.     getOffers: () => [
  74.       {
  75.         id: i,
  76.         title: 'title',
  77.         description: 'description',
  78.         tag: 'tag',
  79.         metric_description: 'metric_description',
  80.         size_info: 'size_info',
  81.         full_price: 'full_price',
  82.         mode: 'mode',
  83.         price: 'price',
  84.         logo: 'logo',
  85.         restaurant_id: i,
  86.         badge: 'badge',
  87.         images: {
  88.           menu: null,
  89.           menu_bh: null
  90.         }
  91.       }
  92.     ],
  93.     getId: () => i
  94.   };
  95.   for (let i = 0; i < 10000; i++) {
  96.     searchEntries.push(se);
  97.     excludeTags.push('tag' + i);
  98.     terms.push('title' + i);
  99.   }
  100. };
  101.  
  102. const foreachWay = () => {
  103.   searchEntries.forEach((se, sid) => {
  104.     if (!se.isOpen()) {return;}
  105.     const offers = se.getOffers([], excludeTags);
  106.     offers.forEach((o) => {
  107.       terms.forEach((t) => {
  108.         if (o.titleSlug?.includes(t) || o.descriptionSlug?.includes(t)) {
  109.           const offer = {
  110.             id:o.id,
  111.             title:o.title,
  112.             description:o.description,
  113.             tag:o.tag,
  114.             metric_description:o.metric_description,
  115.             size_info:o.size_info,
  116.             full_price:o.full_price,
  117.             mode:o.mode,
  118.             price:o.price,
  119.             logo:o.logo,
  120.             restaurant_id: se.getId(),
  121.             badge: generateOfferBadgeSimple(opts, o),
  122.             images: {
  123.               menu: null,
  124.               menu_bh: null
  125.             }
  126.           };
  127.           validOffers.push(offer);
  128.           validRestaurantsIds.push(sid);
  129.         }
  130.       });
  131.     });
  132.   });
  133. };
  134.  
  135. const forWay = () => {
  136.   for (let sid = 0; sid < searchEntries.length; sid++) {
  137.     const se = searchEntries[sid];
  138.     if (!se.isOpen()) {
  139.       continue;
  140.     }
  141.  
  142.     const offers = se.getOffers([], excludeTags);
  143.     for (let i = 0; i < offers.length; i++) {
  144.       const o = offers[i];
  145.       for (let j = 0; j < terms.length; j++) {
  146.         const t = terms[j];
  147.         if (o.titleSlug?.includes(t) || o.descriptionSlug?.includes(t)) {
  148.           const offer = {
  149.             id: o.id,
  150.             title: o.title,
  151.             description: o.description,
  152.             tag: o.tag,
  153.             metric_description: o.metric_description,
  154.             size_info: o.size_info,
  155.             full_price: o.full_price,
  156.             mode: o.mode,
  157.             price: o.price,
  158.             logo: o.logo,
  159.             restaurant_id: se.getId(),
  160.             badge: generateOfferBadgeSimple(opts, o),
  161.             images: {
  162.               menu: null,
  163.               menu_bh: null
  164.             }
  165.           };
  166.           validOffers.push(offer);
  167.           validRestaurantsIds.push(sid);
  168.         }
  169.       }
  170.     }
  171.   }
  172. };
  173.  
  174. profileFunction(foreachWay, []);
  175.  
  176. populateData();
  177.  
  178. profileFunction(forWay, []);
  179.  
Advertisement
Add Comment
Please, Sign In to add comment