Skip to content

Commit

Permalink
Merge pull request #11616 from Automattic/vkarpov15/ts-benchmark
Browse files Browse the repository at this point in the history
Create TypeScript compilation benchmark that we run automatically on PRs
  • Loading branch information
vkarpov15 committed Apr 4, 2022
2 parents eaa445e + 8e5e99a commit 2bf039d
Show file tree
Hide file tree
Showing 4 changed files with 138 additions and 0 deletions.
21 changes: 21 additions & 0 deletions .github/workflows/benchmark.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
name: Benchmark
on:
push:

jobs:
typescript:
runs-on: ubuntu-20.04
name: TypeScript
steps:
- uses: actions/checkout@v3

- name: Setup node
uses: actions/setup-node@v3
with:
node-version: 14

- run: npm install

- run: node ./benchmarks/typescript
env:
DB_URL: ${{ secrets.DB_URL }}
82 changes: 82 additions & 0 deletions benchmarks/typescript.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
'use strict';

const { exec } = require('child_process');
const mongoose = require('../');

const numIterations = 10;

let instantiations = 0;
let memoryUsed = 0;
let checkTime = 0;
let totalTime = 0;

run().catch(err => {
console.error(err);
process.exit(-1);
});

async function run() {
for (let i = 0; i < numIterations; ++i) {
console.log(`${i}...`);
const { stdout } = await new Promise((resolve, reject) => {
exec('../../../node_modules/.bin/tsc --extendedDiagnostics', { cwd: `${__dirname}/typescript/simple` }, (err, stdout, stderr) => {
if (err) {
console.error(err);
console.error(stdout);
console.error(stderr);
return reject(err);
}

resolve({ stdout });
});
});

const lines = stdout.split('\n');

const _instantiations = +lines.find(line => line.startsWith('Instantiations:')).match(/\d+/)[0];
const _memoryUsed = +lines.find(line => line.startsWith('Memory used:')).match(/\d+/)[0];
const _checkTime = +lines.find(line => line.startsWith('Check time:')).match(/\d+(\.\d+)?/)[0];
const _totalTime = +lines.find(line => line.startsWith('Total time:')).match(/\d+(\.\d+)?/)[0];

instantiations += _instantiations;
memoryUsed += _memoryUsed;
checkTime += _checkTime;
totalTime += _totalTime;
}

instantiations /= numIterations;
memoryUsed /= numIterations;
checkTime /= numIterations;
totalTime /= numIterations;
console.log(instantiations);
console.log(memoryUsed);
console.log(checkTime);
console.log(totalTime);

await persist({ instantiations, memoryUsed, checkTime, totalTime });
}

async function persist(results) {
if (!process.env.DB_URL) {
return;
}
if (!process.env.GITHUB_SHA) {
return;
}

await mongoose.connect(process.env.DB_URL);

const BenchmarkResult = mongoose.model('BenchmarkResult', mongoose.Schema({
githash: { type: String, required: true },
benchmarkName: { type: String, required: true },
results: 'Mixed'
}, { timestamps: true }), 'BenchmarkResult');

await BenchmarkResult.findOneAndUpdate(
{ githash: process.env.GITHUB_SHA, benchmarkName: 'TypeScript' },
{ results },
{ upsert: true }
);

await mongoose.disconnect();
}
19 changes: 19 additions & 0 deletions benchmarks/typescript/simple/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { Schema, Model, model } from 'mongoose';

interface User {
name: string;
email: string;
avatar?: string;
}

interface UserModelInterface extends Model<User> {
fetchUser(name: string): Promise<User>;
}

const schema = new Schema<User>({
name: { type: String, required: true },
email: { type: String, required: true },
avatar: String
});

const UserModel = model<User, UserModelInterface>('User', schema);
16 changes: 16 additions & 0 deletions benchmarks/typescript/simple/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"compilerOptions": {
"allowSyntheticDefaultImports": true,
"baseUrl": ".",
"paths": {
"mongoose": ["../../../"]
}
},
"esModuleInterop": true,
"outDir": "typescript",
"strictNullChecks": true,
"include": [
"./*.ts",
"../../../index.d.ts"
]
}

0 comments on commit 2bf039d

Please sign in to comment.