view src/nabble/view/web/tools/Profile.jtp @ 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 source

<%
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;
		}
		%>
		<html>
			<head>
				<title>Profiler</title>
				<style type="text/css">
					body {
						font-family:Verdana, Arial, Serif;
						font-size:.74em;
					}
					.gray {
						background-color: #eeeeee;
						padding: .5em;
						margin: .5em;
						font-size:100%;
					}
				</style>
			</head>
			<body>
				<div>
					<a href="/tools/">Tools</a>
				</div>
				<%
				String file = request.getParameter("file");
				if( file != null ) {
					%>
					<div style="margin:1em .5em .5em;padding:.3em">
						<b>Snapshot File</b>: <%=file%>
					</div>
					<%
				}
				%>
				<table class="gray">
					<tr>
						<td class="row-label">Free Memory</td>
						<td>
							<%=String.format("%.2f",free)%> Mb
						</td>
					</tr>
					<tr>
						<td class="row-label">Used Memory</td>
						<td><%=String.format("%.2f",used)%> Mb</td>
					</tr>
					<tr>
						<td class="row-label">Total Memory</td>
						<td><%=String.format("%.2f",total)%> Mb</td>
					</tr>
				</table>
				<input type="button" onclick="location='/tools/Profile.jtp?action=gc'" value="Run GC"/>
				<% if (profiler == null) { %>
					<div style="margin:1em 0">
						Yourkit profiler is disabled on this server.
					</div>
				<% } else { %>
					<div style="margin:1em 0">
						<input type="button" onclick="location='/tools/Profile.jtp?action=memory'" value="Capture Memory Snapshot"/>
					</div>
					<div style="margin:1em 0">
						<form>
							<input type="hidden" name="action" value="cpu">
							<input type="text" name="seconds" value="5" size="2"> seconds
							<input type="submit" value="CPU Profiling">
						</form>
					</div>
				<% } %>
			</body>
		</html>
		<%
	}

}
%>