Skip to content

Instantly share code, notes, and snippets.

@jayankandathil
Last active December 18, 2015 12:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jayankandathil/5781977 to your computer and use it in GitHub Desktop.
Save jayankandathil/5781977 to your computer and use it in GitHub Desktop.
Sample code snippet showing measurement of actions within a Java servlet and returning the information back to the browser
import java.io.PrintWriter;
import java.text.DecimalFormat;
import javax.servlet.http.HttpServletResponse;
final long lngStart;
final long lngEnd;
final double dblElapsedTime;
// Get timestamp
lngStart = System.currentTimeMillis();
// ...do things that you want to measure
// Get timestamp
lngEnd = System.currentTimeMillis();
dblElapsedTime = ((double)(lngEnd-lngStart))/1000;
// Round off to a single decimal
DecimalFormat df = new DecimalFormat("#.#");
PrintWriter out = response.getWriter();
// Set some standard HTTP Response Headers
response.setContentType("text/html");
response.setCharacterEncoding("UTF-8");
response.setHeader("Cache-Control", "no-store, no-cache, must-revalidate");
out.println("<p>Took [<font color=blue>" + df.format(dblElapsedTime) + "</font>] seconds </p>");
out.flush();
out.close();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment