@@ -9,19 +9,21 @@ const { Suite } = require('benchmark');
9
9
const beautifyBenchmark = require ( 'beautify-benchmark' ) ;
10
10
const { execSync } = require ( 'child_process' ) ;
11
11
const os = require ( 'os' ) ;
12
+ const fs = require ( 'fs' ) ;
12
13
const path = require ( 'path' ) ;
13
14
14
15
// Like build:cjs, but includes __tests__ and copies other files.
15
16
const BUILD_CMD = 'babel src --optional runtime --copy-files --out-dir dist/' ;
16
17
const LOCAL = 'local' ;
17
- const LOCAL_DIR = path . join ( __dirname , '../' ) ;
18
- const TEMP_DIR = os . tmpdir ( ) ;
18
+ function LOCAL_DIR ( ...paths ) {
19
+ return path . join ( __dirname , '..' , ...paths ) ;
20
+ }
21
+ function TEMP_DIR ( ...paths ) {
22
+ return path . join ( os . tmpdir ( ) , 'graphql-js-benchmark' , ...paths ) ;
23
+ }
19
24
20
25
// Returns the complete git hash for a given git revision reference.
21
26
function hashForRevision ( revision ) {
22
- if ( revision === LOCAL ) {
23
- return revision ;
24
- }
25
27
const out = execSync ( `git rev-parse "${ revision } "` , { encoding : 'utf8' } ) ;
26
28
const match = / [ 0 - 9 a - f ] { 8 , 40 } / . exec ( out ) ;
27
29
if ( ! match ) {
@@ -30,54 +32,47 @@ function hashForRevision(revision) {
30
32
return match [ 0 ] ;
31
33
}
32
34
33
- // Returns the temporary directory which hosts the files for this git hash.
34
- function dirForHash ( hash ) {
35
- if ( hash === LOCAL ) {
36
- return path . join ( __dirname , '../' ) ;
37
- }
38
- return path . join ( TEMP_DIR , 'graphql-js-benchmark' , hash ) ;
39
- }
40
-
41
- // Build a benchmarkable environment for the given revision.
35
+ // Build a benchmarkable environment for the given revision
36
+ // and returns path to its 'dist' directory.
42
37
function prepareRevision ( revision ) {
43
38
console . log ( `🍳 Preparing ${ revision } ...` ) ;
44
- const hash = hashForRevision ( revision ) ;
45
- const dir = dirForHash ( hash ) ;
46
- if ( hash === LOCAL ) {
47
- execSync ( `(cd " ${ dir } " && yarn run ${ BUILD_CMD } )` ) ;
39
+
40
+ if ( revision === LOCAL ) {
41
+ execSync ( `yarn run ${ BUILD_CMD } ` ) ;
42
+ return LOCAL_DIR ( 'dist' ) ;
48
43
} else {
49
- execSync ( `
50
- if [ ! -d "${ dir } " ]; then
51
- mkdir -p "${ dir } " &&
52
- git archive "${ hash } " | tar -xC "${ dir } " &&
53
- (cd "${ dir } " && yarn install);
54
- fi &&
55
- # Copy in local tests so the same logic applies to each revision.
56
- for file in $(cd "${ LOCAL_DIR } src"; find . -path '*/__tests__/*');
57
- do cp "${ LOCAL_DIR } src/$file" "${ dir } /src/$file";
58
- done &&
59
- (cd "${ dir } " && yarn run ${ BUILD_CMD } )
60
- ` ) ;
44
+ if ( ! fs . existsSync ( TEMP_DIR ( ) ) ) {
45
+ fs . mkdirSync ( TEMP_DIR ( ) ) ;
46
+ }
47
+
48
+ const hash = hashForRevision ( revision ) ;
49
+ const dir = TEMP_DIR ( hash ) ;
50
+
51
+ if ( ! fs . existsSync ( dir ) ) {
52
+ fs . mkdirSync ( dir ) ;
53
+ execSync ( `git archive "${ hash } " | tar -xC "${ dir } "` ) ;
54
+ execSync ( 'yarn install' , { cwd : dir } ) ;
55
+ }
56
+ for ( const file of findFiles ( LOCAL_DIR ( 'src' ) , '*/__tests__/*' ) ) {
57
+ const from = LOCAL_DIR ( 'src' , file ) ;
58
+ const to = path . join ( dir , 'src' , file ) ;
59
+ fs . copyFileSync ( from , to ) ;
60
+ }
61
+ execSync ( `yarn run ${ BUILD_CMD } ` , { cwd : dir } ) ;
62
+
63
+ return path . join ( dir , 'dist' ) ;
61
64
}
62
65
}
63
66
64
- // Find all benchmark tests to be run.
65
- function findBenchmarks ( ) {
66
- const out = execSync (
67
- `(cd ${ LOCAL_DIR } src; find . -path '*/__tests__/*-benchmark.js')` ,
68
- { encoding : 'utf8' } ,
69
- ) ;
67
+ function findFiles ( cwd , pattern ) {
68
+ const out = execSync ( `find . -path '${ pattern } '` , { cwd, encoding : 'utf8' } ) ;
70
69
return out . split ( '\n' ) . filter ( Boolean ) ;
71
70
}
72
71
73
72
// Run a given benchmark test with the provided revisions.
74
- function runBenchmark ( benchmark , revisions ) {
75
- const modules = revisions . map ( revision =>
76
- require ( path . join (
77
- dirForHash ( hashForRevision ( revision ) ) ,
78
- 'dist' ,
79
- benchmark ,
80
- ) ) ,
73
+ function runBenchmark ( benchmark , enviroments ) {
74
+ const modules = enviroments . map ( ( { distPath} ) =>
75
+ require ( path . join ( distPath , benchmark ) ) ,
81
76
) ;
82
77
const suite = new Suite ( modules [ 0 ] . name , {
83
78
onStart ( event ) {
@@ -94,28 +89,36 @@ function runBenchmark(benchmark, revisions) {
94
89
beautifyBenchmark . log ( ) ;
95
90
} ,
96
91
} ) ;
97
- for ( let i = 0 ; i < revisions . length ; i ++ ) {
98
- suite . add ( revisions [ i ] , modules [ i ] . measure ) ;
92
+ for ( let i = 0 ; i < enviroments . length ; i ++ ) {
93
+ suite . add ( enviroments [ i ] . revision , modules [ i ] . measure ) ;
99
94
}
100
95
suite . run ( { async : false } ) ;
101
96
}
102
97
103
98
// Prepare all revisions and run benchmarks matching a pattern against them.
104
99
function prepareAndRunBenchmarks ( benchmarkPatterns , revisions ) {
105
- const benchmarks = findBenchmarks ( ) . filter (
106
- benchmark =>
107
- benchmarkPatterns . length === 0 ||
108
- benchmarkPatterns . some ( pattern => benchmark . indexOf ( pattern ) !== - 1 ) ,
109
- ) ;
100
+ // Find all benchmark tests to be run.
101
+ let benchmarks = findFiles ( LOCAL_DIR ( 'src' ) , '*/__tests__/*-benchmark.js' ) ;
102
+ if ( benchmarkPatterns . length !== 0 ) {
103
+ benchmarks = benchmarks . filter (
104
+ benchmark => benchmarkPatterns . some (
105
+ pattern => path . join ( 'src' , benchmark ) . includes ( pattern )
106
+ ) ,
107
+ ) ;
108
+ }
109
+
110
110
if ( benchmarks . length === 0 ) {
111
111
console . warn (
112
112
'No benchmarks matching: ' +
113
113
`\u001b[1m${ benchmarkPatterns . join ( '\u001b[0m or \u001b[1m' ) } \u001b[0m` ,
114
114
) ;
115
115
return ;
116
116
}
117
- revisions . forEach ( revision => prepareRevision ( revision ) ) ;
118
- benchmarks . forEach ( benchmark => runBenchmark ( benchmark , revisions ) ) ;
117
+
118
+ const enviroments = revisions . map (
119
+ revision => ( { revision, distPath : prepareRevision ( revision ) } )
120
+ ) ;
121
+ benchmarks . forEach ( benchmark => runBenchmark ( benchmark , enviroments ) ) ;
119
122
}
120
123
121
124
function getArguments ( argv ) {
0 commit comments