Skip to content

Commit

Permalink
Merge pull request #27 from time-loop/mike-m/add-test-data-to-repo
Browse files Browse the repository at this point in the history
chore(fields): add pregenerated test data
  • Loading branch information
mysza authored Sep 27, 2024
2 parents 5928b7f + df57cbe commit 9c34e04
Show file tree
Hide file tree
Showing 6 changed files with 118 additions and 90 deletions.
1 change: 0 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,5 @@ jobs:
run: npm run lint
- name: Build and Test
run: |
npm run test:generate
npm run test
npm run build
17 changes: 17 additions & 0 deletions jest-perf.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/** @type {import('jest').Config} */
const config = {
preset: 'ts-jest',
testEnvironment: 'node',
setupFilesAfterEnv: ['./test/jest.setup.js'],
transform: {
'^.+\\.[tj]s$': 'ts-jest',
},
transformIgnorePatterns: ['^.+/.js$'],
testRegex: '/test/unit/clickup/clickupFieldsDependencyTracker.perf.test.ts$',
testPathIgnorePatterns: ['/node_modules/', '/dist/', '/test/_utils/', '/test/scripts', '/test/jest.setup.js'],
collectCoverageFrom: ['**/*.[tj]s', '!src/grammar-parser/**'],
};

process.env.TZ = 'UTC';

module.exports = config;
9 changes: 8 additions & 1 deletion jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,14 @@ const config = {
},
testRegex: '(/test/.*.(t|j)s)$',
transformIgnorePatterns: ['^.+/.js$'],
testPathIgnorePatterns: ['/node_modules/', '/dist/', '/test/_utils/', '/test/scripts', '/test/jest.setup.js'],
testPathIgnorePatterns: [
'/node_modules/',
'/dist/',
'/test/_utils/',
'/test/scripts',
'/test/jest.setup.js',
'/test/unit/clickup/clickupFieldsDependencyTracker.perf.test.ts',
],
collectCoverageFrom: ['**/*.[tj]s', '!src/grammar-parser/**'],
};

Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"test:clear": "jest --clearCache",
"test": "jest --detectOpenHandles",
"test:coverage": "jest --coverage",
"test:perf": "jest --config=jest-perf.config.js",
"test:generate": "ts-node test/scripts/generatePerfTestData.ts",
"build": "tsc",
"generate-parser": "cd src/grammar-parser && jison grammar-parser.jison",
Expand Down
91 changes: 91 additions & 0 deletions test/unit/clickup/clickupFieldsDependencyTracker.perf.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
import fs from 'fs';
import path from 'path';
import { ClickUpFieldsDependencyTracker } from '../../../src';
import { ClickUpParserVariable } from '../../../src/clickup/clickupParserVariable';

describe('ClickupFieldsDependencyTracker', () => {
describe('performance tests', () => {
interface AugmentedTracker {
getDependencyGraph(): unknown;
}

const filePath = path.join(process.cwd(), 'test', 'data', 'test_custom_fields.json');
const data = fs.readFileSync(filePath, 'utf-8');
const variables = JSON.parse(data);

function getVariables(): ClickUpParserVariable[] {
return [...variables];
}

function getRandomSample<T>(arr: T[], sampleSize: number): T[] {
if (sampleSize > arr.length) {
throw new Error('Sample size cannot be larger than the array size.');
}

// Create a copy of the array to avoid modifying the original array
const arrayCopy = arr.slice();

// Fisher-Yates shuffle, optimized to shuffle only up to sampleSize
for (let i = 0; i < sampleSize; i++) {
const j = Math.floor(Math.random() * (arr.length - i)) + i;
[arrayCopy[i], arrayCopy[j]] = [arrayCopy[j], arrayCopy[i]];
}
// Return the first N elements of the shuffled array
return arrayCopy.slice(0, sampleSize);
}
it('graph creation should be fast enough', () => {
const start = performance.now();
const iterations = 10;
for (let i = 0; i < iterations; i++) {
const variables = getVariables();
const tracker = new ClickUpFieldsDependencyTracker(variables) as unknown as AugmentedTracker;
const graph = tracker.getDependencyGraph();
expect(graph).toBeDefined();
}
const timeAverage = (performance.now() - start) / iterations;

expect(timeAverage).toBeLessThan(250);
console.log(`Graph creation for ${variables.length} variables (average time): ${timeAverage} ms`);
});

it('dependencies validation should be fast enough', () => {
const variables = getVariables();
const validator = new ClickUpFieldsDependencyTracker(variables);
{
// initialze the graph outside of the measurement
const augmented = validator as unknown as AugmentedTracker;
augmented.getDependencyGraph();
}

const start = performance.now();
const iterations = 100;
for (let i = 0; i < iterations; i++) {
validator.validate();
}
const time = (performance.now() - start) / iterations;

expect(time).toBeLessThan(250);
console.log(`Dependencies validation for ${variables.length} variables (average time): ${time} ms`);
});

it('fetching dependants should be fast enough', () => {
const variables = getVariables();
const tracker = new ClickUpFieldsDependencyTracker(variables);
{
// initialze the graph outside of the measurement
const augmented = tracker as unknown as AugmentedTracker;
augmented.getDependencyGraph();
}

const varsSample = getRandomSample(variables, 1000);
const start = performance.now();
for (const variable of varsSample) {
tracker.getDependentFields(variable.name);
}
const time = (performance.now() - start) / variables.length;

expect(time).toBeLessThan(250);
console.log(`Fetching dependants for ${variables.length} variables (average time): ${time} ms`);
});
});
});
89 changes: 1 addition & 88 deletions test/unit/clickup/clickupFieldsDependencyTracker.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import fs from 'fs';
import path from 'path';
import { ClickUpFieldsDependencyTracker } from '../../../src/clickup/clickupFieldsDependencyTracker';
import { ClickUpParserVariable, createClickUpParserVariable } from '../../../src/clickup/clickupParserVariable';
import { createClickUpParserVariable } from '../../../src/clickup/clickupParserVariable';

describe('clickupFieldsValidator', () => {
const createName = (id: string) => `CUSTOM_FIELD_${id}`;
Expand Down Expand Up @@ -87,89 +85,4 @@ describe('clickupFieldsValidator', () => {
}),
});
});

describe('performance tests', () => {
interface AugmentedTracker {
getDependencyGraph(): unknown;
}

const filePath = path.join(process.cwd(), 'test', 'data', 'test_custom_fields.json');
const data = fs.readFileSync(filePath, 'utf-8');
const variables = JSON.parse(data);

function getVariables(): ClickUpParserVariable[] {
return [...variables];
}

function getRandomSample<T>(arr: T[], sampleSize: number): T[] {
if (sampleSize > arr.length) {
throw new Error('Sample size cannot be larger than the array size.');
}

// Create a copy of the array to avoid modifying the original array
const arrayCopy = arr.slice();

// Fisher-Yates shuffle, optimized to shuffle only up to sampleSize
for (let i = 0; i < sampleSize; i++) {
const j = Math.floor(Math.random() * (arr.length - i)) + i;
[arrayCopy[i], arrayCopy[j]] = [arrayCopy[j], arrayCopy[i]];
}
// Return the first N elements of the shuffled array
return arrayCopy.slice(0, sampleSize);
}
it('graph creation should be fast enough', () => {
const start = performance.now();
const iterations = 10;
for (let i = 0; i < iterations; i++) {
const variables = getVariables();
const tracker = new ClickUpFieldsDependencyTracker(variables) as unknown as AugmentedTracker;
const graph = tracker.getDependencyGraph();
expect(graph).toBeDefined();
}
const timeAverage = (performance.now() - start) / iterations;

expect(timeAverage).toBeLessThan(250);
console.log(`Graph creation for ${variables.length} variables (average time): ${timeAverage} ms`);
});

it('dependencies validation should be fast enough', () => {
const variables = getVariables();
const validator = new ClickUpFieldsDependencyTracker(variables);
{
// initialze the graph outside of the measurement
const augmented = validator as unknown as AugmentedTracker;
augmented.getDependencyGraph();
}

const start = performance.now();
const iterations = 100;
for (let i = 0; i < iterations; i++) {
validator.validate();
}
const time = (performance.now() - start) / iterations;

expect(time).toBeLessThan(250);
console.log(`Dependencies validation for ${variables.length} variables (average time): ${time} ms`);
});

it('fetching dependants should be fast enough', () => {
const variables = getVariables();
const tracker = new ClickUpFieldsDependencyTracker(variables);
{
// initialze the graph outside of the measurement
const augmented = tracker as unknown as AugmentedTracker;
augmented.getDependencyGraph();
}

const varsSample = getRandomSample(variables, 1000);
const start = performance.now();
for (const variable of varsSample) {
tracker.getDependentFields(variable.name);
}
const time = (performance.now() - start) / variables.length;

expect(time).toBeLessThan(250);
console.log(`Fetching dependants for ${variables.length} variables (average time): ${time} ms`);
});
});
});

0 comments on commit 9c34e04

Please sign in to comment.