-
Notifications
You must be signed in to change notification settings - Fork 0
/
har2k6.js
95 lines (75 loc) · 2.21 KB
/
har2k6.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
if( process.argv.length != 4 ) {
console.log("\nUsage: node har2k6.js <transform script file> <HAR file>")
process.exit(99);
}
var transformFile = process.argv[2],
harFile = process.argv[3];
var trans = require(transformFile),
fs = require('fs'),
JSONStream = require('JSONStream'),
JSONPath = require('jsonpath-plus');
var stream = fs.createReadStream(harFile, {encoding: 'utf8'}),
parser = JSONStream.parse('log.entries'),
output = "";
// Functions for k6 scope
output += `
import { group } from "k6";
import http from "k6/http";
export let options = {
maxRedirects: 4
}
export default function() {
`
// Initialize JSONStream parser
stream.pipe(parser);
parser.on('data', function (obj) {
var requests = JSONPath({path: '$[*]',
json: obj
});
requests.forEach(transformRequest);
});
parser.on('end', function() {
console.log("\n\n| The Javascript:\n")
// Last brace
output += `
}
`
console.log(output);
});
//
// Functions
//
// JSONStream callback
function transformRequest(value, key, map) {
var requestMethod = value.request.method;
var requestUrl = value.request.url;
var requestPostDataText = "";
var requestPostDataMimeType = "";
if( value.request.postData != undefined ) {
requestPostDataText = value.request.postData.text;
requestPostDataMimeType = value.request.postData.mimeType;
}
var responseStatus = value.response.status;
var responseMimeType = value.response.content.mimeType;
var responseContent = value.response.content.text;
output +=
`response = http.${requestMethod}("${requestUrl}",\
"${requestPostDataText}", "${requestPostDataMimeType}")\n\n`;
var capture="capture";
trans[capture](requestPostDataText);
// requestUrl;
// requestPostData;
// responseStatus;
// responseMimeType;
// responseContent;
}
function xinspect(o,i){
if(typeof i=='undefined')i='';
if(i.length>50)return '[MAX ITERATIONS]';
var r=[];
for(var p in o){
var t=typeof o[p];
r.push(i+'"'+p+'" ('+t+') => '+(t=='object' ? 'object:'+xinspect(o[p],i+' ') : o[p]+''));
}
return r.join(i+'\n');
}