Skip to content

Commit b9f0d90

Browse files
committed
added google csi method/command
1 parent 66a53ba commit b9f0d90

12 files changed

Lines changed: 246 additions & 31 deletions

File tree

lib/helper.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,6 @@ function normalizeObj(root) {
5050
}
5151

5252
function xmlToObj(xml, callback) {
53-
5453
parser.parseString(xml, function (err, obj) {
5554
if (err) {
5655
callback(err);
@@ -112,8 +111,10 @@ function csvParser(data, callback) {
112111
}
113112
return row;
114113
})
115-
.to.array(callback);
114+
.on('error', callback.bind(this))
115+
.to.array(callback.bind(this, undefined));
116116
}
117+
csvParser.async = true;
117118

118119
// Net log has a buggy end of file, attempt to fix
119120
function netLogParser(data) {

lib/mapping.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -641,6 +641,12 @@ var commands = {
641641
optional: true,
642642
info: 'get history of previously run tests'
643643
},
644+
'googlecsi': {
645+
name: 'getGoogleCsiData',
646+
param: 'id',
647+
options: [options.run],
648+
info: 'get Google CSI data (Client Side Instrumentation)'
649+
},
644650
'waterfall': {
645651
name: 'getWaterfallImage',
646652
param: 'id',

lib/webpagetest.js

Lines changed: 61 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@ var paths = {
3232
cancel: 'cancelTest.php',
3333
history: 'testlog.php',
3434
videoCreation: 'video/create.php',
35-
videoView: 'video/view.php'
35+
videoView: 'video/view.php',
36+
googleCsi: 'google/google_csi.php'
3637
};
3738

3839
var filenames = {
@@ -120,6 +121,18 @@ function get(config, pathname, callback, encoding) {
120121
});
121122
}
122123

124+
// execute callback properly normalizing optional args
125+
function callbackYield(callback, err, data, options) {
126+
if (typeof callback === 'function') {
127+
callback.apply(callback, [err, data].concat(options.args));
128+
}
129+
}
130+
131+
// helper for async parser callback
132+
function asyncParserCallback(options, err, data) {
133+
callbackYield(this, err, data, options);
134+
}
135+
123136
// WPT API call wrapper
124137
function api(pathname, callback, query, options) {
125138
var config;
@@ -142,7 +155,7 @@ function api(pathname, callback, query, options) {
142155

143156
// dry run: return the API url (string) only
144157
if (typeof callback === 'function') {
145-
callback.apply(this,
158+
callback.apply(callback,
146159
[undefined, helper.dryRun(config, pathname)]
147160
);
148161
}
@@ -154,19 +167,21 @@ function api(pathname, callback, query, options) {
154167
if (!err) {
155168
try {
156169
if (options.parser) {
157-
data = options.parser(data);
170+
// async parser
171+
if (options.parser.async) {
172+
return options.parser(data,
173+
asyncParserCallback.bind(callback, options));
174+
} else {
175+
data = options.parser(data);
176+
}
158177
} else {
159178
if (!data) {
160179
data = {};
161180
} else if (info.type === 'application/json') {
162181
data = JSON.parse(data);
163182
} else if (info.type === 'text/xml') {
164-
helper.xmlToObj(data, function(err, obj) {
165-
if (typeof callback === 'function') {
166-
callback.apply(this, [err, obj].concat(options.args));
167-
}
168-
});
169-
return;
183+
return helper.xmlToObj(data,
184+
asyncParserCallback.bind(callback, options));
170185
} else if (info.type === 'text/html') {
171186
data = {result: (reHTMLOutput.exec(data) || [])[1]};
172187
}
@@ -176,10 +191,8 @@ function api(pathname, callback, query, options) {
176191
}
177192
}
178193

179-
if (typeof callback === 'function') {
180-
callback.apply(this, [err, data].concat(options.args));
181-
}
182-
}, options.encoding);
194+
callbackYield(callback, err, data, options);
195+
}.bind(this), options.encoding);
183196

184197
}
185198

@@ -188,20 +201,32 @@ function api(pathname, callback, query, options) {
188201
}
189202

190203
// Set the appropriate filename to be requested
191-
function setFilename(input, options) {
204+
function setFilename(input, options, doNotDefault) {
192205
var run, cached;
193206

194207
options = options || {};
195208

196-
run = parseInt(options.run || options.r, 10) || 1;
209+
// set run and cached with or without defaults
210+
run = parseInt(options.run || options.r, 10) ||
211+
(doNotDefault ? undefined : 1);
197212
cached = (options.repeatView || options.cached || options.c) ?
198213
filenames.cached : '';
214+
// when falsy, check set default accordingly
215+
if (doNotDefault && !cached) {
216+
cached = ['repeatView', 'cached', 'c'].some(function(key) {
217+
return key in options;
218+
}) ? '' : undefined;
219+
}
199220

200221
if (typeof input === 'string') {
201222
return run + cached + '_' + input;
202223
} else {
203-
input.run = run;
204-
input.cached = cached ? 1 : 0;
224+
if (run !== undefined) {
225+
input.run = run;
226+
}
227+
if (cached !== undefined) {
228+
input.cached = cached ? 1 : 0;
229+
}
205230
return input;
206231
}
207232
}
@@ -515,25 +540,29 @@ function getTestInfo(id, options, callback) {
515540
}
516541

517542
function getHistory(days, options, callback) {
543+
var query;
544+
518545
callback = callback || options;
519546
options = options === callback ? {} : options;
520-
var query = {
547+
options.parser = options.parser || helper.csvParser;
548+
query = {
521549
all: 'on',
522550
f: 'csv',
523551
days: days ? parseInt(days, 10) : 1
524552
};
525553

526-
return api.call(this, paths.history, function(err, data) {
527-
if (err) {
528-
return callback(err);
529-
} else if (!(data instanceof Buffer)) {
530-
return callback(err, data);
531-
}
554+
return api.call(this, paths.history, callback, query, options);
555+
}
532556

533-
helper.csvParser(data, function(parsed) {
534-
callback(err, parsed);
535-
});
536-
}, query, options);
557+
function getGoogleCsiData(id, options, callback) {
558+
var query;
559+
560+
callback = callback || options;
561+
options = options === callback ? {} : options;
562+
options.parser = options.parser || helper.csvParser;
563+
query = setFilename({test: id}, options, true);
564+
565+
return api.call(this, paths.googleCsi, callback, query, options);
537566
}
538567

539568
function getWaterfallImage(id, options, callback) {
@@ -677,6 +706,7 @@ WebPageTest.prototype = {
677706
getHistory: getHistory,
678707
getWaterfallImage: getWaterfallImage,
679708
getScreenshotImage: getScreenshotImage,
709+
getGoogleCsiData: getGoogleCsiData,
680710
getEmbedVideoPlayer: getEmbedVideoPlayer,
681711
createVideo: createVideo,
682712
scriptToString: WebPageTest.scriptToString,
@@ -698,6 +728,9 @@ WebPageTest.prototype = {
698728
console: getConsoleLogData,
699729
testinfo: getTestInfo,
700730
history: getHistory,
731+
googlecsi: getGoogleCsiData,
732+
player: getEmbedVideoPlayer,
733+
video: createVideo,
701734
waterfall: getWaterfallImage,
702735
screenshot: getScreenshotImage
703736
};

test/dryrun-test.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -388,5 +388,26 @@ describe('Dry Run', function() {
388388
done();
389389
});
390390
});
391+
392+
it('get google csi', function(done) {
393+
wpt.getGoogleCsiData('140101_AB_12', {dryRun: true}, function(err, data) {
394+
if (err) throw err;
395+
assert.equal(data.url, wptServer + 'google/google_csi.php?test=140101_AB_12');
396+
done();
397+
});
398+
});
399+
400+
it('get google csi run 3 cached', function(done) {
401+
wpt.getGoogleCsiData('140101_AB_12', {
402+
dryRun: true,
403+
repeatView: true,
404+
run: 3
405+
}, function(err, data) {
406+
if (err) throw err;
407+
assert.equal(data.url, wptServer + 'google/google_csi.php?test=140101_AB_12&run=3&cached=1');
408+
done();
409+
});
410+
});
411+
391412
});
392413
});
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
2+
Usage: googlecsi [options] <id>
3+
4+
Options:
5+
6+
-h, --help output usage information
7+
-r, --run <number> which run number on a multiple runs test [1]
8+
-c, --cached get the Repeat View (cached view) instead of default First View (primed cache)
9+

test/fixtures/command-line/help.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
console [options] <id> get the browser console log data (if available) from test
1919
testinfo <id> get test request info/details
2020
history [days] get history of previously run tests
21+
googlecsi [options] <id> get Google CSI data (Client Side Instrumentation)
2122
waterfall [options] <id> get the waterfall PNG image
2223
screenshot [options] <id> get the fully loaded page screenshot in JPG format (PNG if in full resolution)
2324
video [options] <tests> create a video from <tests> (comma separated test ids)
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
[
2+
{
3+
"id": "140101_AB_12",
4+
"run": "1",
5+
"cached": "false",
6+
"service": "myservice",
7+
"action": "var1,var2",
8+
"variable": "foo",
9+
"value": "123"
10+
},
11+
{
12+
"id": "140101_AB_12",
13+
"run": "1",
14+
"cached": "false",
15+
"service": "myservice",
16+
"action": "var1,var2",
17+
"variable": "bar",
18+
"value": "456"
19+
},
20+
{
21+
"id": "140101_AB_12",
22+
"run": "1",
23+
"cached": "false",
24+
"service": "myservice",
25+
"action": "var1,var2",
26+
"variable": "baz",
27+
"value": "789"
28+
},
29+
{
30+
"id": "140101_AB_12",
31+
"run": "2",
32+
"cached": "false",
33+
"service": "myservice",
34+
"action": "var1,var2",
35+
"variable": "foo",
36+
"value": "234"
37+
},
38+
{
39+
"id": "140101_AB_12",
40+
"run": "2",
41+
"cached": "false",
42+
"service": "myservice",
43+
"action": "var1,var2",
44+
"variable": "bar",
45+
"value": "567"
46+
},
47+
{
48+
"id": "140101_AB_12",
49+
"run": "2",
50+
"cached": "false",
51+
"service": "myservice",
52+
"action": "var1,var2",
53+
"variable": "baz",
54+
"value": "890"
55+
},
56+
{
57+
"id": "140101_AB_12",
58+
"run": "3",
59+
"cached": "false",
60+
"service": "myservice",
61+
"action": "var1,var2",
62+
"variable": "foo",
63+
"value": "345"
64+
},
65+
{
66+
"id": "140101_AB_12",
67+
"run": "3",
68+
"cached": "false",
69+
"service": "myservice",
70+
"action": "var1,var2",
71+
"variable": "bar",
72+
"value": "678"
73+
},
74+
{
75+
"id": "140101_AB_12",
76+
"run": "3",
77+
"cached": "false",
78+
"service": "myservice",
79+
"action": "var1,var2",
80+
"variable": "baz",
81+
"value": "901"
82+
}
83+
]
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
[
2+
{
3+
"id": "140101_AB_12",
4+
"run": "2",
5+
"cached": "false",
6+
"service": "myservice",
7+
"action": "var1,var2",
8+
"variable": "foo",
9+
"value": "234"
10+
},
11+
{
12+
"id": "140101_AB_12",
13+
"run": "2",
14+
"cached": "false",
15+
"service": "myservice",
16+
"action": "var1,var2",
17+
"variable": "bar",
18+
"value": "567"
19+
},
20+
{
21+
"id": "140101_AB_12",
22+
"run": "2",
23+
"cached": "false",
24+
"service": "myservice",
25+
"action": "var1,var2",
26+
"variable": "baz",
27+
"value": "890"
28+
}
29+
]
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
"id","run","cached","service","action","variable","value"
2+
"140101_AB_12","1","false","myservice","var1,var2","foo","123"
3+
"140101_AB_12","1","false","myservice","var1,var2","bar","456"
4+
"140101_AB_12","1","false","myservice","var1,var2","baz","789"
5+
"140101_AB_12","2","false","myservice","var1,var2","foo","234"
6+
"140101_AB_12","2","false","myservice","var1,var2","bar","567"
7+
"140101_AB_12","2","false","myservice","var1,var2","baz","890"
8+
"140101_AB_12","3","false","myservice","var1,var2","foo","345"
9+
"140101_AB_12","3","false","myservice","var1,var2","bar","678"
10+
"140101_AB_12","3","false","myservice","var1,var2","baz","901"
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
"id","run","cached","service","action","variable","value"
2+
"140101_AB_12","2","false","myservice","var1,var2","foo","234"
3+
"140101_AB_12","2","false","myservice","var1,var2","bar","567"
4+
"140101_AB_12","2","false","myservice","var1,var2","baz","890"

0 commit comments

Comments
 (0)