@@ -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,55 +32,48 @@ 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
- cp -R "${ LOCAL_DIR } /src/__fixtures__" "${ dir } /src/__fixtures__" &&
60
- (cd "${ dir } " && yarn run ${ BUILD_CMD } )
61
- ` ) ;
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 ( `cp -R "${ LOCAL_DIR ( ) } /src/__fixtures__/" "${ dir } /src/__fixtures__/"` ) ;
62
+ execSync ( `yarn run ${ BUILD_CMD } ` , { cwd : dir } ) ;
63
+
64
+ return path . join ( dir , 'dist' ) ;
62
65
}
63
66
}
64
67
65
- // Find all benchmark tests to be run.
66
- function findBenchmarks ( ) {
67
- const out = execSync (
68
- `(cd ${ LOCAL_DIR } src; find . -path '*/__tests__/*-benchmark.js')` ,
69
- { encoding : 'utf8' } ,
70
- ) ;
68
+ function findFiles ( cwd , pattern ) {
69
+ const out = execSync ( `find . -path '${ pattern } '` , { cwd, encoding : 'utf8' } ) ;
71
70
return out . split ( '\n' ) . filter ( Boolean ) ;
72
71
}
73
72
74
73
// Run a given benchmark test with the provided revisions.
75
- function runBenchmark ( benchmark , revisions ) {
76
- const modules = revisions . map ( revision =>
77
- require ( path . join (
78
- dirForHash ( hashForRevision ( revision ) ) ,
79
- 'dist' ,
80
- benchmark ,
81
- ) ) ,
74
+ function runBenchmark ( benchmark , enviroments ) {
75
+ const modules = enviroments . map ( ( { distPath} ) =>
76
+ require ( path . join ( distPath , benchmark ) ) ,
82
77
) ;
83
78
const suite = new Suite ( modules [ 0 ] . name , {
84
79
onStart ( event ) {
@@ -95,28 +90,36 @@ function runBenchmark(benchmark, revisions) {
95
90
beautifyBenchmark . log ( ) ;
96
91
} ,
97
92
} ) ;
98
- for ( let i = 0 ; i < revisions . length ; i ++ ) {
99
- suite . add ( revisions [ i ] , modules [ i ] . measure ) ;
93
+ for ( let i = 0 ; i < enviroments . length ; i ++ ) {
94
+ suite . add ( enviroments [ i ] . revision , modules [ i ] . measure ) ;
100
95
}
101
96
suite . run ( { async : false } ) ;
102
97
}
103
98
104
99
// Prepare all revisions and run benchmarks matching a pattern against them.
105
100
function prepareAndRunBenchmarks ( benchmarkPatterns , revisions ) {
106
- const benchmarks = findBenchmarks ( ) . filter (
107
- benchmark =>
108
- benchmarkPatterns . length === 0 ||
109
- benchmarkPatterns . some ( pattern => benchmark . indexOf ( pattern ) !== - 1 ) ,
110
- ) ;
101
+ // Find all benchmark tests to be run.
102
+ let benchmarks = findFiles ( LOCAL_DIR ( 'src' ) , '*/__tests__/*-benchmark.js' ) ;
103
+ if ( benchmarkPatterns . length !== 0 ) {
104
+ benchmarks = benchmarks . filter (
105
+ benchmark => benchmarkPatterns . some (
106
+ pattern => path . join ( 'src' , benchmark ) . includes ( pattern )
107
+ ) ,
108
+ ) ;
109
+ }
110
+
111
111
if ( benchmarks . length === 0 ) {
112
112
console . warn (
113
113
'No benchmarks matching: ' +
114
114
`\u001b[1m${ benchmarkPatterns . join ( '\u001b[0m or \u001b[1m' ) } \u001b[0m` ,
115
115
) ;
116
116
return ;
117
117
}
118
- revisions . forEach ( revision => prepareRevision ( revision ) ) ;
119
- benchmarks . forEach ( benchmark => runBenchmark ( benchmark , revisions ) ) ;
118
+
119
+ const enviroments = revisions . map (
120
+ revision => ( { revision, distPath : prepareRevision ( revision ) } )
121
+ ) ;
122
+ benchmarks . forEach ( benchmark => runBenchmark ( benchmark , enviroments ) ) ;
120
123
}
121
124
122
125
function getArguments ( argv ) {
0 commit comments