DiskBenchmarkPlugin.java
9.67 KB
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* javax.servlet.Servlet
* javax.servlet.ServletException
* javax.servlet.http.HttpServletRequest
* javax.servlet.http.HttpServletResponse
* org.apache.felix.scr.annotations.Component
* org.apache.felix.scr.annotations.Property
* org.apache.felix.scr.annotations.Service
* org.apache.felix.webconsole.AbstractWebConsolePlugin
*/
package com.adobe.granite.webconsole.plugins.impl;
import java.io.File;
import java.io.FileDescriptor;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.RandomAccessFile;
import java.net.URL;
import java.util.ArrayList;
import java.util.Random;
import javax.servlet.Servlet;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Property;
import org.apache.felix.scr.annotations.Service;
import org.apache.felix.webconsole.AbstractWebConsolePlugin;
@Component
@Service(value={Servlet.class})
public class DiskBenchmarkPlugin
extends AbstractWebConsolePlugin {
@Property(name="felix.webconsole.label")
private static final String LABEL = "diskbenchmark";
@Property(name="felix.webconsole.title")
private static final String TITLE = "Disk Benchmark";
private static final String RESOURCE_PREFIX = "/diskbenchmark/";
private static volatile boolean stop;
private ArrayList<String> results = new ArrayList();
public String getLabel() {
return "diskbenchmark";
}
public String getTitle() {
return "Disk Benchmark";
}
protected void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
this.doGet(req, res);
}
protected void renderContent(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
PrintWriter pw = res.getWriter();
pw.println("<p class=\"intro\">This is a simple disk performance benchmark. It test how fast Java can read from and write to the hard disk. The test uses temporary files, which are deleted at the end of the run.</p>");
String action = req.getParameter("action");
if ("start".equals(action) && "POST".equals(req.getMethod())) {
this.start(req, res);
} else if (action == null || "stop".equals(action)) {
this.stop(req, res);
}
}
/*
* WARNING - Removed try catching itself - possible behaviour change.
*/
private void start(HttpServletRequest req, HttpServletResponse res) throws IOException {
String directory = req.getParameter("directory");
directory = new File(directory).getAbsolutePath();
int fileSize = Integer.parseInt(req.getParameter("fileSize"));
int time = Integer.parseInt(req.getParameter("time"));
int delay = Integer.parseInt(req.getParameter("delay"));
String mode = req.getParameter("mode");
new File(directory).mkdirs();
File temp = File.createTempFile("disktest-", null, new File(directory));
temp.deleteOnExit();
boolean sync = false;
if (mode.endsWith("+")) {
sync = true;
mode = mode.substring(0, mode.length() - 1);
}
PrintWriter pw = res.getWriter();
pw.println("<form method='post'>");
pw.println("<input type='hidden' name='action' value='stop' />");
pw.println("<input type='submit' value='Stop' />");
pw.println("</form>");
pw.println("<div class='info'>Directory: " + directory);
pw.println("<br />File: " + temp.getAbsolutePath());
pw.println("<br />File size (MB): " + fileSize);
pw.println("<br />Run (ms): " + time);
pw.println("<br />Delay (ms): " + delay);
pw.println("<br />File mode: " + mode);
pw.println("<br />Sync: " + sync);
pw.println("<br />Collecting...</div>");
pw.println("Data format: operation type, operations per second for block sizes 256 bytes, 512 bytes, 1 KB, 2 KB, 4 KB, 8 KB, 16 KB.<br />");
pw.flush();
stop = false;
this.results.clear();
RandomAccessFile f = null;
try {
f = new RandomAccessFile(temp, "rw");
f.setLength(fileSize * 1024 * 1024);
f.close();
f = new RandomAccessFile(temp, mode);
Random random = new Random(1);
int minBlockSize = 256;
int maxBlockSize = 16384;
int blockSize = minBlockSize;
byte[] buffer = new byte[maxBlockSize];
boolean readOnly = false;
String s = null;
while (!stop) {
if (s == null) {
String string = s = "r".equals(mode) || readOnly ? "read" : "write";
}
if (delay > 0) {
try {
Thread.sleep(delay);
}
catch (Exception e) {
// empty catch block
}
}
long start = System.currentTimeMillis();
long end = start + (long)time;
long now = start;
int op = 0;
do {
if ("r".equals(mode) || readOnly) {
f.seek(blockSize * random.nextInt(fileSize * 1024 * 1024 / blockSize));
f.readFully(buffer, 0, blockSize);
} else {
f.seek(blockSize * random.nextInt(fileSize * 1024 * 1024 / blockSize));
random.nextBytes(buffer);
f.write(buffer, 0, blockSize);
if (sync) {
f.getFD().sync();
}
}
now = System.currentTimeMillis();
if (now > end) break;
++op;
} while (true);
s = s + " " + op;
if ((blockSize += blockSize) <= maxBlockSize) continue;
readOnly = !readOnly;
blockSize = minBlockSize;
pw.flush();
pw.print(s);
pw.println("<br />");
this.results.add(s);
s = null;
}
}
finally {
if (f != null) {
f.close();
}
temp.delete();
}
}
private void stop(HttpServletRequest req, HttpServletResponse res) throws IOException {
stop = true;
PrintWriter pw = res.getWriter();
pw.println("<form method='post'>");
pw.println("<div><label>Directory: <input type='text' name='directory' value='.' /></label>");
pw.println("<p class='desc'>The test will pause this long until running the next test.</p></div>");
pw.println("<div><label>File size (MB): <input type='text' name='fileSize' value='32' /></label>");
pw.println("<p class='desc'>The test creates one temporary file with the given size.</p></div>");
pw.println("<div><label>Run (ms): <input type='text' name='time' value='1000' /></label>");
pw.println("<p class='desc'>The test will run at full speed for this many milliseconds.</p></div>");
pw.println("<div><label>Delay (ms): <input type='text' name='delay' value='100' /></label>");
pw.println("<p class='desc'>After running a test, there is a pause of this many milliseconds.</p></div>");
pw.println("<div><label>File mode (r/rw/rw+): <input type='text' name='mode' value='rw' /></label>");
pw.println("<p class='desc'>The file access mode. Use rw for read+write, r for read-only, rw+ for read+write+fsync.</p></div>");
pw.println("<input type='hidden' name='action' value='start' />");
pw.println("<input type='submit' value='Start Benchmark' />");
pw.println("</form>");
if (this.results.size() > 0) {
pw.println("<table>");
pw.println("<thead>");
pw.println("<tr>");
pw.println("<th class='type' rowspan='2'>Operation Type</th>");
pw.println("<th class='values' colspan='7'>Operations Per Second for Block Sizes</th>");
pw.println("</tr>");
pw.println("<tr>");
pw.println("<th class='value'>256 B</th>");
pw.println("<th class='value'>512 B</th>");
pw.println("<th class='value'>1 KB</th>");
pw.println("<th class='value'>2 KB</th>");
pw.println("<th class='value'>4 KB</th>");
pw.println("<th class='value'>8 KB</th>");
pw.println("<th class='value'>16 KB</th>");
pw.println("</tr>");
pw.println("</thead>");
pw.println("<tbody>");
for (String s : this.results) {
String[] rows = s.split(" ");
pw.println("<tr>");
int i = 0;
for (String cell : rows) {
if (i == 0) {
pw.print("<td class='type'>");
} else {
pw.print("<td class='value'>");
}
pw.print(cell);
pw.println("</td>");
++i;
}
pw.println("</tr>");
}
pw.println("</tbody>");
pw.println("</table>");
}
}
protected String[] getCssReferences() {
return new String[]{"/diskbenchmark/res/diskbenchmark.css"};
}
private URL getResource(String path) {
return path != null && path.startsWith("/diskbenchmark/") ? this.getClass().getResource(path.substring("/diskbenchmark/".length() - 1)) : null;
}
}