Skip to content

Commit 79dbe57

Browse files
committed
added commands and options aliases; added request(id) option
1 parent 1b64f42 commit 79dbe57

12 files changed

Lines changed: 159 additions & 54 deletions

File tree

bin/webpagetest

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ Object.keys(mapping.commands).forEach(function eachCommand(name) {
123123
args.push(param);
124124
}
125125
if (cmd.options) {
126-
options = mapping.setOptions(name, opts);
126+
options = mapping.setOptions(name, opts || param);
127127
}
128128
}
129129

@@ -141,7 +141,7 @@ Object.keys(mapping.commands).forEach(function eachCommand(name) {
141141
},
142142
'results': function(id, opts) {
143143
defaultAction(id, opts);
144-
if (opts.specs) {
144+
if (opts.specs && !program.dryrun) {
145145
formatData = null;
146146
}
147147
},

lib/helper.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,31 @@ function WPTAPIError(code, message) {
197197
WPTAPIError.prototype = new Error();
198198
WPTAPIError.prototype.constructor = WPTAPIError;
199199

200+
// Set valid parameter in query
201+
function setQuery(map, options, query) {
202+
query = query || {};
203+
options = options || {};
204+
205+
map.options.forEach(function eachOpts(opt) {
206+
var nokey = map.nokey && map.nokey.indexOf(opt) > -1;
207+
208+
Object.keys(opt).forEach(function eachOpt(key) {
209+
var param = opt[key],
210+
name = param.name,
211+
value = options[name] || options[key];
212+
213+
if (value !== undefined && param.api && !nokey) {
214+
if (param.array) {
215+
value = [].concat(value).join(' ');
216+
}
217+
query[param.api] = param.bool ? (value ? 1 : 0) : value;
218+
}
219+
});
220+
});
221+
222+
return query;
223+
}
224+
200225
module.exports = {
201226
xmlToObj: xmlToObj,
202227
csvToObj: svToObj.bind(null, ',', false),
@@ -207,5 +232,6 @@ module.exports = {
207232
dryRun: dryRun,
208233
normalizeServer: normalizeServer,
209234
localhost: localhost,
235+
setQuery: setQuery,
210236
WPTAPIError: WPTAPIError
211237
};

lib/mapping.js

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -134,13 +134,6 @@ var options = {
134134
param: 'type',
135135
info: 'type of authentication: 0 = Basic, 1 = SNS [0]'
136136
},
137-
'request': {
138-
name: 'requestId',
139-
key: 'e',
140-
api: 'r',
141-
param: 'id',
142-
info: 'echo request ID, useful to track asynchronous requests'
143-
},
144137
'notify': {
145138
name: 'notifyEmail',
146139
key: 'n',
@@ -349,6 +342,15 @@ var options = {
349342
info: 'timeout for polling and waiting results [no timeout]'
350343
}
351344
},
345+
request: {
346+
'request': {
347+
name: 'requestId',
348+
key: 'e',
349+
api: 'r',
350+
param: 'id',
351+
info: 'echo request ID, useful to track asynchronous requests'
352+
}
353+
},
352354
run: {
353355
'run': {
354356
name: 'run',
@@ -401,6 +403,7 @@ var options = {
401403
'median': {
402404
name: 'medianMetric',
403405
key: 'm',
406+
api: 'medianMetric',
404407
param: 'metric',
405408
info: 'set the metric used to calculate median for multiple runs tests [loadTime]'
406409
},
@@ -472,7 +475,7 @@ var options = {
472475
'noellipsis': {
473476
name: 'noEllipsis',
474477
api: 'dots',
475-
key: 'e',
478+
key: 'i',
476479
bool: true,
477480
invert: true,
478481
info: 'hide ellipsis (...) for missing items [false]'
@@ -492,26 +495,29 @@ var commands = {
492495
'status': {
493496
name: 'getTestStatus',
494497
param: 'id',
498+
options: [options.request],
495499
info: 'check test status'
496500
},
497501
'results': {
498502
name: 'getTestResults',
499503
param: 'id',
500-
options: [options.results],
504+
options: [options.results, options.request],
501505
info: 'get test results'
502506
},
503507
'locations': {
504508
name: 'getLocations',
509+
options: [options.request],
505510
info: 'list locations and the number of pending tests'
506511
},
507512
'testers': {
508513
name: 'getTesters',
514+
options: [options.request],
509515
info: 'list testers status and details'
510516
},
511517
'test': {
512518
name: 'runTest',
513519
param: 'url_or_script',
514-
options: [options.test, options.results],
520+
options: [options.test, options.request, options.results],
515521
info: 'run test',
516522
nokey: [options.results]
517523
},

lib/webpagetest.js

Lines changed: 41 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -197,24 +197,21 @@ function setFilename(input, options) {
197197
// Methods
198198

199199
function getTestStatus(id, options, callback) {
200+
var query = {test: id};
201+
200202
callback = callback || options;
201203
options = options === callback ? undefined : options;
204+
helper.setQuery(mapping.commands.status, options, query);
202205

203-
return api.call(this, paths.testStatus, callback, {test: id}, options);
206+
return api.call(this, paths.testStatus, callback, query, options);
204207
}
205208

206209
function getTestResults(id, options, callback) {
207210
var query = {test: id};
208211

209212
callback = callback || typeof options === 'function' && options;
210213
options = options === callback ? {} : options || {};
211-
212-
if (options.requestId) {
213-
query.r = options.requestId;
214-
}
215-
if (options.medianMetric) {
216-
query.medianMetric = options.medianMetric;
217-
}
214+
helper.setQuery(mapping.commands.results, options, query);
218215

219216
// specs
220217
if (options.specs && !options.dryRun) {
@@ -231,14 +228,18 @@ function getLocations(options, callback) {
231228
callback = callback || options;
232229
options = options === callback ? undefined : options;
233230

234-
return api.call(this, paths.locations, callback, undefined, options);
231+
var query = helper.setQuery(mapping.commands.locations, options);
232+
233+
return api.call(this, paths.locations, callback, query, options);
235234
}
236235

237236
function getTesters(options, callback) {
238237
callback = callback || options;
239238
options = options === callback ? undefined : options;
240239

241-
return api.call(this, paths.testers, callback, undefined, options);
240+
var query = helper.setQuery(mapping.commands.testers, options);
241+
242+
return api.call(this, paths.testers, callback, query, options);
242243
}
243244

244245
function runTest(what, options, callback) {
@@ -247,22 +248,9 @@ function runTest(what, options, callback) {
247248
callback = callback || options;
248249
options = options === callback ? {} : options;
249250

250-
// Testing url or script?
251+
// testing url or script?
251252
query[reSpace.test(what) ? 'script' : 'url'] = what;
252-
253-
// only valid parameters in query
254-
Object.keys(mapping.options.test).forEach(function testParamsEach(key) {
255-
var param = mapping.options.test[key],
256-
name = param.name,
257-
value = options[name];
258-
259-
if (value !== undefined && param.api) {
260-
if (param.array) {
261-
value = [].concat(value).join(' ');
262-
}
263-
query[param.api] = param.bool ? (value ? 1 : 0) : value;
264-
}
265-
});
253+
helper.setQuery(mapping.commands.test, options, query);
266254

267255
// connectivity
268256
if (reConnectivity.test(options.connectivity) && query.location) {
@@ -353,20 +341,24 @@ function runTest(what, options, callback) {
353341

354342
// poll|wait results options
355343
Object.keys(mapping.options.results).forEach(function resultsOpts(key) {
356-
var name = mapping.options.results[key].name;
357-
resultsOptions[name] = options[name];
344+
var name = mapping.options.results[key].name,
345+
value = options[name] || options[key];
346+
347+
if (value !== undefined) {
348+
resultsOptions[name] = value;
349+
}
358350
});
359351

360352
// poll results
361-
if (options.pollResults) {
353+
if (options.pollResults && !options.dryRun) {
362354
options.pollResults = (parseInt(options.pollResults, 10) || 5) * 1000;
363355

364356
return api.call(this, paths.test, testCallback.bind(this, poll),
365357
query, options);
366358
}
367359

368360
// wait results
369-
if (options.waitResults) {
361+
if (options.waitResults && !options.dryRun) {
370362

371363
options.waitResults = helper.localhost(options.waitResults,
372364
WebPageTest.defaultWaitResultsPort);
@@ -526,7 +518,7 @@ function getWaterfallImage(id, options, callback) {
526518
Object.keys(mapping.options.waterfall).forEach(function wfParamsEach(key) {
527519
var param = mapping.options.waterfall[key],
528520
name = param.name,
529-
value = options[name];
521+
value = options[name] || options[key];
530522

531523
if (value !== undefined) {
532524
params[param.api || name] =
@@ -622,7 +614,25 @@ WebPageTest.prototype = {
622614
getWaterfallImage: getWaterfallImage,
623615
getScreenshotImage: getScreenshotImage,
624616
scriptToString: WebPageTest.scriptToString,
625-
listen: listen
617+
listen: listen,
618+
619+
// short aliases
620+
status: getTestStatus,
621+
results: getTestResults,
622+
locations: getLocations,
623+
testers: getTesters,
624+
test: runTest,
625+
cancel: cancelTest,
626+
pagespeed: getPageSpeedData,
627+
har: getHARData,
628+
utilization: getUtilizationData,
629+
request: getRequestData,
630+
timeline: getTimelineData,
631+
netlog: getNetLogData,
632+
console: getConsoleLogData,
633+
testinfo: getTestInfo,
634+
waterfall: getWaterfallImage,
635+
screenshot: getScreenshotImage
626636
};
627637

628638
module.exports = WebPageTest;

test/dryrun-test.js

Lines changed: 60 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,17 @@ describe('Dry Run', function() {
2020
});
2121
});
2222

23+
it('gets a test status request with requestId', function(done) {
24+
wpt.getTestStatus('120816_V2_2', {
25+
request: '12345',
26+
dryRun: true
27+
}, function (err, data) {
28+
if (err) return done(err);
29+
assert.equal(data.url, wptServer + 'testStatus.php?test=120816_V2_2&r=12345');
30+
done();
31+
});
32+
});
33+
2334
it('gets a test results request', function(done) {
2435
wpt.getTestResults('120816_V2_2', {dryRun: true}, function (err, data) {
2536
if (err) return done(err);
@@ -28,6 +39,17 @@ describe('Dry Run', function() {
2839
});
2940
});
3041

42+
it('gets a test results request with requestId', function(done) {
43+
wpt.getTestResults('120816_V2_2', {
44+
request: '12345',
45+
dryRun: true
46+
}, function (err, data) {
47+
if (err) return done(err);
48+
assert.equal(data.url, wptServer + 'xmlResult.php?test=120816_V2_2&r=12345');
49+
done();
50+
});
51+
});
52+
3153
it('gets a test results for multi runs with custom median metric request', function(done) {
3254
wpt.getTestResults('120816_V2_2', {
3355
dryRun: true,
@@ -48,6 +70,14 @@ describe('Dry Run', function() {
4870
});
4971
});
5072

73+
it('gets the locations list request with requestId', function(done) {
74+
wpt.getLocations({dryRun: true, request: '12345'}, function (err, data) {
75+
if (err) return done(err);
76+
assert.equal(data.url, wptServer + 'getLocations.php?r=12345');
77+
done();
78+
});
79+
});
80+
5181
it('gets the testers list request', function(done) {
5282
wpt.getTesters({dryRun: true}, function (err, data) {
5383
if (err) return done(err);
@@ -56,6 +86,14 @@ describe('Dry Run', function() {
5686
});
5787
});
5888

89+
it('gets the testers list request with requestId', function(done) {
90+
wpt.getTesters({dryRun: true, request: '12345'}, function (err, data) {
91+
if (err) return done(err);
92+
assert.equal(data.url, wptServer + 'getTesters.php?r=12345');
93+
done();
94+
});
95+
});
96+
5997
it('gets a simple test request', function(done) {
6098
wpt.runTest('http://foobar.com', {dryRun: true}, function (err, data) {
6199
if (err) return done(err);
@@ -73,10 +111,11 @@ describe('Dry Run', function() {
73111
timeline: true,
74112
netLog: true,
75113
fullResolutionScreenshot: true,
114+
request: '12345',
76115
dryRun: true
77116
}, function (err, data) {
78117
if (err) return done(err);
79-
assert.equal(data.url, wptServer + 'runtest.php?url=http%3A%2F%2Ftwitter.com%2Fmarcelduran&location=Local_Firefox_Chrome%3AChrome&runs=3&fvonly=1&label=test%20123&pngss=1&timeline=1&netlog=1&f=json');
118+
assert.equal(data.url, wptServer + 'runtest.php?url=http%3A%2F%2Ftwitter.com%2Fmarcelduran&location=Local_Firefox_Chrome%3AChrome&runs=3&fvonly=1&label=test%20123&pngss=1&timeline=1&netlog=1&r=12345&f=json');
80119
done();
81120
});
82121
});
@@ -240,5 +279,25 @@ describe('Dry Run', function() {
240279
});
241280
});
242281

282+
// alias
283+
284+
it('gets a custom test request using aliases', function(done) {
285+
wpt.test('http://twitter.com/marcelduran', {
286+
l: 'Local_Firefox_Chrome:Chrome',
287+
L: 'test 123',
288+
r: 3,
289+
first: true,
290+
M: true,
291+
netlog: true,
292+
full: true,
293+
e: '12345',
294+
dryRun: true
295+
}, function (err, data) {
296+
if (err) return done(err);
297+
assert.equal(data.url, wptServer + 'runtest.php?url=http%3A%2F%2Ftwitter.com%2Fmarcelduran&fvonly=1&pngss=1&netlog=1&location=Local_Firefox_Chrome%3AChrome&runs=3&label=test%20123&timeline=1&r=12345&f=json');
298+
done();
299+
});
300+
});
301+
243302
});
244303
});

test/fixtures/command-line/help-locations.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,6 @@
33

44
Options:
55

6-
-h, --help output usage information
6+
-h, --help output usage information
7+
-e, --request <id> echo request ID, useful to track asynchronous requests
78

test/fixtures/command-line/help-results.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
Options:
55

66
-h, --help output usage information
7-
-m, --median <metric> set the metric used to calculate median for multiple runs tests [loadtTime]
7+
-m, --median <metric> set the metric used to calculate median for multiple runs tests [loadTime]
88
-S, --specs <json_or_file> set the specs for performance test suite
99
-r, --reporter <name> set performance test suite reporter output: [dot]|spec|tap|xunit|list|progress|min|nyan|landing|json|doc|markdown|teamcity
10+
-e, --request <id> echo request ID, useful to track asynchronous requests
1011

0 commit comments

Comments
 (0)