Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import fs from 'fs';
- import console from 'console';
- import { Session } from 'inspector/promises';
- import path from 'path';
- async function profileFunction(func, args) {
- const cpuProfileFilename = path.resolve(`bench2/${func.name}.cpuprofile`);
- const heapSnapshotFilename = path.resolve(`bench2/${func.name}.heapsnapshot`);
- const session = new Session();
- const fd = fs.openSync(heapSnapshotFilename, 'w');
- session.on('HeapProfiler.addHeapSnapshotChunk', (m) => {
- fs.writeSync(fd, m.params.chunk);
- });
- session.connect();
- await session.post('Profiler.enable');
- await session.post('Profiler.start');
- // @ts-expect-error unknown
- func(...args);
- const { profile } = await session.post('Profiler.stop');
- fs.writeFileSync(cpuProfileFilename, JSON.stringify(profile));
- await session.post('HeapProfiler.takeHeapSnapshot');
- fs.closeSync(fd);
- session.disconnect();
- console.log(`\t- CPU Profile: ${cpuProfileFilename}`);
- console.log(`\t- Heap snapshot Profile: ${cpuProfileFilename}`);
- }
- let searchEntries = [];
- let excludeTags = [];
- let terms = [];
- const validOffers = [];
- const validRestaurantsIds = [];
- const opts = {
- showBadge: true,
- showPrice: true,
- showPriceDiscount: true,
- showPriceFull: true,
- showPriceMode: true,
- showPriceSize: true,
- showPriceMetric: true,
- showTitle: true,
- showDescription: true,
- showLogo: true,
- showRestaurantId: true,
- showImages: true
- };
- const generateOfferBadgeSimple = (opts, offer) => {
- let badge = '';
- if (opts.showBadge) {
- badge = offer.badge;
- }
- return badge;
- };
- const populateData = () => {
- console.log('Populating data...');
- searchEntries = [];
- excludeTags = [];
- terms = [];
- const se = {
- isOpen: () => true,
- getOffers: () => [
- {
- id: i,
- title: 'title',
- description: 'description',
- tag: 'tag',
- metric_description: 'metric_description',
- size_info: 'size_info',
- full_price: 'full_price',
- mode: 'mode',
- price: 'price',
- logo: 'logo',
- restaurant_id: i,
- badge: 'badge',
- images: {
- menu: null,
- menu_bh: null
- }
- }
- ],
- getId: () => i
- };
- for (let i = 0; i < 10000; i++) {
- searchEntries.push(se);
- excludeTags.push('tag' + i);
- terms.push('title' + i);
- }
- };
- const foreachWay = () => {
- searchEntries.forEach((se, sid) => {
- if (!se.isOpen()) {return;}
- const offers = se.getOffers([], excludeTags);
- offers.forEach((o) => {
- terms.forEach((t) => {
- if (o.titleSlug?.includes(t) || o.descriptionSlug?.includes(t)) {
- const offer = {
- id:o.id,
- title:o.title,
- description:o.description,
- tag:o.tag,
- metric_description:o.metric_description,
- size_info:o.size_info,
- full_price:o.full_price,
- mode:o.mode,
- price:o.price,
- logo:o.logo,
- restaurant_id: se.getId(),
- badge: generateOfferBadgeSimple(opts, o),
- images: {
- menu: null,
- menu_bh: null
- }
- };
- validOffers.push(offer);
- validRestaurantsIds.push(sid);
- }
- });
- });
- });
- };
- const forWay = () => {
- for (let sid = 0; sid < searchEntries.length; sid++) {
- const se = searchEntries[sid];
- if (!se.isOpen()) {
- continue;
- }
- const offers = se.getOffers([], excludeTags);
- for (let i = 0; i < offers.length; i++) {
- const o = offers[i];
- for (let j = 0; j < terms.length; j++) {
- const t = terms[j];
- if (o.titleSlug?.includes(t) || o.descriptionSlug?.includes(t)) {
- const offer = {
- id: o.id,
- title: o.title,
- description: o.description,
- tag: o.tag,
- metric_description: o.metric_description,
- size_info: o.size_info,
- full_price: o.full_price,
- mode: o.mode,
- price: o.price,
- logo: o.logo,
- restaurant_id: se.getId(),
- badge: generateOfferBadgeSimple(opts, o),
- images: {
- menu: null,
- menu_bh: null
- }
- };
- validOffers.push(offer);
- validRestaurantsIds.push(sid);
- }
- }
- }
- }
- };
- profileFunction(foreachWay, []);
- populateData();
- profileFunction(forWay, []);
Advertisement
Add Comment
Please, Sign In to add comment