diff src/nabble/view/web/tools/Profile.java @ 0:7ecd1a4ef557

add content
author Franklin Schmidt <fschmidt@gmail.com>
date Thu, 21 Mar 2019 19:15:52 -0600
parents
children
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/nabble/view/web/tools/Profile.java	Thu Mar 21 19:15:52 2019 -0600
@@ -0,0 +1,98 @@
+
+package nabble.view.web.tools;
+
+import com.yourkit.api.Controller;
+import com.yourkit.api.ProfilingModes;
+import fschmidt.util.java.HtmlUtils;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import javax.servlet.ServletException;
+import javax.servlet.http.HttpServlet;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+import java.io.IOException;
+import java.io.PrintWriter;
+
+
+public final class Profile extends HttpServlet {
+
+	private static final Logger logger = LoggerFactory.getLogger(Profile.class);
+
+	private static Controller profiler;
+	static {
+		try {
+			profiler = new com.yourkit.api.Controller();
+		} catch (Exception e) {
+			logger.info("Profiler error: " + e);
+		}
+	}
+
+	protected void service(HttpServletRequest request,HttpServletResponse response)
+		throws ServletException, IOException
+	{
+		PrintWriter out = response.getWriter();
+
+		float oneMega = 1024f * 1024f;
+		float free = Runtime.getRuntime().freeMemory() / oneMega;
+		float total = Runtime.getRuntime().totalMemory() / oneMega;
+		float used = total - free;
+
+		String action = request.getParameter("action");
+		if ("gc".equals(action)) {
+			System.gc();
+			response.sendRedirect("/tools/Profile.jtp");
+			return;
+		} else if ("memory".equals(action)) {
+			String file;
+			try {
+				file = profiler.captureSnapshot(ProfilingModes.SNAPSHOT_WITH_HEAP);
+			} catch(Exception e) {
+				throw new RuntimeException(e);
+			}
+			response.sendRedirect("/tools/Profile.jtp?file="+HtmlUtils.urlEncode(file));
+			return;
+		} else if ("cpu".equals(action)) {
+			long seconds = Long.valueOf( request.getParameter("seconds") );
+			String file;
+			try {
+				profiler.startCPUProfiling(ProfilingModes.CPU_SAMPLING, null);
+				Thread.sleep(seconds*1000L);
+				profiler.stopCPUProfiling();
+				file = profiler.captureSnapshot(ProfilingModes.SNAPSHOT_WITHOUT_HEAP);
+			} catch(Exception e) {
+				throw new RuntimeException(e);
+			}
+			response.sendRedirect("/tools/Profile.jtp?file="+HtmlUtils.urlEncode(file));
+			return;
+		}
+		
+		out.print( "\n<html>\n	<head>\n		<title>Profiler</title>\n		<style type=\"text/css\">\n			body {\n				font-family:Verdana, Arial, Serif;\n				font-size:.74em;\n			}\n			.gray {\n				background-color: #eeeeee;\n				padding: .5em;\n				margin: .5em;\n				font-size:100%;\n			}\n		</style>\n	</head>\n	<body>\n		<div>\n			<a href=\"/tools/\">Tools</a>\n		</div>\n		" );
+
+				String file = request.getParameter("file");
+				if( file != null ) {
+					
+		out.print( "\n<div style=\"margin:1em .5em .5em;padding:.3em\">\n	<b>Snapshot File</b>: " );
+		out.print( (file) );
+		out.print( "\n</div>\n" );
+
+				}
+				
+		out.print( "\n<table class=\"gray\">\n	<tr>\n		<td class=\"row-label\">Free Memory</td>\n		<td>\n			" );
+		out.print( (String.format("%.2f",free)) );
+		out.print( " Mb\n		</td>\n	</tr>\n	<tr>\n		<td class=\"row-label\">Used Memory</td>\n		<td>" );
+		out.print( (String.format("%.2f",used)) );
+		out.print( " Mb</td>\n	</tr>\n	<tr>\n		<td class=\"row-label\">Total Memory</td>\n		<td>" );
+		out.print( (String.format("%.2f",total)) );
+		out.print( " Mb</td>\n	</tr>\n</table>\n<input type=\"button\" onclick=\"location='/tools/Profile.jtp?action=gc'\" value=\"Run GC\"/>\n" );
+ if (profiler == null) { 
+		out.print( "\n	<div style=\"margin:1em 0\">\n		Yourkit profiler is disabled on this server.\n	</div>\n" );
+ } else { 
+		out.print( "\n	<div style=\"margin:1em 0\">\n		<input type=\"button\" onclick=\"location='/tools/Profile.jtp?action=memory'\" value=\"Capture Memory Snapshot\"/>\n	</div>\n	<div style=\"margin:1em 0\">\n		<form>\n			<input type=\"hidden\" name=\"action\" value=\"cpu\">\n			<input type=\"text\" name=\"seconds\" value=\"5\" size=\"2\"> seconds\n			<input type=\"submit\" value=\"CPU Profiling\">\n		</form>\n	</div>\n" );
+ } 
+		out.print( "\n</body>\n</html>\n" );
+
+	}
+
+}
+