/
locust.js
173 lines (151 loc) Β· 5.27 KB
/
locust.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
$(window).ready(function() {
if($("#locust_count").length > 0) {
$("#locust_count").focus().select();
}
});
$("#box_stop a.stop-button").click(function(event) {
event.preventDefault();
$.get($(this).attr("href"));
$("body").attr("class", "stopped");
$(".box_stop").hide();
$("a.new_test").show();
$("a.edit_test").hide();
$(".user_count").hide();
});
$("#box_stop a.reset-button").click(function(event) {
event.preventDefault();
$.get($(this).attr("href"));
});
$("#new_test").click(function(event) {
event.preventDefault();
$("#start").show();
$("#locust_count").focus().select();
});
$(".edit_test").click(function(event) {
event.preventDefault();
$("#edit").show();
$("#new_locust_count").focus().select();
});
$(".close_link").click(function(event) {
event.preventDefault();
$(this).parent().parent().hide();
});
var alternate = false;
$("ul.tabs").tabs("div.panes > div").on("onClick", function(event) {
if (event.target == $(".chart-tab-link")[0]) {
// trigger resizing of charts
rpsChart.resize();
responseTimeChart.resize();
usersChart.resize();
}
});
var stats_tpl = $('#stats-template');
var errors_tpl = $('#errors-template');
var exceptions_tpl = $('#exceptions-template');
var slaves_tpl = $('#slave-template');
$('#swarm_form').submit(function(event) {
event.preventDefault();
$.post($(this).attr("action"), $(this).serialize(),
function(response) {
if (response.success) {
$("body").attr("class", "hatching");
$("#start").fadeOut();
$("#status").fadeIn();
$(".box_running").fadeIn();
$("a.new_test").fadeOut();
$("a.edit_test").fadeIn();
$(".user_count").fadeIn();
}
}
);
});
$('#edit_form').submit(function(event) {
event.preventDefault();
$.post($(this).attr("action"), $(this).serialize(),
function(response) {
if (response.success) {
$("body").attr("class", "hatching");
$("#edit").fadeOut();
}
}
);
});
var sortBy = function(field, reverse, primer){
reverse = (reverse) ? -1 : 1;
return function(a,b){
a = a[field];
b = b[field];
if (typeof(primer) != 'undefined'){
a = primer(a);
b = primer(b);
}
if (a<b) return reverse * -1;
if (a>b) return reverse * 1;
return 0;
}
}
// Sorting by column
var sortAttribute = "name";
var slaveSortAttribute = "id";
var desc = false;
var report;
$(".stats_label").click(function(event) {
event.preventDefault();
sortAttribute = $(this).attr("data-sortkey");
desc = !desc;
$('#stats tbody').empty();
$('#errors tbody').empty();
alternate = false;
totalRow = report.stats.pop();
sortedStats = (report.stats).sort(sortBy(sortAttribute, desc));
sortedStats.push(totalRow);
$('#stats tbody').jqoteapp(stats_tpl, sortedStats);
alternate = false;
$('#errors tbody').jqoteapp(errors_tpl, (report.errors).sort(sortBy(sortAttribute, desc)));
});
// init charts
var rpsChart = new LocustLineChart($(".charts-container"), "Total Requests per Second", ["RPS"], "reqs/s");
var responseTimeChart = new LocustLineChart($(".charts-container"), "Response Times", ["Median Response Time", "95% percentile"], "ms");
var usersChart = new LocustLineChart($(".charts-container"), "Number of Users", ["Users"], "users");
function updateStats() {
$.get('/stats/requests', function (report) {
$("#total_rps").html(Math.round(report.total_rps*100)/100);
//$("#fail_ratio").html(Math.round(report.fail_ratio*10000)/100);
$("#fail_ratio").html(Math.round(report.fail_ratio*100));
$("#status_text").html(report.state);
$("#userCount").html(report.user_count);
if (report.slaves) {
slaves = (report.slaves).sort(sortBy(slaveSortAttribute, desc));
$("#slaves tbody").empty();
$("#slaves tbody").jqoteapp(slaves_tpl, slaves);
$("#slaveCount").html(slaves.length);
}
$('#stats tbody').empty();
$('#errors tbody').empty();
alternate = false;
totalRow = report.stats.pop();
sortedStats = (report.stats).sort(sortBy(sortAttribute, desc));
sortedStats.push(totalRow);
$('#stats tbody').jqoteapp(stats_tpl, sortedStats);
alternate = false;
$('#errors tbody').jqoteapp(errors_tpl, (report.errors).sort(sortBy(sortAttribute, desc)));
if (report.state !== "stopped"){
// get total stats row
var total = report.stats[report.stats.length-1];
// update charts
rpsChart.addValue([total.current_rps]);
responseTimeChart.addValue([report.current_response_time_percentile_50, report.current_response_time_percentile_95]);
usersChart.addValue([report.user_count]);
}
setTimeout(updateStats, 2000);
});
}
updateStats();
function updateExceptions() {
$.get('/exceptions', function (data) {
$('#exceptions tbody').empty();
$('#exceptions tbody').jqoteapp(exceptions_tpl, data.exceptions);
setTimeout(updateExceptions, 5000);
});
}
updateExceptions();