-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsinglePageLoadTester.js
112 lines (100 loc) · 2.47 KB
/
singlePageLoadTester.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
var LoadTestingSession = require("./loadTestingSession.js");
var fs = require('fs');
function SinglePageLoadTester(redlineApi, testNum, rand, config)
{
// Redline API
this.redlineApi = redlineApi;
// Test info
this.testNum = testNum;
this.rand = rand;
// INI Config
this.config = config;
// Check for min and max delay
this.minDelayMs = this.maxDelayMs = null;
if (config.min_delay_ms && config.max_delay_ms)
{
var num = parseInt(config.min_delay_ms, 10);
if (!isNaN(num))
this.minDelayMs = num;
num = parseInt(config.max_delay_ms, 10);
if (!isNaN(num))
this.maxDelayMs = num;
}
this.delayRangeMs = 0;
if (this.minDelayMs !== null && this.maxDelayMs !== null)
this.delayRangeMs = this.maxDelayMs - this.minDelayMs + 1;
// Check for number of iterations
this.remainingIterations = 1;
if (config.num_iterations)
{
var num = parseInt(config.num_iterations, 10);
if (!isNaN(num))
this.remainingIterations = num;
}
// Parameters
this.parameters = {
get: [],
post: []
};
if (config.parameter_file)
{
try
{
var paramJson = JSON.parse(""+fs.readFileSync(config.parameter_file));
if (paramJson)
{
if (paramJson.get)
this.parameters.get = paramJson.get;
if (paramJson.post)
this.parameters.post = paramJson.post;
}
} catch (e) {}
}
}
/** Run test */
SinglePageLoadTester.prototype.runTest = function(callback)
{
var that = this;
// Set delay
var delay = 1;
if (this.delayRangeMs != 0)
delay = Math.floor((Math.random()*this.delayRangeMs)+this.minDelayMs);
// Make request after timeout
setTimeout(function() {
try
{
that.loadPage(that.config.url, function(failed) {
// Iteration complete
that.remainingIterations--;
// Another iteration?
if (!failed && that.remainingIterations > 0)
that.runTest(callback);
else
{
// Callback
if (callback)
callback.call(that, failed);
}
});
} catch (e) {
// Callback
if (callback)
callback.call(that, failed);
}
}, delay);
};
/** Load Page */
SinglePageLoadTester.prototype.loadPage = function(pageUrl, callback)
{
var that = this;
// Resource loading?
var loadResources = that.config.load_resources == "1";
var loadTestSess = new LoadTestingSession(that.testNum, that.redlineApi, loadResources);
loadTestSess.setParameters(this.parameters);
loadTestSess.loadPage(pageUrl, function(failed) {
// Callback
if (callback)
callback.call(that, failed);
});
};
module.exports = SinglePageLoadTester;