-
Notifications
You must be signed in to change notification settings - Fork 2
/
benchmark
executable file
·202 lines (169 loc) · 6.67 KB
/
benchmark
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
#!/usr/bin/env node
const fs = require( 'fs' );
const path = require( 'path' );
const program = require( 'commander' );
const transformer = require( '../enketo-core/node_modules/enketo-transformer' );
const puppeteer = require( 'puppeteer' );
const graphite = require( 'graphite' );
const config = require( './config.json' );
const client = graphite.createClient( `plaintext://${config[ 'graphite url' ]}` );
const pkg = require( './package.json' );
program
.version( pkg.version )
.option( '-a, --all', 'benchmark all forms' )
.option( '-f, --filename [filename]', 'specific XForm file to benchmark' )
.parse( process.argv );
const options = program.opts();
if ( options.all ) {
console.log( '================================================================\nbenchmarking all forms\n================================================================' );
_getFileNames()
.then( fileNames => {
const funcs = fileNames.map( filename => () => _transform( filename )
.then( _writeHtmlFile )
.then( _render )
.then( _send )
.catch( e => console.error( e ) )
);
// run sequentially so they don't influence each other
return funcs.reduce( ( p, fn ) => p.then( fn ), Promise.resolve() )
.then( process.exit );
} )
.catch( _logError )
.then( process.exit );
} else if ( options.filename ) {
console.log( '================================================================\nbenchmarking %s\n================================================================', program.filename );
_transform( options.filename )
.then( _writeHtmlFile )
.then( _render )
.then( _send )
.catch( _logError )
.then( process.exit );
} else {
console.log( 'nothing to do' );
}
function _getFileNames() {
return new Promise( ( resolve, reject ) => {
fs.readdir( path.join( __dirname, 'forms' ), ( err, filenames ) => {
if ( err ) {
reject( err );
} else {
resolve( filenames.filter( filename => filename.lastIndexOf( '.xml' ) === filename.length - 4 ) );
}
} );
} );
}
function _transform( filename ) {
console.log( `transforming ${filename}...` );
const start = Date.now();
return _getFileContents( filename )
.then( transformer.transform )
.then( result => {
// add filename without extension
result.formId = filename.substring( 0, filename.length - 4 );
result.metrics = {};
result.metrics.transform = Date.now() - start;
return result;
} );
}
function _getFileContents( filename ) {
const filePath = path.join( __dirname, './forms', filename );
return new Promise( ( resolve, reject ) => {
fs.readFile( filePath, ( err, xform ) => {
if ( err ) {
reject( err );
} else {
resolve( {
xform
} );
}
} );
} );
}
async function _render( survey ) {
console.log( 'rendering...' );
const browser = await puppeteer.launch();
const closeBrowser = error => {
browser.close();
console.log( 'error occurred, closed browser' );
throw error;
}
const page = await browser.newPage();
// intercept any resources requested by the page and assume they are for external data so just return XML
await page.setRequestInterception( true );
page.on( 'request', interceptedRequest => {
if ( interceptedRequest.url().startsWith( 'jr:' ) ) {
interceptedRequest.abort();
} else {
interceptedRequest.continue();
}
} );
// render performance
const renderStart = Date.now();
const url = `file:${path.join( __dirname, config[ 'enketo core path' ], '/build/', survey.formId+'.html')}`;
await page.goto( url ).catch( closeBrowser );
survey.metrics.render = Date.now() - renderStart;
// save a screenshot, useful to check if the form actually loaded
const screenshotPath = path.join( __dirname, `screenshots/${survey.formId}.png` );
await page.screenshot( { path: screenshotPath } ).catch( closeBrowser );
// validation performance
const validationStart = Date.now()
await page.evaluate( () => window.form.validateAll() );
survey.metrics.validate = Date.now() - validationStart;
await browser.close();
return survey;
}
function _writeHtmlFile( survey ) {
const filePath = path.join( __dirname, config[ 'enketo core path' ], '/build', `${survey.formId}.html` );
console.log( 'write to static html file', filePath );
return new Promise( ( resolve, reject ) => {
_getBaseHtml()
.then( html => {
// clean up model to allow it be a javascript variable
survey.model = survey.model.replace( /\n/g, "" ).replace( /\"/g, "'" );
html = html.replace( /\/\/\s*{{globalModelStr}}/m, `var globalModelStr = "${survey.model}"` );
html = html.replace( "<!-- {{<form>}} -->" /*/<!--\s*<form>\s*-->/m*/ , survey.form );
// write to a temporary html file in the enketo-core folder that has the correct relative resource references
fs.writeFile( filePath, html, err => {
if ( err ) {
reject( err );
} else {
resolve( survey );
}
} );
} );
} );
}
function _getBaseHtml() {
return new Promise( ( resolve, reject ) => {
fs.readFile( path.join( path.join( __dirname, config[ 'enketo core path' ], '/build/index.html' ) ), ( err, html ) => {
if ( err ) {
reject( err );
} else {
resolve( html.toString() );
}
} );
} );
}
function _send( survey ) {
const metrics = {};
metrics[ [ `${config[ 'data prefix' ]}.${survey.formId}` ] ] = survey.metrics;
console.log( 'sending data to graphite...', metrics );
return new Promise( function( resolve, reject ) {
client.write( metrics, function( err ) {
if ( err ) {
reject( err );
} else {
console.log( 'sent data successfully!\n----------------------------------------------------------------' );
resolve();
}
} );
} );
//return Promise.resolve();
}
function _logError( error ) {
if ( error.code === 'ENOENT' ) {
console.error( `${options.filename} could not be found` );
} else {
console.error( 'error occurred', error );
}
}