1
- import * as assert from 'node:assert' ;
2
- import * as cp from 'node:child_process' ;
3
- import * as fs from 'node:fs' ;
4
- import * as os from 'node:os' ;
5
- import * as path from 'node:path' ;
1
+ import assert from 'node:assert' ;
2
+ import cp from 'node:child_process' ;
3
+ import fs from 'node:fs' ;
4
+ import os from 'node:os' ;
5
+ import path from 'node:path' ;
6
+
7
+ import { exec , execOutput , localRepoPath } from './utils.js' ;
6
8
7
9
const NS_PER_SEC = 1e9 ;
8
10
const LOCAL = 'local' ;
@@ -12,28 +14,14 @@ const maxTime = 5;
12
14
// The minimum sample size required to perform statistical analysis.
13
15
const minSamples = 5 ;
14
16
15
- // Get the revisions and make things happen!
16
- if ( require . main === module ) {
17
+ async function runBenchmarks ( ) {
18
+ // Get the revisions and make things happen!
17
19
const { benchmarks, revisions } = getArguments ( process . argv . slice ( 2 ) ) ;
18
20
const benchmarkProjects = prepareBenchmarkProjects ( revisions ) ;
19
21
20
- runBenchmarks ( benchmarks , benchmarkProjects ) . catch ( ( error ) => {
21
- console . error ( error ) ;
22
- process . exit ( 1 ) ;
23
- } ) ;
24
- }
25
-
26
- function localDir ( ...paths : ReadonlyArray < string > ) {
27
- return path . join ( __dirname , '..' , ...paths ) ;
28
- }
29
-
30
- function exec ( command : string , options = { } ) {
31
- const result = cp . execSync ( command , {
32
- encoding : 'utf-8' ,
33
- stdio : [ 'inherit' , 'pipe' , 'inherit' ] ,
34
- ...options ,
35
- } ) ;
36
- return result ?. trimEnd ( ) ;
22
+ for ( const benchmark of benchmarks ) {
23
+ await runBenchmark ( benchmark , benchmarkProjects ) ;
24
+ }
37
25
}
38
26
39
27
interface BenchmarkProject {
@@ -59,7 +47,7 @@ function prepareBenchmarkProjects(
59
47
fs . rmSync ( projectPath , { recursive : true , force : true } ) ;
60
48
fs . mkdirSync ( projectPath ) ;
61
49
62
- fs . cpSync ( localDir ( 'benchmark' ) , path . join ( projectPath , 'benchmark' ) , {
50
+ fs . cpSync ( localRepoPath ( 'benchmark' ) , path . join ( projectPath , 'benchmark' ) , {
63
51
recursive : true ,
64
52
} ) ;
65
53
@@ -81,14 +69,14 @@ function prepareBenchmarkProjects(
81
69
82
70
function prepareNPMPackage ( revision : string ) {
83
71
if ( revision === LOCAL ) {
84
- const repoDir = localDir ( ) ;
72
+ const repoDir = localRepoPath ( ) ;
85
73
const archivePath = path . join ( tmpDir , 'graphql-local.tgz' ) ;
86
74
fs . renameSync ( buildNPMArchive ( repoDir ) , archivePath ) ;
87
75
return archivePath ;
88
76
}
89
77
90
78
// Returns the complete git hash for a given git revision reference.
91
- const hash = exec ( `git rev-parse "${ revision } "` ) ;
79
+ const hash = execOutput ( `git rev-parse "${ revision } "` ) ;
92
80
93
81
const archivePath = path . join ( tmpDir , `graphql-${ hash } .tgz` ) ;
94
82
if ( fs . existsSync ( archivePath ) ) {
@@ -109,7 +97,9 @@ function prepareBenchmarkProjects(
109
97
exec ( 'npm --quiet run build:npm' , { cwd : repoDir } ) ;
110
98
111
99
const distDir = path . join ( repoDir , 'npmDist' ) ;
112
- const archiveName = exec ( `npm --quiet pack ${ distDir } ` , { cwd : repoDir } ) ;
100
+ const archiveName = execOutput ( `npm --quiet pack ${ distDir } ` , {
101
+ cwd : repoDir ,
102
+ } ) ;
113
103
return path . join ( repoDir , archiveName ) ;
114
104
}
115
105
}
@@ -266,35 +256,33 @@ function maxBy<T>(array: ReadonlyArray<T>, fn: (obj: T) => number) {
266
256
}
267
257
268
258
// Prepare all revisions and run benchmarks matching a pattern against them.
269
- async function runBenchmarks (
270
- benchmarks : ReadonlyArray < string > ,
259
+ async function runBenchmark (
260
+ benchmark : string ,
271
261
benchmarkProjects : ReadonlyArray < BenchmarkProject > ,
272
262
) {
273
- for ( const benchmark of benchmarks ) {
274
- const results = [ ] ;
275
- for ( let i = 0 ; i < benchmarkProjects . length ; ++ i ) {
276
- const { revision, projectPath } = benchmarkProjects [ i ] ;
277
- const modulePath = path . join ( projectPath , benchmark ) ;
278
-
279
- if ( i === 0 ) {
280
- const { name } = await sampleModule ( modulePath ) ;
281
- console . log ( '⏱ ' + name ) ;
282
- }
263
+ const results = [ ] ;
264
+ for ( let i = 0 ; i < benchmarkProjects . length ; ++ i ) {
265
+ const { revision, projectPath } = benchmarkProjects [ i ] ;
266
+ const modulePath = path . join ( projectPath , benchmark ) ;
267
+
268
+ if ( i === 0 ) {
269
+ const { name } = await sampleModule ( modulePath ) ;
270
+ console . log ( '⏱ ' + name ) ;
271
+ }
283
272
284
- try {
285
- const samples = await collectSamples ( modulePath ) ;
273
+ try {
274
+ const samples = await collectSamples ( modulePath ) ;
286
275
287
- results . push ( computeStats ( revision , samples ) ) ;
288
- process . stdout . write ( ' ' + cyan ( i + 1 ) + ' tests completed.\u000D' ) ;
289
- } catch ( error ) {
290
- console . log ( ' ' + revision + ': ' + red ( String ( error ) ) ) ;
291
- }
276
+ results . push ( computeStats ( revision , samples ) ) ;
277
+ process . stdout . write ( ' ' + cyan ( i + 1 ) + ' tests completed.\u000D' ) ;
278
+ } catch ( error ) {
279
+ console . log ( ' ' + revision + ': ' + red ( String ( error ) ) ) ;
292
280
}
293
- console . log ( '\n' ) ;
294
-
295
- beautifyBenchmark ( results ) ;
296
- console . log ( '' ) ;
297
281
}
282
+ console . log ( '\n' ) ;
283
+
284
+ beautifyBenchmark ( results ) ;
285
+ console . log ( '' ) ;
298
286
}
299
287
300
288
function getArguments ( argv : ReadonlyArray < string > ) {
@@ -324,7 +312,7 @@ function getArguments(argv: ReadonlyArray<string>) {
324
312
325
313
function findAllBenchmarks ( ) {
326
314
return fs
327
- . readdirSync ( localDir ( 'benchmark' ) , { withFileTypes : true } )
315
+ . readdirSync ( localRepoPath ( 'benchmark' ) , { withFileTypes : true } )
328
316
. filter ( ( dirent ) => dirent . isFile ( ) )
329
317
. map ( ( dirent ) => dirent . name )
330
318
. filter ( ( name ) => name . endsWith ( '-benchmark.js' ) )
@@ -421,3 +409,5 @@ function sampleModule(modulePath: string): Promise<BenchmarkSample> {
421
409
} ) ;
422
410
} ) ;
423
411
}
412
+
413
+ await runBenchmarks ( ) ;
0 commit comments