Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf(utils): setup microbenchmark for crawlFileSystem #355

Merged
merged 7 commits into from
Dec 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions packages/utils/perf/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Micro Benchmarks

Execute any benchmark by running `nx run utils:perf` naming the folder e.g. `nx run utils:perf score-report`.

To list benchmarks run `ls -l | grep "^d" | awk '{print $9}'`

## scoreReport

`nx run utils:perf score-report`
105 changes: 105 additions & 0 deletions packages/utils/perf/crawl-file-system/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
import * as Benchmark from 'benchmark';
import { join } from 'path';
import {
CrawlFileSystemOptions,
crawlFileSystem,
} from '../../src/lib/file-system';
import { crawlFileSystemOptimized0 } from './optimized0';

const PROCESS_ARGUMENT_TARGET_DIRECTORY = String(
process.argv
.find(arg => arg.startsWith('--directory'))
?.split('=')
.pop() || '',
);
const PROCESS_ARGUMENT_PATTERN = String(
process.argv
.find(arg => arg.startsWith('--pattern'))
?.split('=')
.pop() || '',
);

const suite = new Benchmark.Suite('report-scoring');

const TARGET_DIRECTORY =
PROCESS_ARGUMENT_TARGET_DIRECTORY ||
join(process.cwd(), '..', '..', '..', 'node_modules');
const PATTERN = PROCESS_ARGUMENT_PATTERN || /.json$/;

// ==================

const start = performance.now();

// Add listener
const listeners = {
cycle: function (event: Benchmark.Event) {
console.info(String(event.target));
},
complete: () => {
if (typeof suite.filter === 'function') {
console.info(' ');
console.info(
`Total Duration: ${((performance.now() - start) / 1000).toFixed(
2,
)} sec`,
);
console.info('Fastest is ' + suite.filter('fastest').map('name'));
}
},
};

// ==================

// Add tests
const options = {
directory: TARGET_DIRECTORY,
pattern: PATTERN,
};
suite.add('Base', wrapWithDefer(crawlFileSystem));
suite.add('Optimized 0', wrapWithDefer(crawlFileSystemOptimized0));

// ==================

// Add Listener
Object.entries(listeners).forEach(([name, fn]) => {
suite.on(name, fn);
});

// ==================

console.info('You can adjust the test with the following arguments:');
console.info(
`directory target directory of test --directory=${TARGET_DIRECTORY}`,
);
console.info(
`pattern pattern to search --pattern=${PATTERN}`,
);
console.info(' ');
console.info('Start benchmark...');
console.info(' ');

suite.run({
async: true,
});

// ==============================================================

function wrapWithDefer<T>(
asyncFn: (options: CrawlFileSystemOptions<T>) => Promise<unknown[]>,
) {
return {
defer: true, // important for async functions
fn: function (deferred: { resolve: () => void }) {
return asyncFn(options)
.catch(() => [])
.then((result: unknown[]) => {
if (result.length === 0) {
throw new Error(`Result length is ${result.length}`);
} else {
deferred.resolve();
}
return void 0;
});
},
};
}
8 changes: 8 additions & 0 deletions packages/utils/perf/crawl-file-system/optimized0.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { CrawlFileSystemOptions } from '../../src';

export function crawlFileSystemOptimized0<T = string>(
options: CrawlFileSystemOptions<T>,
): Promise<T[]> {
const { directory, pattern: needle = '@TODO' } = options;
return Promise.resolve([directory, needle] as T[]);
}
106 changes: 0 additions & 106 deletions packages/utils/perf/implementations/base.ts

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import * as Benchmark from 'benchmark';
import { Report } from '@code-pushup/models';
import { scoreReport } from './implementations/base';
import { scoreReportOptimized0 } from './implementations/optimized0';
import { scoreReportOptimized1 } from './implementations/optimized1';
import { scoreReportOptimized2 } from './implementations/optimized2';
import { scoreReportOptimized3 } from './implementations/optimized3';
import { scoreReport } from '../../src/lib/scoring';
import { scoreReportOptimized0 } from './optimized0';
import { scoreReportOptimized1 } from './optimized1';
import { scoreReportOptimized2 } from './optimized2';
import { scoreReportOptimized3 } from './optimized3';

interface MinimalReportOptions {
numAuditsP1?: number;
Expand Down Expand Up @@ -66,7 +66,7 @@ const listeners = {
// ==================

// Add tests
suite.add('scoreReport', _scoreReport);
suite.add('scoreReport', scoreReport);
suite.add('scoreReportOptimized0', _scoreReportOptimized0);
suite.add('scoreReportOptimized1', _scoreReportOptimized1);
suite.add('scoreReportOptimized2', _scoreReportOptimized2);
Expand Down Expand Up @@ -97,18 +97,16 @@ console.info(' ');

const start = performance.now();

suite.run();

console.info(
`Total Duration: ${((performance.now() - start) / 1000).toFixed(2)} sec`,
);
suite.run({
onComplete: () => {
console.info(
`Total Duration: ${((performance.now() - start) / 1000).toFixed(2)} sec`,
);
},
});

// ==============================================================

function _scoreReport() {
scoreReport(minimalReport());
}

function _scoreReportOptimized0() {
scoreReportOptimized0(minimalReport());
}
Expand Down
11 changes: 10 additions & 1 deletion packages/utils/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,16 @@
}
},
"perf": {
"command": "npx tsx ./packages/utils/perf --tsconfig=packages/utils/tsconfig.perf.json"
"command": "npx tsx --tsconfig=../tsconfig.perf.json",
"options": {
"cwd": "./packages/utils/perf"
}
},
"perf:list": {
"command": "ls -l | grep \"^d\" | awk '{print $9}'",
"options": {
"cwd": "./packages/utils/perf"
}
},
"unit-test": {
"executor": "@nx/vite:test",
Expand Down
1 change: 1 addition & 0 deletions packages/utils/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export {
export {
FileResult,
MultipleFileResults,
CrawlFileSystemOptions,
crawlFileSystem,
ensureDirectoryExists,
fileExists,
Expand Down
8 changes: 5 additions & 3 deletions packages/utils/src/lib/file-system.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,12 +101,14 @@ export async function importEsmModule<T = unknown>(
export function pluginWorkDir(slug: string): string {
return join('node_modules', '.code-pushup', slug);
}

export async function crawlFileSystem<T = string>(options: {
export type CrawlFileSystemOptions<T> = {
directory: string;
pattern?: string | RegExp;
fileTransform?: (filePath: string) => Promise<T> | T;
}): Promise<T[]> {
};
export async function crawlFileSystem<T = string>(
options: CrawlFileSystemOptions<T>,
): Promise<T[]> {
const {
directory,
pattern,
Expand Down