forked from moodlehq/moodle-performance-comparison
-
Notifications
You must be signed in to change notification settings - Fork 0
/
recorder.bsf
193 lines (165 loc) · 7.74 KB
/
recorder.bsf
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
import java.io.*;
import java.util.regex.*;
import org.apache.commons.lang3.StringUtils;
import org.apache.jmeter.util.JMeterUtils; // http://jakarta.apache.org/jmeter/api/org/apache/jmeter/util/JMeterUtils.html
import org.apache.jmeter.threads.JMeterContext; // http://jakarta.apache.org/jmeter/api/org/apache/jmeter/threads/JMeterContext.html
import org.apache.jmeter.samplers.SampleResult; // http://jakarta.apache.org/jmeter/api/org/apache/jmeter/samplers/SampleResult.html
MoodleResult(JMeterContext ctx) {
Integer thread = ctx.getThreadNum();
SampleResult result = ctx.getPreviousResult();
String html = result.getResponseDataAsString();
String dbreads = "0";
Pattern pdbreads = Pattern.compile(".*?DB reads/writes: (\\d+)/\\d+.*", Pattern.UNIX_LINES | Pattern.DOTALL);
Matcher mdbreads = pdbreads.matcher(html);
if (mdbreads.matches()) {
dbreads = mdbreads.group(1);
}
String dbwritesstr = "0";
Pattern pdbwrites = Pattern.compile(".*?DB reads/writes: \\d+/(\\d+).*", Pattern.UNIX_LINES | Pattern.DOTALL);
Matcher mdbwrites = pdbwrites.matcher(html);
if (mdbwrites.matches()) {
dbwritesstr = mdbwrites.group(1);
}
Integer dbwrites = Integer.parseInt(dbwritesstr);
// Adding logs if required.
if (props.get("includelogs") != null) {
Pattern plogwrites = Pattern.compile(".*?Log DB writes (\\d+).*", Pattern.UNIX_LINES | Pattern.DOTALL);
Matcher mlogwrites = plogwrites.matcher(html);
if (mlogwrites.matches()) {
dbwrites = dbwrites + Integer.parseInt(mlogwrites.group(1));
}
}
String dbquerytime = "0";
Pattern pdbquerytime = Pattern.compile(".*?DB queries time: (\\d+(\\.\\d+)?) secs.*", Pattern.UNIX_LINES | Pattern.DOTALL);
Matcher mdbquerytime = pdbquerytime.matcher(html);
if (mdbquerytime.matches()) {
dbquerytime = mdbquerytime.group(1);
}
String memoryused = "0";
Pattern pmemoryused = Pattern.compile(".*?RAM: (\\d+(\\.\\d+)?)MB.*", Pattern.UNIX_LINES | Pattern.DOTALL);
Matcher mmemoryused = pmemoryused.matcher(html);
if (mmemoryused.matches()) {
memoryused = mmemoryused.group(1);
}
String filesincluded = "0";
Pattern pfilesincluded = Pattern.compile(".*?Included (\\d+) files.*", Pattern.UNIX_LINES | Pattern.DOTALL);
Matcher mfilesincluded = pfilesincluded.matcher(html);
if (mfilesincluded.matches()) {
filesincluded = mfilesincluded.group(1);
}
String serverload = "0";
Pattern pserverload = Pattern.compile(".*?Load average: (\\d+(\\.\\d+)?).*", Pattern.UNIX_LINES | Pattern.DOTALL);
Matcher mserverload = pserverload.matcher(html);
if (mserverload.matches()) {
serverload = mserverload.group(1);
}
String sessionsize = "0";
Pattern psessionsize = Pattern.compile(".*?Session[^:]*: (\\d+(\\.\\d+)? ?[a-zA-Z]*).*", Pattern.UNIX_LINES | Pattern.DOTALL);
Matcher msessionsize = psessionsize.matcher(html);
if (msessionsize.matches()) {
sessionsize = msessionsize.group(1);
}
String timeused = "0";
Pattern ptimeused = Pattern.compile(".*?\"timeused\">(\\d+(\\.\\d+)?) secs.*", Pattern.UNIX_LINES | Pattern.DOTALL);
Matcher mtimeused = ptimeused.matcher(html);
if (mtimeused.matches()) {
timeused = mtimeused.group(1);
}
// Actual information collected about the sample by jmeter
String username = vars.get("username");
String name = StringUtils.rightPad(result.getSampleLabel(), 30);
String url = result.getUrlAsString();
Integer bytes = result.getBytes();
Long time = result.getTime();
Long latency = result.getLatency();
Long starttime = result.getStartTime();
String status = result.getResponseCode();
headerToString() {
String str = "status | thread | ";
str += StringUtils.rightPad("user", 10) + " | ";
str += StringUtils.rightPad("name", 30) + " | db-r | db-w | ";
str += StringUtils.rightPad("dbquerytime", 8) + " | ";
str += StringUtils.rightPad("memory", 8) + " | ";
str += StringUtils.rightPad("files", 6) + " | ";
str += StringUtils.rightPad("load", 6) + " |";
return str;
}
toString() {
String str = StringUtils.rightPad(status, 6) + " | ";
str += StringUtils.rightPad(Integer.toString(thread), 6) + " | ";
str += StringUtils.rightPad(username, 10) + " | ";
str += StringUtils.rightPad(name, 30) + " | ";
str += StringUtils.rightPad(dbreads, 4) + " | ";
str += StringUtils.rightPad(Integer.toString(dbwrites), 4) + " | ";
str += StringUtils.rightPad(dbquerytime, 8) + " | ";
str += StringUtils.rightPad(memoryused, 8) + " | ";
str += StringUtils.rightPad(filesincluded, 6) + " | ";
str += StringUtils.rightPad(serverload, 6) + " | ";
str += url;
return str;
}
toPHP() {
int bytesPos = sessionsize.indexOf(" bytes");
int kbsPos = sessionsize.indexOf("KB");
// Convert the size to KB and strip out the measure.
if (bytesPos != -1) {
sessionsize = "0." + sessionsize.substring(0, bytesPos);
} else if (kbsPos != -1) {
sessionsize = sessionsize.substring(0, kbsPos);
}
String php = "$results["+thread+"][] = array(\n";
php += " 'thread'=>"+thread+",\n"; // Int
php += " 'starttime'=>"+starttime+",\n"; // Long
php += " 'dbreads'=>"+Integer.parseInt(dbreads)+",\n"; // String => Int
php += " 'dbwrites'=>"+dbwrites+",\n";
php += " 'dbquerytime'=>"+dbquerytime+",\n";
php += " 'memoryused'=>'"+memoryused+"',\n";
php += " 'filesincluded'=>'"+filesincluded+"',\n";
php += " 'serverload'=>'"+serverload+"',\n";
php += " 'sessionsize'=>'"+sessionsize+"',\n";
php += " 'timeused'=>'"+timeused+"',\n";
php += " 'name'=>'"+name+"',\n";
php += " 'url'=>'"+url+"',\n";
php += " 'bytes'=>'"+bytes+"',\n";
php += " 'time'=>'"+time+"',\n";
php += " 'latency'=>'"+latency+"',\n";
php += ");\n";
return php;
}
return this;
}
EscapeQuotes(String text) {
return text.replace("'", "\\'");
}
Runnable mr = MoodleResult(ctx);
// Get the file (it is created in testStarted).
String filenamepath = "runs/tmpfilename.php";
// We add the run info when starting the first thread
if (JMeterUtils.getProperty("headerprinted") == null) {
// Output headers.
JMeterUtils.setProperty("headerprinted", "1");
print(mr.headerToString());
FileWriter fstream = new FileWriter(filenamepath, true);
BufferedWriter out = new BufferedWriter(fstream);
out.write("$host = '"+vars.get("host")+"';\n");
out.write("$sitepath = '"+vars.get("sitepath")+"';\n");
out.write("$group = '"+EscapeQuotes(props.get("group"))+"';\n");
out.write("$rundesc = '"+EscapeQuotes(props.get("desc"))+"';\n");
out.write("$users = '"+vars.get("users")+"';\n");
out.write("$loopcount = '"+vars.get("loops")+"';\n");
out.write("$rampup = '"+vars.get("rampup")+"';\n");
out.write("$throughput = '"+vars.get("throughput")+"';\n");
out.write("$size = '"+vars.get("size")+"';\n");
out.write("$baseversion = '"+vars.get("moodleversion")+"';\n");
out.write("$siteversion = '"+EscapeQuotes(props.get("siteversion"))+"';\n");
out.write("$sitebranch = '"+EscapeQuotes(props.get("sitebranch"))+"';\n");
out.write("$sitecommit = '"+EscapeQuotes(props.get("sitecommit"))+"';\n");
out.close();
// Send the run timestamp to set it as run filename.
props.put("filepath", "runs/" + vars.get("runtimestamp") + ".php");
}
FileWriter fstream = new FileWriter(filenamepath, true);
BufferedWriter out = new BufferedWriter(fstream);
out.write(mr.toPHP());
out.close();
print(mr.toString());