-
-
Notifications
You must be signed in to change notification settings - Fork 3.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #11616 from Automattic/vkarpov15/ts-benchmark
Create TypeScript compilation benchmark that we run automatically on PRs
- Loading branch information
Showing
4 changed files
with
138 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
] | ||
} |