List of usage examples for org.springframework.util StopWatch getTotalTimeMillis
public long getTotalTimeMillis()
From source file:org.springframework.aop.interceptor.CustomizableTraceInterceptor.java
/** * Writes a log message before the invocation based on the value of {@code enterMessage}. * If the invocation succeeds, then a log message is written on exit based on the value * {@code exitMessage}. If an exception occurs during invocation, then a message is * written based on the value of {@code exceptionMessage}. * @see #setEnterMessage/*from w w w . j a va 2s . c o m*/ * @see #setExitMessage * @see #setExceptionMessage */ @Override protected Object invokeUnderTrace(MethodInvocation invocation, Log logger) throws Throwable { String name = ClassUtils.getQualifiedMethodName(invocation.getMethod()); StopWatch stopWatch = new StopWatch(name); Object returnValue = null; boolean exitThroughException = false; try { stopWatch.start(name); writeToLog(logger, replacePlaceholders(this.enterMessage, invocation, null, null, -1)); returnValue = invocation.proceed(); return returnValue; } catch (Throwable ex) { if (stopWatch.isRunning()) { stopWatch.stop(); } exitThroughException = true; writeToLog(logger, replacePlaceholders(this.exceptionMessage, invocation, null, ex, stopWatch.getTotalTimeMillis()), ex); throw ex; } finally { if (!exitThroughException) { if (stopWatch.isRunning()) { stopWatch.stop(); } writeToLog(logger, replacePlaceholders(this.exitMessage, invocation, returnValue, null, stopWatch.getTotalTimeMillis())); } } }
From source file:org.springframework.beans.factory.DefaultListableBeanFactoryTests.java
@Test public void testPrototypeCreationIsFastEnough() { Assume.group(TestGroup.PERFORMANCE); Assume.notLogging(factoryLog);/* ww w . j av a 2 s. co m*/ DefaultListableBeanFactory lbf = new DefaultListableBeanFactory(); RootBeanDefinition rbd = new RootBeanDefinition(TestBean.class); rbd.setScope(RootBeanDefinition.SCOPE_PROTOTYPE); lbf.registerBeanDefinition("test", rbd); StopWatch sw = new StopWatch(); sw.start("prototype"); for (int i = 0; i < 100000; i++) { lbf.getBean("test"); } sw.stop(); // System.out.println(sw.getTotalTimeMillis()); assertTrue("Prototype creation took too long: " + sw.getTotalTimeMillis(), sw.getTotalTimeMillis() < 3000); }
From source file:org.springframework.beans.factory.DefaultListableBeanFactoryTests.java
@Test public void testPrototypeCreationWithDependencyCheckIsFastEnough() { Assume.group(TestGroup.PERFORMANCE); Assume.notLogging(factoryLog);//from w ww . j a v a 2 s . c om DefaultListableBeanFactory lbf = new DefaultListableBeanFactory(); RootBeanDefinition rbd = new RootBeanDefinition(LifecycleBean.class); rbd.setScope(RootBeanDefinition.SCOPE_PROTOTYPE); rbd.setDependencyCheck(RootBeanDefinition.DEPENDENCY_CHECK_OBJECTS); lbf.registerBeanDefinition("test", rbd); lbf.addBeanPostProcessor(new LifecycleBean.PostProcessor()); StopWatch sw = new StopWatch(); sw.start("prototype"); for (int i = 0; i < 100000; i++) { lbf.getBean("test"); } sw.stop(); // System.out.println(sw.getTotalTimeMillis()); assertTrue("Prototype creation took too long: " + sw.getTotalTimeMillis(), sw.getTotalTimeMillis() < 3000); }
From source file:org.springframework.beans.factory.DefaultListableBeanFactoryTests.java
/** * @Test/*from ww w . j a v a 2 s .com*/ * public void testPrototypeCreationIsFastEnough2() throws Exception { * if (factoryLog.isTraceEnabled() || factoryLog.isDebugEnabled()) { * // Skip this test: Trace logging blows the time limit. * return; * } * DefaultListableBeanFactory lbf = new DefaultListableBeanFactory(); * Method setBeanNameMethod = TestBean.class.getMethod("setBeanName", String.class); * Method setBeanFactoryMethod = TestBean.class.getMethod("setBeanFactory", BeanFactory.class); * StopWatch sw = new StopWatch(); * sw.start("prototype"); * for (int i = 0; i < 100000; i++) { * TestBean tb = TestBean.class.newInstance(); * setBeanNameMethod.invoke(tb, "test"); * setBeanFactoryMethod.invoke(tb, lbf); * } * sw.stop(); * // System.out.println(sw.getTotalTimeMillis()); * assertTrue("Prototype creation took too long: " + sw.getTotalTimeMillis(), sw.getTotalTimeMillis() < 500); * } */ @Test @Ignore // TODO re-enable when ConstructorResolver TODO sorted out public void testPrototypeCreationWithConstructorArgumentsIsFastEnough() { Assume.group(TestGroup.PERFORMANCE); Assume.notLogging(factoryLog); DefaultListableBeanFactory lbf = new DefaultListableBeanFactory(); RootBeanDefinition rbd = new RootBeanDefinition(TestBean.class); rbd.setScope(RootBeanDefinition.SCOPE_PROTOTYPE); rbd.getConstructorArgumentValues().addGenericArgumentValue("juergen"); rbd.getConstructorArgumentValues().addGenericArgumentValue("99"); lbf.registerBeanDefinition("test", rbd); StopWatch sw = new StopWatch(); sw.start("prototype"); for (int i = 0; i < 100000; i++) { TestBean tb = (TestBean) lbf.getBean("test"); assertEquals("juergen", tb.getName()); assertEquals(99, tb.getAge()); } sw.stop(); // System.out.println(sw.getTotalTimeMillis()); assertTrue("Prototype creation took too long: " + sw.getTotalTimeMillis(), sw.getTotalTimeMillis() < 3000); }
From source file:org.springframework.beans.factory.DefaultListableBeanFactoryTests.java
@Test public void testPrototypeCreationWithResolvedConstructorArgumentsIsFastEnough() { Assume.group(TestGroup.PERFORMANCE); Assume.notLogging(factoryLog);//from www .ja v a 2 s . co m DefaultListableBeanFactory lbf = new DefaultListableBeanFactory(); RootBeanDefinition rbd = new RootBeanDefinition(TestBean.class); rbd.setScope(RootBeanDefinition.SCOPE_PROTOTYPE); rbd.getConstructorArgumentValues().addGenericArgumentValue(new RuntimeBeanReference("spouse")); lbf.registerBeanDefinition("test", rbd); lbf.registerBeanDefinition("spouse", new RootBeanDefinition(TestBean.class)); TestBean spouse = (TestBean) lbf.getBean("spouse"); StopWatch sw = new StopWatch(); sw.start("prototype"); for (int i = 0; i < 100000; i++) { TestBean tb = (TestBean) lbf.getBean("test"); assertSame(spouse, tb.getSpouse()); } sw.stop(); // System.out.println(sw.getTotalTimeMillis()); assertTrue("Prototype creation took too long: " + sw.getTotalTimeMillis(), sw.getTotalTimeMillis() < 4000); }
From source file:org.springframework.beans.factory.DefaultListableBeanFactoryTests.java
@Test public void testPrototypeCreationWithPropertiesIsFastEnough() { Assume.group(TestGroup.PERFORMANCE); Assume.notLogging(factoryLog);//w ww . j a v a 2s . co m DefaultListableBeanFactory lbf = new DefaultListableBeanFactory(); RootBeanDefinition rbd = new RootBeanDefinition(TestBean.class); rbd.setScope(RootBeanDefinition.SCOPE_PROTOTYPE); rbd.getPropertyValues().add("name", "juergen"); rbd.getPropertyValues().add("age", "99"); lbf.registerBeanDefinition("test", rbd); StopWatch sw = new StopWatch(); sw.start("prototype"); for (int i = 0; i < 100000; i++) { TestBean tb = (TestBean) lbf.getBean("test"); assertEquals("juergen", tb.getName()); assertEquals(99, tb.getAge()); } sw.stop(); // System.out.println(sw.getTotalTimeMillis()); assertTrue("Prototype creation took too long: " + sw.getTotalTimeMillis(), sw.getTotalTimeMillis() < 3000); }
From source file:org.springframework.beans.factory.DefaultListableBeanFactoryTests.java
@Test public void testPrototypeCreationWithResolvedPropertiesIsFastEnough() { Assume.group(TestGroup.PERFORMANCE); Assume.notLogging(factoryLog);/*from w ww.jav a2 s . com*/ DefaultListableBeanFactory lbf = new DefaultListableBeanFactory(); RootBeanDefinition rbd = new RootBeanDefinition(TestBean.class); rbd.setScope(RootBeanDefinition.SCOPE_PROTOTYPE); rbd.getPropertyValues().add("spouse", new RuntimeBeanReference("spouse")); lbf.registerBeanDefinition("test", rbd); lbf.registerBeanDefinition("spouse", new RootBeanDefinition(TestBean.class)); TestBean spouse = (TestBean) lbf.getBean("spouse"); StopWatch sw = new StopWatch(); sw.start("prototype"); for (int i = 0; i < 100000; i++) { TestBean tb = (TestBean) lbf.getBean("test"); assertSame(spouse, tb.getSpouse()); } sw.stop(); // System.out.println(sw.getTotalTimeMillis()); assertTrue("Prototype creation took too long: " + sw.getTotalTimeMillis(), sw.getTotalTimeMillis() < 4000); }
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); }/*from w w w .j a v a 2s. c om*/ 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
@Override protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain chain) throws ServletException, IOException { StopWatch stopWatch = createStopWatchIfNecessary(request); String path = new UrlPathHelper().getPathWithinApplication(request); int status = HttpStatus.INTERNAL_SERVER_ERROR.value(); try {/*from w ww.j ava2 s. c o m*/ chain.doFilter(request, response); status = getStatus(response); } finally { if (!request.isAsyncStarted()) { stopWatch.stop(); request.removeAttribute(ATTRIBUTE_STOP_WATCH); recordMetrics(request, path, status, stopWatch.getTotalTimeMillis()); } } }
From source file:org.springframework.boot.actuate.metrics.web.servlet.MetricsFilter.java
@Override protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain chain) throws ServletException, IOException { StopWatch stopWatch = createStopWatchIfNecessary(request); String path = new UrlPathHelper().getPathWithinApplication(request); int status = HttpStatus.INTERNAL_SERVER_ERROR.value(); try {/*from www . j a v a2 s .co m*/ chain.doFilter(request, response); status = getStatus(response); } finally { if (!request.isAsyncStarted()) { if (response.isCommitted()) { status = getStatus(response); } stopWatch.stop(); request.removeAttribute(ATTRIBUTE_STOP_WATCH); recordMetrics(request, path, status, stopWatch.getTotalTimeMillis()); } } }