-
Notifications
You must be signed in to change notification settings - Fork 1
/
ajaxracer.js
executable file
·128 lines (112 loc) · 3.86 KB
/
ajaxracer.js
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
#!/usr/bin/env node
/**
* Copyright 2018 Aarhus University
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
// Internal
var builder = require('./src/utils/builder.js');
var httpServer = require('./src/utils/http-server.js');
var phase1 = require('./src/phases/phase-1.js');
var phase2 = require('./src/phases/phase-2.js');
var reportGenerator = require('./src/utils/report_generator.js');
// External
var colors = require('colors');
var defaults = require('object.defaults');
var fs = require('fs');
var path = require('path');
var promiseFinally = require('promise.prototype.finally');
var util = require('util');
promiseFinally.shim();
var root = __dirname;
function main(runId, url, options) {
defaults(options, {
build: true,
dryRun: false,
headless: true,
rerun: true,
skipPhase1: false,
skipPhase2: false,
skipReportGeneration: false,
maxTimerDuration: 2000,
maxTimerChainLength: -1,
waitForPromises: true
});
if (fs.existsSync(url)) {
url = util.format('http://localhost:8080/%s', url);
}
// 1) Build the analysis runtime.
var onBuildCompleted = null;
if (options.build) {
onBuildCompleted = builder.build();
} else {
onBuildCompleted = Promise.resolve();
}
// 2) Start http-server.
var onHttpServerStarted = onBuildCompleted.then(
() => httpServer.serve(path.resolve('.'), 8080));
// 3) Run Phase 1.
var onPhase1Completed = onHttpServerStarted;
if (!options.skipPhase1) {
onPhase1Completed = onHttpServerStarted.then(() => {
console.log('Running Phase 1'.blue.bold);
return phase1(runId, url, options)
.catch((e) => console.log(e.toString().red.bold));
});
}
// 4) Run Phase 2.
var onPhase2Completed = onPhase1Completed;
if (!options.skipPhase2) {
onPhase2Completed = onPhase1Completed.then(() => {
console.log('Running Phase 2'.blue.bold);
return phase2(runId, url, options)
.catch((e) => console.log(e.toString().red.bold));;
});
}
// 5) Run report generation.
var onReportGenerated = onPhase2Completed;
if (!options.skipReportGeneration) {
onReportGenerated =
onPhase2Completed.finally(() => reportGenerator(runId, url));
}
// 6) DONE!
return onReportGenerated;
}
// If this file is being run from the command line, then run main.
// Otherwise, main is simply exported.
if (require.main === module) {
var argv = require('yargs')
.usage('Usage: ./ajaxracer.js --run-id <ID> --url <url>')
.option('dry-run', { default: false, type: 'boolean' })
.option('headless', { default: true, type: 'boolean' })
.option('max-timer-chain-length', { default: -1, type: 'number' })
.option('max-timer-duration', { default: 2000, type: 'number' })
.option('run-id', { demand: true, type: 'string' })
.option('rerun', { default: true, type: 'boolean' })
.option('skip-phase-1', { default: false, type: 'boolean' })
.option('skip-phase-2', { default: false, type: 'boolean' })
.option('skip-report-generation', { default: false, type: 'boolean' })
.option('url', { demand: true, type: 'string' })
.option('wait-for-promises', { default: true, type: 'boolean' })
.help()
.argv;
main(argv.runId, argv.url, argv)
.then(() => {
console.log('DONE'.blue.bold);
process.exit(0);
}, (err) => {
console.error(err.stack.red.bold);
process.exit(1);
});
}
module.exports = main;