Skip to content

Commit

Permalink
Adding type benchmarking - finds time and number of instantiations fo…
Browse files Browse the repository at this point in the history
…r the type
  • Loading branch information
bcd00 committed Jan 5, 2024
1 parent 6f7543e commit 0dc11f1
Show file tree
Hide file tree
Showing 12 changed files with 129 additions and 37 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,8 @@ sandworm

/tests/**/private.*

benchmarks.json

# typescript-coverage-report
/coverage-ts

Expand Down
3 changes: 3 additions & 0 deletions apps/ove-core-ui/src/pages/dashboard/page.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
const Dashboard = () => <></>;

export default Dashboard;
7 changes: 6 additions & 1 deletion babel.config.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
{
"babelrcRoots": ["*"]
"babelrcRoots": ["*"],
"env": {
"test": {
"plugins": ["@babel/plugin-transform-modules-commonjs"]
}
}
}
2 changes: 1 addition & 1 deletion jest.config.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const { getJestProjects } = require('@nrwl/jest');

module.exports = {
projects: getJestProjects(),
projects: getJestProjects()
};
2 changes: 1 addition & 1 deletion jest.config.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { getJestProjects } from '@nrwl/jest';

export default {
projects: getJestProjects(),
projects: getJestProjects()
};
8 changes: 7 additions & 1 deletion jest.integration.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ export default {
{
useESM: true
}
],
"node_modules/@arktype": [
"babel-jest"
]
}
},
transformIgnorePatterns: [
"node_modules/(?!@arktype)"
]
};
2 changes: 1 addition & 1 deletion jest.preset.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const nxPreset = require('@nrwl/jest/preset').default;

module.exports = {
...nxPreset,
...nxPreset
};
6 changes: 6 additions & 0 deletions libs/ove-types/jest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,13 @@ export default {
tsconfig: '<rootDir>/tsconfig.spec.json',
},
],
"@arktype": [
"babel-jest"
]
},
transformIgnorePatterns: [
"<rootDir>/node_modules/(?!@arktype)"
],
moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx'],
coverageDirectory: '../../coverage/libs/ove-types',
};
66 changes: 66 additions & 0 deletions libs/ove-types/src/lib/hardware/service.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import { bench } from "@arktype/attest";
import { writeFileSync, readFileSync, statSync } from "fs";
import { type RouteMethod, type TServiceRoutesSchema } from "./service";

const formatOutput = () => {
const results = (console.log as ReturnType<typeof jest.fn>).mock.calls.map(x => x[0] as string).filter(o => o.includes("Result: ")).map(o => o.match(/Result: ([\d|.]+)/)![1]);
return {
time: parseFloat(results[0]),
instantiations: parseInt(results[1])
};
};

const init = () => {
console.log = jest.fn();
console.group = jest.fn();
console.error = jest.fn();
};

describe("service types", () => {
let benchmarks: Record<string, { time: number, instantiations: number }> = {};

it("RouteMethod", async () => {
init();
bench("RouteMethod", () => "" as RouteMethod).mean([0, "ns"]).types([0, "instantiations"]);
const {
time,
instantiations
} = formatOutput();

benchmarks["RouteMethod"] = { time, instantiations };

expect(time).toBeLessThan(50);
expect(instantiations).toBe(0);
});

it("getStatus", async () => {
init();
bench("getStatus", () => ({}) as TServiceRoutesSchema["getStatus"]).mean([0, "ns"]).types([0, "instantiations"]);
const {
time,
instantiations
} = formatOutput();

benchmarks["getStatus"] = { time, instantiations };

expect(time).toBeLessThan(50);
expect(instantiations).toBeLessThan(33000);
});

afterAll(() => {
let existing: Record<string, any>
try {
statSync("./benchmarks.json");
existing = JSON.parse(readFileSync("./benchmarks.json").toString());
} catch (e) {
console.error(e);
existing = {};
}

if (!("ove-types" in existing)) {
existing["ove-types"] = {};
}
existing["ove-types"]["service"] = benchmarks;
writeFileSync("./benchmarks.json", JSON.stringify(existing, undefined, 2));
});
});
6 changes: 4 additions & 2 deletions libs/ove-types/tsconfig.spec.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
{
"exclude": ["../../node_modules"],
"extends": "./tsconfig.json",
"compilerOptions": {
"outDir": "../../dist/out-tsc",
"noEmit": true,
"module": "commonjs",
"types": ["jest", "node"]
"types": ["jest", "node"],
"allowJs": true
},
"include": [
"jest.config.ts",
Expand Down
61 changes: 31 additions & 30 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
"devDependencies": {
"@arktype/attest": "^0.5.0",
"@babel/preset-react": "^7.14.5",
"@babel/plugin-transform-modules-commonjs": "^7.23.3",
"@lhci/cli": "^0.13.0",
"@lhci/server": "^0.13.0",
"@mands/nx-playwright": "^0.1.19",
Expand Down

0 comments on commit 0dc11f1

Please sign in to comment.