Skip to content

Commit

Permalink
Normalize file paths in file-processor.ts and file-processor.test.ts
Browse files Browse the repository at this point in the history
  • Loading branch information
gmickel committed Jul 20, 2024
1 parent 642f6d1 commit df273cd
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 14 deletions.
8 changes: 6 additions & 2 deletions src/core/file-processor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { isBinaryFile } from 'isbinaryfile';
import micromatch from 'micromatch';
import Piscina from 'piscina';
import { FileCache } from '../utils/file-cache';
import { normalizePath } from '../utils/normalize-path';

export interface FileInfo {
path: string;
Expand Down Expand Up @@ -193,7 +194,7 @@ export async function processFiles(
cachePromises.push(
(async () => {
try {
const cached = await fileCache.get(filePath);
const cached = await fileCache.get(normalizePath(filePathStr));
if (cached) {
fileInfos.push(cached);
return; // Skip processing and setting cache for cached files
Expand All @@ -212,7 +213,10 @@ export async function processFiles(
});

if (result) {
await fileCache.set(filePathStr, result as FileInfo);
await fileCache.set(
normalizePath(filePathStr),
result as FileInfo,
);
fileInfos.push(result as FileInfo);
}
} catch (error) {
Expand Down
3 changes: 3 additions & 0 deletions src/utils/normalize-path.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export function normalizePath(filePath: string): string {
return filePath.replace(/\\/g, '/');
}
35 changes: 27 additions & 8 deletions tests/performance/file-processor.perf.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,26 +86,45 @@ describe.sequential('Performance Tests', () => {
await createTestFiles(1000, 10, 'cache'); // Increased to 1000 files

console.log('First run (no cache):');
const [firstRunFiles, firstRunTime] = await runProcessFiles();
const startFirstRun = performance.now();
const firstRunFiles = await processFiles({
path: TEST_DIR,
cachePath: DEFAULT_CACHE_PATH,
});
const endFirstRun = performance.now();
const firstRunDuration = endFirstRun - startFirstRun;
console.log(`First run duration: ${firstRunDuration} ms`);
expect(firstRunFiles).toHaveLength(1000);

console.log('Second run (with cache):');
const [secondRunFiles, secondRunTime] = await runProcessFiles();
const startSecondRun = performance.now();
const secondRunFiles = await processFiles({
path: TEST_DIR,
cachePath: DEFAULT_CACHE_PATH,
});
const endSecondRun = performance.now();
const secondRunDuration = endSecondRun - startSecondRun;
console.log(`Second run duration: ${secondRunDuration} ms`);

console.log(`First run file count: ${firstRunFiles.length}`);
console.log(`Second run file count: ${secondRunFiles.length}`);

expect(secondRunFiles).toHaveLength(firstRunFiles.length);

console.log(`First run time: ${firstRunTime} ms`);
console.log(`Second run time: ${secondRunTime} ms`);
console.log(`Time saved: ${firstRunTime - secondRunTime} ms`);
console.log(`First run time: ${firstRunDuration} ms`);
console.log(`Second run time: ${secondRunDuration} ms`);
console.log(`Time saved: ${firstRunDuration - secondRunDuration} ms`);
console.log(
`Percentage faster: ${(((firstRunTime - secondRunTime) / firstRunTime) * 100).toFixed(2)}%`,
`Percentage faster: ${(((firstRunDuration - secondRunDuration) / firstRunDuration) * 100).toFixed(2)}%`,
);

expect(secondRunTime).toBeLessThan(firstRunTime);
expect(secondRunTime).toBeLessThan(firstRunTime * 0.7);
// Change this to ensure the improvement is noticeable across platforms
const improvementPercentage =
((firstRunDuration - secondRunDuration) / firstRunDuration) * 100;
console.log(`Improvement Percentage: ${improvementPercentage}%`);

expect(secondRunDuration).toBeLessThan(firstRunDuration);
expect(improvementPercentage).toBeGreaterThan(30); // Adjust as per your requirements and observed performance patterns
}, 60000);

it('should handle a mix of file sizes efficiently', async () => {
Expand Down
5 changes: 1 addition & 4 deletions tests/unit/file-processor.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,12 @@ import Piscina from 'piscina';
import { beforeEach, describe, expect, it, vi } from 'vitest';
import { type FileInfo, processFiles } from '../../src/core/file-processor';
import { FileCache } from '../../src/utils/file-cache';
import { normalizePath } from '../../src/utils/normalize-path';

vi.mock('fast-glob');
vi.mock('piscina');
vi.mock('../../src/utils/file-cache');

function normalizePath(filePath: string): string {
return filePath.replace(/\\/g, '/');
}

describe('processFiles', () => {
const fixturesPath = path.resolve(__dirname, '../fixtures/test-project');
const normalizedFixturesPath = path.normalize(fixturesPath);
Expand Down

0 comments on commit df273cd

Please sign in to comment.