Example usage for org.springframework.util StopWatch StopWatch

List of usage examples for org.springframework.util StopWatch StopWatch

Introduction

In this page you can find the example usage for org.springframework.util StopWatch StopWatch.

Prototype

public StopWatch() 

Source Link

Document

Construct a new StopWatch .

Usage

From source file:org.springframework.beans.factory.xml.XmlBeanFactoryTests.java

@Test
public void lookupOverrideMethodsWithSetterInjection() {
    DefaultListableBeanFactory xbf = new DefaultListableBeanFactory();
    XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(xbf);
    reader.loadBeanDefinitions(OVERRIDES_CONTEXT);

    lookupOverrideMethodsWithSetterInjection(xbf, "overrideOneMethod", true);
    // Should work identically on subclass definition, in which lookup
    // methods are inherited
    lookupOverrideMethodsWithSetterInjection(xbf, "overrideInheritedMethod", true);

    // Check cost of repeated construction of beans with method overrides
    // Will pick up misuse of CGLIB
    int howMany = 100;
    StopWatch sw = new StopWatch();
    sw.start("Look up " + howMany + " prototype bean instances with method overrides");
    for (int i = 0; i < howMany; i++) {
        lookupOverrideMethodsWithSetterInjection(xbf, "overrideOnPrototype", false);
    }/* www.ja v  a2s .c o  m*/
    sw.stop();
    // System.out.println(sw);
    if (!LogFactory.getLog(DefaultListableBeanFactory.class).isDebugEnabled()) {
        assertTrue(sw.getTotalTimeMillis() < 2000);
    }

    // Now test distinct bean with swapped value in factory, to ensure the two are independent
    OverrideOneMethod swappedOom = (OverrideOneMethod) xbf.getBean("overrideOneMethodSwappedReturnValues");

    TestBean tb = swappedOom.getPrototypeDependency();
    assertEquals("David", tb.getName());
    tb = swappedOom.protectedOverrideSingleton();
    assertEquals("Jenny", tb.getName());
}

From source file:org.springframework.boot.actuate.autoconfigure.MetricsFilter.java

private StopWatch createStopWatchIfNecessary(HttpServletRequest request) {
    StopWatch stopWatch = (StopWatch) request.getAttribute(ATTRIBUTE_STOP_WATCH);
    if (stopWatch == null) {
        stopWatch = new StopWatch();
        stopWatch.start();/*from   w w  w .  ja  v  a 2s .  c  o  m*/
        request.setAttribute(ATTRIBUTE_STOP_WATCH, stopWatch);
    }
    return stopWatch;
}

From source file:org.springframework.boot.SpringApplication.java

/**
 * Run the Spring application, creating and refreshing a new
 * {@link ApplicationContext}.//from ww  w .  j  av  a 2 s .  c  om
 * @param args the application arguments (usually passed from a Java main method)
 * @return a running {@link ApplicationContext}
 */
public ConfigurableApplicationContext run(String... args) {
    StopWatch stopWatch = new StopWatch();
    stopWatch.start();
    ConfigurableApplicationContext context = null;
    configureHeadlessProperty();
    SpringApplicationRunListeners listeners = getRunListeners(args);
    listeners.started();
    try {
        context = doRun(listeners, args);
        stopWatch.stop();
        if (this.logStartupInfo) {
            new StartupInfoLogger(this.mainApplicationClass).logStarted(getApplicationLog(), stopWatch);
        }
        return context;
    } catch (Throwable ex) {
        try {
            listeners.finished(context, ex);
            this.log.error("Application startup failed", ex);
        } finally {
            if (context != null) {
                context.close();
            }
        }
        ReflectionUtils.rethrowRuntimeException(ex);
        return context;
    }
}

From source file:org.springframework.cloud.gateway.test.support.AbstractHttpServer.java

@Override
public final void start() {
    synchronized (this.lifecycleMonitor) {
        if (!isRunning()) {
            String serverName = getClass().getSimpleName();
            if (logger.isDebugEnabled()) {
                logger.debug("Starting " + serverName + "...");
            }/*from www . j  a  v  a2 s.  c o  m*/
            this.running = true;
            try {
                StopWatch stopWatch = new StopWatch();
                stopWatch.start();
                startInternal();
                long millis = stopWatch.getTotalTimeMillis();
                if (logger.isDebugEnabled()) {
                    logger.debug("Server started on port " + getPort() + "(" + millis + " millis).");
                }
            } catch (Throwable ex) {
                throw new IllegalStateException(ex);
            }
        }
    }

}

From source file:org.springframework.cloud.gateway.test.support.AbstractHttpServer.java

@Override
public final void stop() {
    synchronized (this.lifecycleMonitor) {
        if (isRunning()) {
            String serverName = getClass().getSimpleName();
            logger.debug("Stopping " + serverName + "...");
            this.running = false;
            try {
                StopWatch stopWatch = new StopWatch();
                stopWatch.start();//  ww  w .j a va  2 s.  com
                stopInternal();
                logger.debug("Server stopped (" + stopWatch.getTotalTimeMillis() + " millis).");
            } catch (Throwable ex) {
                throw new IllegalStateException(ex);
            } finally {
                reset();
            }
        }
    }
}

From source file:org.springframework.context.annotation.AnnotationProcessorPerformanceTests.java

@Test
public void testPrototypeCreationWithResourcePropertiesIsFastEnough() {
    GenericApplicationContext ctx = new GenericApplicationContext();
    AnnotationConfigUtils.registerAnnotationConfigProcessors(ctx);
    ctx.refresh();//ww w  . j a  v a  2  s .  c  o m

    RootBeanDefinition rbd = new RootBeanDefinition(ResourceAnnotatedTestBean.class);
    rbd.setScope(RootBeanDefinition.SCOPE_PROTOTYPE);
    ctx.registerBeanDefinition("test", rbd);
    ctx.registerBeanDefinition("spouse", new RootBeanDefinition(TestBean.class));
    TestBean spouse = (TestBean) ctx.getBean("spouse");
    StopWatch sw = new StopWatch();
    sw.start("prototype");
    for (int i = 0; i < 100000; i++) {
        TestBean tb = (TestBean) ctx.getBean("test");
        assertSame(spouse, tb.getSpouse());
    }
    sw.stop();
    assertTrue("Prototype creation took too long: " + sw.getTotalTimeMillis(), sw.getTotalTimeMillis() < 4000);
}

From source file:org.springframework.context.annotation.AnnotationProcessorPerformanceTests.java

@Test
public void testPrototypeCreationWithOverriddenResourcePropertiesIsFastEnough() {
    GenericApplicationContext ctx = new GenericApplicationContext();
    AnnotationConfigUtils.registerAnnotationConfigProcessors(ctx);
    ctx.refresh();/* ww  w  .  j a v a2  s  . c  o m*/

    RootBeanDefinition rbd = new RootBeanDefinition(ResourceAnnotatedTestBean.class);
    rbd.setScope(RootBeanDefinition.SCOPE_PROTOTYPE);
    rbd.getPropertyValues().add("spouse", new RuntimeBeanReference("spouse"));
    ctx.registerBeanDefinition("test", rbd);
    ctx.registerBeanDefinition("spouse", new RootBeanDefinition(TestBean.class));
    TestBean spouse = (TestBean) ctx.getBean("spouse");
    StopWatch sw = new StopWatch();
    sw.start("prototype");
    for (int i = 0; i < 100000; i++) {
        TestBean tb = (TestBean) ctx.getBean("test");
        assertSame(spouse, tb.getSpouse());
    }
    sw.stop();
    assertTrue("Prototype creation took too long: " + sw.getTotalTimeMillis(), sw.getTotalTimeMillis() < 4000);
}

From source file:org.springframework.context.annotation.AnnotationProcessorPerformanceTests.java

@Test
public void testPrototypeCreationWithAutowiredPropertiesIsFastEnough() {
    GenericApplicationContext ctx = new GenericApplicationContext();
    AnnotationConfigUtils.registerAnnotationConfigProcessors(ctx);
    ctx.refresh();/*from ww  w . ja v a2  s  . c om*/

    RootBeanDefinition rbd = new RootBeanDefinition(AutowiredAnnotatedTestBean.class);
    rbd.setScope(RootBeanDefinition.SCOPE_PROTOTYPE);
    ctx.registerBeanDefinition("test", rbd);
    ctx.registerBeanDefinition("spouse", new RootBeanDefinition(TestBean.class));
    TestBean spouse = (TestBean) ctx.getBean("spouse");
    StopWatch sw = new StopWatch();
    sw.start("prototype");
    for (int i = 0; i < 100000; i++) {
        TestBean tb = (TestBean) ctx.getBean("test");
        assertSame(spouse, tb.getSpouse());
    }
    sw.stop();
    assertTrue("Prototype creation took too long: " + sw.getTotalTimeMillis(), sw.getTotalTimeMillis() < 4000);
}

From source file:org.springframework.context.annotation.AnnotationProcessorPerformanceTests.java

@Test
public void testPrototypeCreationWithOverriddenAutowiredPropertiesIsFastEnough() {
    GenericApplicationContext ctx = new GenericApplicationContext();
    AnnotationConfigUtils.registerAnnotationConfigProcessors(ctx);
    ctx.refresh();/*from   w  w w . ja  v a 2s  .  c  om*/

    RootBeanDefinition rbd = new RootBeanDefinition(AutowiredAnnotatedTestBean.class);
    rbd.setScope(RootBeanDefinition.SCOPE_PROTOTYPE);
    rbd.getPropertyValues().add("spouse", new RuntimeBeanReference("spouse"));
    ctx.registerBeanDefinition("test", rbd);
    ctx.registerBeanDefinition("spouse", new RootBeanDefinition(TestBean.class));
    TestBean spouse = (TestBean) ctx.getBean("spouse");
    StopWatch sw = new StopWatch();
    sw.start("prototype");
    for (int i = 0; i < 100000; i++) {
        TestBean tb = (TestBean) ctx.getBean("test");
        assertSame(spouse, tb.getSpouse());
    }
    sw.stop();
    assertTrue("Prototype creation took too long: " + sw.getTotalTimeMillis(), sw.getTotalTimeMillis() < 6000);
}

From source file:org.springframework.context.expression.ApplicationContextExpressionTests.java

@Test
public void prototypeCreationIsFastEnough() {
    Assume.group(TestGroup.PERFORMANCE);
    Assume.notLogging(factoryLog);//from  ww  w.j a v  a 2s  . co m
    GenericApplicationContext ac = new GenericApplicationContext();
    RootBeanDefinition rbd = new RootBeanDefinition(TestBean.class);
    rbd.setScope(RootBeanDefinition.SCOPE_PROTOTYPE);
    rbd.getConstructorArgumentValues().addGenericArgumentValue("#{systemProperties.name}");
    rbd.getPropertyValues().add("country", "#{systemProperties.country}");
    ac.registerBeanDefinition("test", rbd);
    ac.refresh();
    StopWatch sw = new StopWatch();
    sw.start("prototype");
    System.getProperties().put("name", "juergen");
    System.getProperties().put("country", "UK");
    try {
        for (int i = 0; i < 100000; i++) {
            TestBean tb = (TestBean) ac.getBean("test");
            assertEquals("juergen", tb.getName());
            assertEquals("UK", tb.getCountry());
        }
        sw.stop();
    } finally {
        System.getProperties().remove("country");
        System.getProperties().remove("name");
    }
    assertTrue("Prototype creation took too long: " + sw.getTotalTimeMillis(), sw.getTotalTimeMillis() < 6000);
}