Search in sources :

Example 1 with NullWriter

use of com.github.bordertech.wcomponents.util.NullWriter in project wcomponents by BorderTech.

the class WWindowInterceptor_Test method testServiceRequest.

@Test
public void testServiceRequest() {
    final int initialServletStep = 123;
    final int initialWindowStep = 111;
    // Create app
    WApplication app = new WApplication();
    WWindow window = new WWindow();
    window.setContent(new MockLabel("dummy"));
    app.add(window);
    WContent content = new WContent();
    content.setContentAccess(new InternalResource("/wcomponents-test.properties", "test"));
    app.add(content);
    app.setLocked(true);
    // Set up servlet env
    WServlet.WServletEnvironment servletEnvironment = new WServlet.WServletEnvironment("/app", "", "");
    servletEnvironment.setStep(initialServletStep);
    // Set user session
    UIContext uic = createUIContext();
    uic.setUI(app);
    uic.setEnvironment(servletEnvironment);
    setActiveContext(uic);
    window.setStep(initialWindowStep);
    window.display();
    // Target the content first - should pass through and update the environment's step
    WWindowInterceptor interceptor = new WWindowInterceptor(true);
    TestInterceptor testInterceptor = new TestInterceptor();
    interceptor.setBackingComponent(testInterceptor);
    interceptor.attachUI(app);
    MockRequest request = new MockRequest();
    request.setParameter(Environment.TARGET_ID, content.getId());
    interceptor.serviceRequest(request);
    Assert.assertEquals("Servlet step should have changed after serviceRequest", initialServletStep + 1, servletEnvironment.getStep());
    Assert.assertEquals("Window step should not have changed", initialWindowStep, window.getStep());
    interceptor.preparePaint(request);
    Assert.assertEquals("Servlet step should have changed after preparePaint", initialServletStep + 2, servletEnvironment.getStep());
    Assert.assertEquals("Window step should not have changed", initialWindowStep, window.getStep());
    interceptor.paint(new WebXmlRenderContext(new PrintWriter(new NullWriter())));
    Assert.assertEquals("Servlet step should have changed after paint", initialServletStep + 3, servletEnvironment.getStep());
    Assert.assertEquals("Window step should not have changed", initialWindowStep, window.getStep());
    servletEnvironment.setStep(initialServletStep);
    request = new MockRequest();
    request.setParameter(WWindow.WWINDOW_REQUEST_PARAM_KEY, window.getId());
    interceptor.serviceRequest(request);
    Assert.assertEquals("Window step should have changed after serviceRequest", initialWindowStep + 1, window.getStep());
    Assert.assertEquals("Servlet step should not have changed", initialServletStep, servletEnvironment.getStep());
    interceptor.preparePaint(request);
    Assert.assertEquals("Window step should have changed after preparePaintnot have changed", initialWindowStep + 2, window.getStep());
    Assert.assertEquals("Servlet step should not have changed", initialServletStep, servletEnvironment.getStep());
    interceptor.paint(new WebXmlRenderContext(new PrintWriter(new NullWriter())));
    Assert.assertEquals("Window step should have changed after paint", initialWindowStep + 3, window.getStep());
    Assert.assertEquals("Servlet step should not have changed", initialServletStep, servletEnvironment.getStep());
    String actualTargetId = testInterceptor.hiddenParams.get(WWindow.WWINDOW_REQUEST_PARAM_KEY);
    Assert.assertEquals("Hidden params target id should be window id", window.getId(), actualTargetId);
}
Also used : WebXmlRenderContext(com.github.bordertech.wcomponents.servlet.WebXmlRenderContext) InternalResource(com.github.bordertech.wcomponents.InternalResource) UIContext(com.github.bordertech.wcomponents.UIContext) WWindow(com.github.bordertech.wcomponents.WWindow) WServlet(com.github.bordertech.wcomponents.servlet.WServlet) NullWriter(com.github.bordertech.wcomponents.util.NullWriter) WContent(com.github.bordertech.wcomponents.WContent) WApplication(com.github.bordertech.wcomponents.WApplication) MockLabel(com.github.bordertech.wcomponents.MockLabel) MockRequest(com.github.bordertech.wcomponents.util.mock.MockRequest) PrintWriter(java.io.PrintWriter) Test(org.junit.Test)

Example 2 with NullWriter

use of com.github.bordertech.wcomponents.util.NullWriter in project wcomponents by BorderTech.

the class WebXmlRenderingPerformance_Test method testRenderingScalingWithInterceptorChain.

@Test
public void testRenderingScalingWithInterceptorChain() throws Exception {
    final RenderContext renderContext = new WebXmlRenderContext(new PrintWriter(new NullWriter()));
    final AllComponents component1 = new AllComponents();
    makeAllInputsMandatory(component1);
    component1.setLocked(true);
    final UIContext uic1 = createUIContext();
    final UIContext uicChain = createUIContext();
    sendRequest(component1, uic1);
    Runnable runnable = new Runnable() {

        @Override
        public void run() {
            setActiveContext(uic1);
            for (int i = 0; i < NUM_REPETITIONS; i++) {
                // Just paint component
                component1.preparePaint(new MockRequest());
                component1.paint(renderContext);
            }
        }
    };
    // JIT warm-up
    runnable.run();
    long renderTime1 = time(runnable) / NUM_REPETITIONS;
    runnable = new Runnable() {

        @Override
        public void run() {
            setActiveContext(uicChain);
            for (int i = 0; i < NUM_REPETITIONS; i++) {
                // Paint with the interceptor chain
                WebUtilities.renderWithTransformToHTML(component1);
            }
        }
    };
    // JIT warm-up
    runnable.run();
    long renderTimeChain = time(runnable) / NUM_REPETITIONS;
    LOG.info("Render Component time: " + (renderTime1 / 1000000.0) + "ms");
    LOG.info("Render Component/Chain time: " + (renderTimeChain / 1000000.0) + "ms");
    Assert.assertTrue("Render with Chain time scaling should not be more than 3x.", renderTimeChain < renderTime1 * 3);
}
Also used : WebXmlRenderContext(com.github.bordertech.wcomponents.servlet.WebXmlRenderContext) RenderContext(com.github.bordertech.wcomponents.RenderContext) WebXmlRenderContext(com.github.bordertech.wcomponents.servlet.WebXmlRenderContext) UIContext(com.github.bordertech.wcomponents.UIContext) MockRequest(com.github.bordertech.wcomponents.util.mock.MockRequest) NullWriter(com.github.bordertech.wcomponents.util.NullWriter) PrintWriter(java.io.PrintWriter) AllComponents(com.github.bordertech.wcomponents.AllComponents) Test(org.junit.Test)

Example 3 with NullWriter

use of com.github.bordertech.wcomponents.util.NullWriter in project wcomponents by BorderTech.

the class WebXmlRenderingPerformance_Test method testRenderingScaling.

@Test
public void testRenderingScaling() throws Exception {
    final RenderContext renderContext = new WebXmlRenderContext(new PrintWriter(new NullWriter()));
    final AllComponents component1 = new AllComponents();
    final UIContext uic1 = createUIContext();
    sendRequest(component1, uic1);
    final AllComponents component10 = new AllComponents(10);
    final UIContext uic10 = createUIContext();
    sendRequest(component10, uic10);
    Runnable runnable = new Runnable() {

        @Override
        public void run() {
            setActiveContext(uic1);
            for (int i = 0; i < NUM_REPETITIONS; i++) {
                component1.paint(renderContext);
            }
        }
    };
    // JIT warm-up
    runnable.run();
    long renderTime1 = time(runnable) / NUM_REPETITIONS;
    runnable = new Runnable() {

        @Override
        public void run() {
            setActiveContext(uic10);
            for (int i = 0; i < NUM_REPETITIONS; i++) {
                component10.paint(renderContext);
            }
        }
    };
    // JIT warm-up
    runnable.run();
    long renderTime10 = time(runnable) / NUM_REPETITIONS;
    LOG.info("Render 1x time: " + (renderTime1 / 1000000.0) + "ms");
    LOG.info("Render 10x time: " + (renderTime10 / 1000000.0) + "ms");
    Assert.assertTrue("Render time scaling should be O(n)", renderTime10 < renderTime1 * 12);
}
Also used : WebXmlRenderContext(com.github.bordertech.wcomponents.servlet.WebXmlRenderContext) RenderContext(com.github.bordertech.wcomponents.RenderContext) WebXmlRenderContext(com.github.bordertech.wcomponents.servlet.WebXmlRenderContext) UIContext(com.github.bordertech.wcomponents.UIContext) NullWriter(com.github.bordertech.wcomponents.util.NullWriter) PrintWriter(java.io.PrintWriter) AllComponents(com.github.bordertech.wcomponents.AllComponents) Test(org.junit.Test)

Example 4 with NullWriter

use of com.github.bordertech.wcomponents.util.NullWriter in project wcomponents by BorderTech.

the class WPopup_Test method testNotVisibleAfterPaint.

@Test
public void testNotVisibleAfterPaint() {
    WPopup popup = new WPopup();
    // Should default to not visible
    Assert.assertFalse("Popup should not be visible by default", popup.isVisible());
    // Make visible
    popup.setLocked(true);
    setActiveContext(createUIContext());
    popup.setVisible(true);
    // Check not visible after paint
    popup.paint(new WebXmlRenderContext(new PrintWriter(new NullWriter())));
    Assert.assertFalse("Popup should not be visible after paint", popup.isVisible());
}
Also used : WebXmlRenderContext(com.github.bordertech.wcomponents.servlet.WebXmlRenderContext) NullWriter(com.github.bordertech.wcomponents.util.NullWriter) PrintWriter(java.io.PrintWriter) Test(org.junit.Test)

Example 5 with NullWriter

use of com.github.bordertech.wcomponents.util.NullWriter in project wcomponents by BorderTech.

the class WComponentRenderPerfTest method runRenderTest.

/**
 * Runs the render test.
 */
private static void runRenderTest() {
    UIContextImpl uic = new UIContextImpl();
    PrintWriter printWriter = new PrintWriter(new NullWriter());
    RenderContext renderContext = new WebXmlRenderContext(printWriter);
    WComponent component = null;
    long baseLineMemory = getHeapUsed();
    try {
        component = (WComponent) Class.forName(CLASS_TO_TEST).newInstance();
    } catch (Exception e) {
        String msg = "Unable to instantiate test component: " + CLASS_TO_TEST;
        LOG.error(LINE_PREFIX + msg);
        throw new SystemException(msg, e);
    }
    long memBeforePaint = getHeapUsed() - baseLineMemory;
    // Set up velocity etc. to obtain a memory reading, and
    // so that the performance results aren't skewed too much
    UIContextHolder.pushContext(uic);
    try {
        component.paint(renderContext);
        long memAfterOnePaint = getHeapUsed() - baseLineMemory;
        long startTime = System.currentTimeMillis();
        // Figure out the loop overhead
        for (int i = 0; i < NUM_RENDERS; i++) {
        }
        long loopOverhead = System.currentTimeMillis() - startTime;
        startTime = System.currentTimeMillis();
        // Now run the render test
        for (int i = 0; i < NUM_RENDERS; i++) {
            component.paint(renderContext);
        }
        long elapsedTime = System.currentTimeMillis() - startTime - loopOverhead;
        long memAfterAllPaints = getHeapUsed() - baseLineMemory;
        LOG.info(LINE_PREFIX + "Memory use before paint: " + memBeforePaint);
        LOG.info(LINE_PREFIX + "Memory use after 1 paint: " + memAfterOnePaint);
        LOG.info(LINE_PREFIX + "Memory use after " + NUM_RENDERS + " paints: " + memAfterAllPaints);
        LOG.info(LINE_PREFIX + "Render time: " + (elapsedTime / (double) NUM_RENDERS) + "ms");
        Object[] treeAndSession = new Object[] { component, uic };
        ObjectGraphNode root = ObjectGraphDump.dump(treeAndSession);
        LOG.info(LINE_PREFIX + "Component mem use: " + ((ObjectGraphNode) root.getChildAt(0)).getSize());
        LOG.info(LINE_PREFIX + "UIC mem use: " + ((ObjectGraphNode) root.getChildAt(1)).getSize());
    } finally {
        UIContextHolder.popContext();
    }
}
Also used : WebXmlRenderContext(com.github.bordertech.wcomponents.servlet.WebXmlRenderContext) RenderContext(com.github.bordertech.wcomponents.RenderContext) WebXmlRenderContext(com.github.bordertech.wcomponents.servlet.WebXmlRenderContext) NullWriter(com.github.bordertech.wcomponents.util.NullWriter) IOException(java.io.IOException) SystemException(com.github.bordertech.wcomponents.util.SystemException) WComponent(com.github.bordertech.wcomponents.WComponent) SystemException(com.github.bordertech.wcomponents.util.SystemException) UIContextImpl(com.github.bordertech.wcomponents.UIContextImpl) ObjectGraphNode(com.github.bordertech.wcomponents.util.ObjectGraphNode) PrintWriter(java.io.PrintWriter)

Aggregations

NullWriter (com.github.bordertech.wcomponents.util.NullWriter)8 PrintWriter (java.io.PrintWriter)8 WebXmlRenderContext (com.github.bordertech.wcomponents.servlet.WebXmlRenderContext)7 Test (org.junit.Test)5 MockRequest (com.github.bordertech.wcomponents.util.mock.MockRequest)4 RenderContext (com.github.bordertech.wcomponents.RenderContext)3 UIContext (com.github.bordertech.wcomponents.UIContext)3 WServlet (com.github.bordertech.wcomponents.servlet.WServlet)3 MockHttpServletRequest (com.github.bordertech.wcomponents.util.mock.servlet.MockHttpServletRequest)3 AllComponents (com.github.bordertech.wcomponents.AllComponents)2 UIContextImpl (com.github.bordertech.wcomponents.UIContextImpl)2 InterceptorComponent (com.github.bordertech.wcomponents.container.InterceptorComponent)2 MockResponse (com.github.bordertech.wcomponents.util.mock.MockResponse)2 InternalResource (com.github.bordertech.wcomponents.InternalResource)1 MockLabel (com.github.bordertech.wcomponents.MockLabel)1 Response (com.github.bordertech.wcomponents.Response)1 WApplication (com.github.bordertech.wcomponents.WApplication)1 WComponent (com.github.bordertech.wcomponents.WComponent)1 WContent (com.github.bordertech.wcomponents.WContent)1 WWindow (com.github.bordertech.wcomponents.WWindow)1