use of org.apache.sling.engine.impl.request.RequestData in project sling by apache.
the class RequestProcessorMBeanImplTest method test_statistics.
/**
* Asserts that the simple standard deviation algorithm used by the
* RequestProcessorMBeanImpl is equivalent to the Commons Math
* SummaryStatistics implementation.
*
* It also tests that resetStatistics method, actually resets all the statistics
*
* @throws NotCompliantMBeanException not expected
*/
@Test
public void test_statistics() throws NotCompliantMBeanException {
final SummaryStatistics durationStats = new SummaryStatistics();
final SummaryStatistics servletCallCountStats = new SummaryStatistics();
final SummaryStatistics peakRecursionDepthStats = new SummaryStatistics();
final RequestProcessorMBeanImpl bean = new RequestProcessorMBeanImpl();
assertEquals(0l, bean.getRequestsCount());
assertEquals(Long.MAX_VALUE, bean.getMinRequestDurationMsec());
assertEquals(0l, bean.getMaxRequestDurationMsec());
assertEquals(0.0, bean.getMeanRequestDurationMsec(), 0);
assertEquals(0.0, bean.getStandardDeviationDurationMsec(), 0);
assertEquals(Integer.MAX_VALUE, bean.getMinServletCallCount());
assertEquals(0l, bean.getMaxServletCallCount());
assertEquals(0.0, bean.getMeanServletCallCount(), 0);
assertEquals(0.0, bean.getStandardDeviationServletCallCount(), 0);
assertEquals(Integer.MAX_VALUE, bean.getMinPeakRecursionDepth());
assertEquals(0l, bean.getMaxPeakRecursionDepth());
assertEquals(0.0, bean.getMeanPeakRecursionDepth(), 0);
assertEquals(0.0, bean.getStandardDeviationPeakRecursionDepth(), 0);
final Random random = new Random(System.currentTimeMillis() / 17);
final int num = 10000;
final int min = 85;
final int max = 250;
for (int i = 0; i < num; i++) {
final long durationValue = min + random.nextInt(max - min);
final int callCountValue = min + random.nextInt(max - min);
final int peakRecursionDepthValue = min + random.nextInt(max - min);
durationStats.addValue(durationValue);
servletCallCountStats.addValue(callCountValue);
peakRecursionDepthStats.addValue(peakRecursionDepthValue);
final RequestData requestData = context.mock(RequestData.class, "requestData" + i);
context.checking(new Expectations() {
{
one(requestData).getElapsedTimeMsec();
will(returnValue(durationValue));
one(requestData).getServletCallCount();
will(returnValue(callCountValue));
one(requestData).getPeakRecusionDepth();
will(returnValue(peakRecursionDepthValue));
}
});
bean.addRequestData(requestData);
}
assertEquals("Number of points must be the same", durationStats.getN(), bean.getRequestsCount());
assertEquals("Min Duration must be equal", (long) durationStats.getMin(), bean.getMinRequestDurationMsec());
assertEquals("Max Duration must be equal", (long) durationStats.getMax(), bean.getMaxRequestDurationMsec());
assertAlmostEqual("Mean Duration", durationStats.getMean(), bean.getMeanRequestDurationMsec(), num);
assertAlmostEqual("Standard Deviation Duration", durationStats.getStandardDeviation(), bean.getStandardDeviationDurationMsec(), num);
assertEquals("Min Servlet Call Count must be equal", (long) servletCallCountStats.getMin(), bean.getMinServletCallCount());
assertEquals("Max Servlet Call Count must be equal", (long) servletCallCountStats.getMax(), bean.getMaxServletCallCount());
assertAlmostEqual("Mean Servlet Call Count", servletCallCountStats.getMean(), bean.getMeanServletCallCount(), num);
assertAlmostEqual("Standard Deviation Servlet Call Count", servletCallCountStats.getStandardDeviation(), bean.getStandardDeviationServletCallCount(), num);
assertEquals("Min Peak Recursion Depth must be equal", (long) peakRecursionDepthStats.getMin(), bean.getMinPeakRecursionDepth());
assertEquals("Max Peak Recursion Depth must be equal", (long) peakRecursionDepthStats.getMax(), bean.getMaxPeakRecursionDepth());
assertAlmostEqual("Mean Peak Recursion Depth", peakRecursionDepthStats.getMean(), bean.getMeanPeakRecursionDepth(), num);
assertAlmostEqual("Standard Deviation Peak Recursion Depth", peakRecursionDepthStats.getStandardDeviation(), bean.getStandardDeviationPeakRecursionDepth(), num);
//check method resetStatistics
//In the previous test, some requests have been processed, now we reset the statistics so everything statistic is reinitialized
bean.resetStatistics();
//Simulate a single request
final long durationValue = min + random.nextInt(max - min);
final int callCountValue = min + random.nextInt(max - min);
final int peakRecursionDepthValue = min + random.nextInt(max - min);
final RequestData requestData = context.mock(RequestData.class, "requestDataAfterReset");
context.checking(new Expectations() {
{
one(requestData).getElapsedTimeMsec();
will(returnValue(durationValue));
one(requestData).getServletCallCount();
will(returnValue(callCountValue));
one(requestData).getPeakRecusionDepth();
will(returnValue(peakRecursionDepthValue));
}
});
bean.addRequestData(requestData);
//As only one request has been simulated since resetStatiscts: min, max and mean statistics should be equals to the request data
assertEquals("After resetStatistics Number of requests must be one", 1, bean.getRequestsCount());
assertEquals("After resetStatistics Min Duration must be equal", bean.getMinRequestDurationMsec(), (long) durationValue);
assertEquals("After resetStatistics Max Duration must be equal", bean.getMaxRequestDurationMsec(), (long) durationValue);
assertEquals("After resetStatistics Mean Duration must be equal", bean.getMeanRequestDurationMsec(), (double) durationValue, 0d);
assertEquals("After resetStatistics Min Servlet Call Count must be equal", bean.getMinServletCallCount(), callCountValue);
assertEquals("After resetStatistics Max Servlet Call Count must be equal", bean.getMaxServletCallCount(), callCountValue);
assertEquals("After resetStatistics Mean Servlet Call Count", bean.getMeanServletCallCount(), (double) callCountValue, 0d);
assertEquals("After resetStatistics Min Peak Recursion Depth must be equal", bean.getMinPeakRecursionDepth(), peakRecursionDepthValue);
assertEquals("After resetStatistics Max Peak Recursion Depth must be equal", bean.getMinPeakRecursionDepth(), peakRecursionDepthValue);
assertEquals("After resetStatistics Mean Peak Recursion Depth", bean.getMeanPeakRecursionDepth(), (double) peakRecursionDepthValue, 0d);
}
use of org.apache.sling.engine.impl.request.RequestData in project sling by apache.
the class SlingHttpServletRequestImplTest method getUserPrincipal_test3.
@Test
public void getUserPrincipal_test3() {
final HttpServletRequest servletRequest = context.mock(HttpServletRequest.class);
context.checking(new Expectations() {
{
one(servletRequest).getServletPath();
will(returnValue("/path"));
allowing(servletRequest).getPathInfo();
will(returnValue("/path"));
}
});
final RequestData requestData = context.mock(RequestData.class, "requestData");
final ResourceResolver resourceResolver = context.mock(ResourceResolver.class);
final Principal principal = context.mock(Principal.class);
context.checking(new Expectations() {
{
allowing(requestData).getResourceResolver();
will(returnValue(resourceResolver));
allowing(resourceResolver).adaptTo(Principal.class);
will(returnValue(principal));
}
});
slingHttpServletRequestImpl = new SlingHttpServletRequestImpl(requestData, servletRequest);
Assert.assertEquals(principal, slingHttpServletRequestImpl.getUserPrincipal());
}
use of org.apache.sling.engine.impl.request.RequestData in project sling by apache.
the class SlingHttpServletRequestImplTest method getUserPrincipal_test.
@Test
public void getUserPrincipal_test() {
final HttpServletRequest servletRequest = context.mock(HttpServletRequest.class);
context.checking(new Expectations() {
{
one(servletRequest).getServletPath();
will(returnValue("/path"));
allowing(servletRequest).getPathInfo();
will(returnValue("/path"));
allowing(servletRequest).getRemoteUser();
will(returnValue("remoteUser"));
}
});
final RequestData requestData = context.mock(RequestData.class, "requestData");
final ResourceResolver resourceResolver = context.mock(ResourceResolver.class);
context.checking(new Expectations() {
{
allowing(requestData).getResourceResolver();
will(returnValue(resourceResolver));
allowing(resourceResolver).adaptTo(Principal.class);
will(returnValue(null));
}
});
slingHttpServletRequestImpl = new SlingHttpServletRequestImpl(requestData, servletRequest);
Assert.assertEquals("UserPrincipal: remoteUser", slingHttpServletRequestImpl.getUserPrincipal().toString());
}
use of org.apache.sling.engine.impl.request.RequestData in project sling by apache.
the class SlingRequestProcessorImpl method dispatchRequest.
// ---------- Generic Content Request processor ----------------------------
/**
* Dispatches the request on behalf of the
* {@link org.apache.sling.engine.impl.request.SlingRequestDispatcher}.
*/
public void dispatchRequest(ServletRequest request, ServletResponse response, Resource resource, RequestPathInfo resolvedURL, boolean include) throws IOException, ServletException {
// we need a SlingHttpServletRequest/SlingHttpServletResponse tupel
// to continue
SlingHttpServletRequest cRequest = RequestData.toSlingHttpServletRequest(request);
SlingHttpServletResponse cResponse = RequestData.toSlingHttpServletResponse(response);
// get the request data (and btw check the correct type)
final RequestData requestData = RequestData.getRequestData(cRequest);
final ContentData oldContentData = requestData.getContentData();
final ContentData contentData = requestData.setContent(resource, resolvedURL);
try {
// resolve the servlet
Servlet servlet = servletResolver.resolveServlet(cRequest);
contentData.setServlet(servlet);
FilterChainType type = include ? FilterChainType.INCLUDE : FilterChainType.FORWARD;
processComponent(cRequest, cResponse, type);
} finally {
requestData.resetContent(oldContentData);
}
}
Aggregations