forked from dropwizard/metrics
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add CPU profiling support to the AdminServlet (fixes dropwizard#927)
- Loading branch information
Showing
5 changed files
with
146 additions
and
3 deletions.
There are no files selected for viewing
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
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
81 changes: 81 additions & 0 deletions
81
metrics-servlets/src/main/java/com/codahale/metrics/servlets/CpuProfileServlet.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
package com.codahale.metrics.servlets; | ||
|
||
import java.io.IOException; | ||
import java.io.OutputStream; | ||
import java.util.concurrent.locks.Lock; | ||
import java.util.concurrent.locks.ReentrantLock; | ||
import javax.servlet.ServletException; | ||
import javax.servlet.http.HttpServlet; | ||
import javax.servlet.http.HttpServletRequest; | ||
import javax.servlet.http.HttpServletResponse; | ||
import org.joda.time.Duration; | ||
import com.papertrail.profiler.CpuProfile; | ||
|
||
/** | ||
* An HTTP servlets which outputs a {@link https://github.com/gperftools/gperftools} parseable response. | ||
*/ | ||
public class CpuProfileServlet extends HttpServlet { | ||
private static final long serialVersionUID = -668666696530287501L; | ||
private static final String CONTENT_TYPE = "pprof/raw"; | ||
private static final String CACHE_CONTROL = "Cache-Control"; | ||
private static final String NO_CACHE = "must-revalidate,no-cache,no-store"; | ||
private final Lock lock = new ReentrantLock(); | ||
|
||
@Override | ||
protected void doGet(HttpServletRequest req, | ||
HttpServletResponse resp) throws ServletException, IOException { | ||
|
||
int duration = 10; | ||
if (req.getParameter("duration") != null) { | ||
try { | ||
duration = Integer.parseInt(req.getParameter("duration")); | ||
} catch (NumberFormatException e) { | ||
duration = 10; | ||
} | ||
} | ||
|
||
int frequency = 100; | ||
if (req.getParameter("frequency") != null) { | ||
try { | ||
frequency = Integer.parseInt(req.getParameter("frequency")); | ||
} catch (NumberFormatException e) { | ||
frequency = 100; | ||
} | ||
} | ||
|
||
final Thread.State state; | ||
if ("blocked".equalsIgnoreCase(req.getParameter("state"))) { | ||
state = Thread.State.BLOCKED; | ||
} | ||
else { | ||
state = Thread.State.RUNNABLE; | ||
} | ||
|
||
resp.setStatus(HttpServletResponse.SC_OK); | ||
resp.setHeader(CACHE_CONTROL, NO_CACHE); | ||
resp.setContentType(CONTENT_TYPE); | ||
final OutputStream output = resp.getOutputStream(); | ||
try { | ||
doProfile(output, duration, frequency, state); | ||
} finally { | ||
output.close(); | ||
} | ||
} | ||
|
||
protected void doProfile(OutputStream out, int duration, int frequency, Thread.State state) throws IOException { | ||
if (lock.tryLock()) { | ||
try { | ||
CpuProfile profile = CpuProfile.record(Duration.standardSeconds(duration), | ||
frequency, state); | ||
if (profile == null) { | ||
throw new RuntimeException("could not create CpuProfile"); | ||
} | ||
profile.writeGoogleProfile(out); | ||
return; | ||
} finally { | ||
lock.unlock(); | ||
} | ||
} | ||
throw new RuntimeException("Only one profile request may be active at a time"); | ||
} | ||
} |
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
43 changes: 43 additions & 0 deletions
43
metrics-servlets/src/test/java/com/codahale/metrics/servlets/CpuProfileServletTest.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package com.codahale.metrics.servlets; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import org.eclipse.jetty.http.HttpHeader; | ||
import org.eclipse.jetty.servlet.ServletTester; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
|
||
public class CpuProfileServletTest extends AbstractServletTest { | ||
|
||
@Override | ||
protected void setUp(ServletTester tester) { | ||
tester.addServlet(CpuProfileServlet.class, "/pprof"); | ||
} | ||
|
||
@Before | ||
public void setUp() throws Exception { | ||
request.setMethod("GET"); | ||
request.setURI("/pprof?duration=1"); | ||
request.setVersion("HTTP/1.0"); | ||
|
||
processRequest(); | ||
} | ||
|
||
@Test | ||
public void returns200OK() throws Exception { | ||
assertThat(response.getStatus()) | ||
.isEqualTo(200); | ||
} | ||
|
||
@Test | ||
public void returnsPprofRaw() throws Exception { | ||
assertThat(response.get(HttpHeader.CONTENT_TYPE)) | ||
.isEqualTo("pprof/raw"); | ||
} | ||
|
||
@Test | ||
public void returnsUncacheable() throws Exception { | ||
assertThat(response.get(HttpHeader.CACHE_CONTROL)) | ||
.isEqualTo("must-revalidate,no-cache,no-store"); | ||
|
||
} | ||
} |