Please note that this is the simplest version. It could even be more optimised.
public class J extends HttpServlet {
private static long start;
private static final AtomicLong count = new AtomicLong();
public static void main(final String[] args) throws Exception {
final Server server = new Server(8080);
final ServletContextHandler servletHandler = new ServletContextHandler( //
ServletContextHandler.NO_SECURITY | ServletContextHandler.NO_SESSIONS);
servletHandler.addServlet(J.class, "/*");
final HandlerCollection logHandler = new HandlerCollection();
logHandler.setHandlers(new Handler[] {servletHandler});
server.setHandler(logHandler);
server.start();
server.join();
}
@Override
protected void doPost(final HttpServletRequest request, final HttpServletResponse response) throws ServletException,
IOException {
if (J.start == 0)
J.start = System.currentTimeMillis();
final ServletInputStream is = request.getInputStream();
final byte[] b = new byte[1024];
int read;
while (( read = is.read(b) ) > 0)
J.count.addAndGet(read);
is.close();
final OutputStream os = response.getOutputStream();
os.close();
final float speed = (float) J.count.get() * 1000 / ( System.currentTimeMillis() - J.start ) / 1024 / 1024;
System.out.println(String.format("%.1f MB/sec %.1f Mb/sec", speed, speed * 8));
}
}