Last active
December 18, 2015 12:19
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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