Example usage for org.springframework.util StopWatch stop

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

Introduction

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

Prototype

public void stop() throws IllegalStateException 

Source Link

Document

Stop the current task.

Usage

From source file:org.springframework.aop.aspectj.autoproxy.AspectJAutoProxyCreatorTests.java

@Test
public void testAspectsAndAdvisorNotAppliedToPrototypeIsFastEnough() {
    Assume.group(TestGroup.PERFORMANCE);
    Assume.notLogging(factoryLog);//from w  ww  .j a  v  a2 s.c o  m
    ClassPathXmlApplicationContext ac = newContext("aspectsPlusAdvisor.xml");
    StopWatch sw = new StopWatch();
    sw.start("Prototype Creation");
    for (int i = 0; i < 100000; i++) {
        INestedTestBean shouldNotBeWeaved = (INestedTestBean) ac.getBean("i21");
        if (i < 10) {
            assertFalse(AopUtils.isAopProxy(shouldNotBeWeaved));
        }
    }
    sw.stop();

    // What's a reasonable expectation for _any_ server or developer machine load?
    // 3 seconds?
    assertStopWatchTimeLimit(sw, 6000);
}

From source file:org.springframework.aop.aspectj.autoproxy.AspectJAutoProxyCreatorTests.java

@Test
public void testAspectsAndAdvisorNotAppliedToManySingletonsIsFastEnough() {
    Assume.group(TestGroup.PERFORMANCE);
    Assume.notLogging(factoryLog);/*from w  w w. j av  a  2s .c o  m*/
    GenericApplicationContext ac = new GenericApplicationContext();
    new XmlBeanDefinitionReader(ac)
            .loadBeanDefinitions(new ClassPathResource(qName("aspectsPlusAdvisor.xml"), getClass()));
    for (int i = 0; i < 10000; i++) {
        ac.registerBeanDefinition("singleton" + i, new RootBeanDefinition(NestedTestBean.class));
    }
    StopWatch sw = new StopWatch();
    sw.start("Singleton Creation");
    ac.refresh();
    sw.stop();

    // What's a reasonable expectation for _any_ server or developer machine load?
    // 8 seconds?
    assertStopWatchTimeLimit(sw, 8000);
}

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  ww  .j av  a2 s.  co  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.aop.interceptor.PerformanceMonitorInterceptor.java

@Override
protected Object invokeUnderTrace(MethodInvocation invocation, Log logger) throws Throwable {
    String name = createInvocationTraceName(invocation);
    StopWatch stopWatch = new StopWatch(name);
    stopWatch.start(name);/*  ww  w  . j  av  a  2  s  . com*/
    try {
        return invocation.proceed();
    } finally {
        stopWatch.stop();
        writeToLog(logger, stopWatch.shortSummary());
    }
}

From source file:org.springframework.batch.admin.jmx.StepExecutionServiceLevelMonitor.java

public void invoke(ProceedingJoinPoint joinPoint, final StepExecution stepExecution, Step step)
        throws Throwable {

    final AtomicBoolean finished = new AtomicBoolean(false);
    final StopWatch timer = new StopWatch(stepExecution.getStepName() + ":execution");

    try {/*from  www  . j a v a2 s .c  o m*/

        if (timeout > 0) {

            if (notificationPublisher != null) {
                Notification notification = new Notification("INFO", this, sequence++,
                        "Starting:" + stepExecution);
                notificationPublisher.sendNotification(notification);
            }

            timer.start("StepExecution.Id:" + stepExecution.getId());
            final long threshold = (long) (timeout * (1 - warningMargin));
            Date warningTime = new Date(System.currentTimeMillis() + threshold);
            logger.debug("Scheduling warning after (ms) " + threshold);
            taskScheduler.schedule(new Runnable() {

                public void run() {
                    if (!finished.get()) {
                        logger.debug("Sending warning (step not complete after " + threshold + " ms): "
                                + stepExecution);
                        if (notificationPublisher != null) {
                            Notification notification = new Notification("WARN",
                                    StepExecutionServiceLevelMonitor.this, sequence++,
                                    "Warning:" + stepExecution);
                            notificationPublisher.sendNotification(notification);
                        }
                    } else {
                        logger.debug("No warning necessary for " + stepExecution);
                    }
                }

            }, warningTime);
        }

        joinPoint.proceed();

    } finally {

        finished.set(true);

        if (timeout > 0) {
            timer.stop();
            Executors.newSingleThreadScheduledExecutor().shutdown();
            if (timer.getLastTaskTimeMillis() > timeout) {
                overruns++;
                logger.debug("Notifying overrun " + stepExecution);
                if (notificationPublisher != null) {
                    Notification notification = new Notification("ERROR", this, sequence++,
                            "Overrun:" + stepExecution);
                    notificationPublisher.sendNotification(notification);
                }
            }
        }

    }

}

From source file:org.springframework.batch.core.scope.StepScopePerformanceTests.java

private int doTest(String name, String test) throws Exception {
    @SuppressWarnings("unchecked")
    ItemStreamReader<String> reader = (ItemStreamReader<String>) applicationContext.getBean(name);
    reader.open(new ExecutionContext());
    StopWatch stopWatch = new StopWatch(test);
    stopWatch.start();/*from   w w w .  j av a2  s .  c  om*/
    int count = 0;
    while (reader.read() != null) {
        // do nothing
        count++;
    }
    stopWatch.stop();
    reader.close();
    logger.info(stopWatch.shortSummary());
    return count;
}

From source file:org.springframework.batch.item.xml.AbstractStaxEventWriterItemWriterTests.java

/**
 * Write list of domain objects and check the output file.
 *//*from ww w.ja v a2  s. co m*/
@SuppressWarnings("resource")
@Test
public void testWrite() throws Exception {
    StopWatch stopWatch = new StopWatch(getClass().getSimpleName());
    stopWatch.start();
    for (int i = 0; i < MAX_WRITE; i++) {
        new TransactionTemplate(new ResourcelessTransactionManager()).execute(new TransactionCallback<Void>() {
            @Override
            public Void doInTransaction(TransactionStatus status) {
                try {
                    writer.write(objects);
                } catch (RuntimeException e) {
                    throw e;
                } catch (Exception e) {
                    throw new IllegalStateException("Exception encountered on write", e);
                }
                return null;
            }
        });
    }
    writer.close();
    stopWatch.stop();
    logger.info("Timing for XML writer: " + stopWatch);
    XMLUnit.setIgnoreWhitespace(true);
    // String content = FileUtils.readFileToString(resource.getFile());
    // System.err.println(content);
    XMLAssert.assertXMLEqual(new FileReader(expected.getFile()), new FileReader(resource.getFile()));

}

From source file:org.springframework.beans.AbstractPropertyAccessorTests.java

@Test
public void setPrimitiveArrayPropertyLargeMatching() {
    Assume.group(TestGroup.PERFORMANCE);
    Assume.notLogging(LogFactory.getLog(AbstractPropertyAccessorTests.class));

    PrimitiveArrayBean target = new PrimitiveArrayBean();
    AbstractPropertyAccessor accessor = createAccessor(target);
    int[] input = new int[1024];
    StopWatch sw = new StopWatch();
    sw.start("array1");
    for (int i = 0; i < 1000; i++) {
        accessor.setPropertyValue("array", input);
    }/*from  w w w.ja  v a2 s  . c  o m*/
    sw.stop();
    assertEquals(1024, target.getArray().length);
    assertEquals(0, target.getArray()[0]);
    long time1 = sw.getLastTaskTimeMillis();
    assertTrue("Took too long", sw.getLastTaskTimeMillis() < 100);

    accessor.registerCustomEditor(String.class, new StringTrimmerEditor(false));
    sw.start("array2");
    for (int i = 0; i < 1000; i++) {
        accessor.setPropertyValue("array", input);
    }
    sw.stop();
    assertTrue("Took too long", sw.getLastTaskTimeMillis() < 125);

    accessor.registerCustomEditor(int.class, "array.somePath", new CustomNumberEditor(Integer.class, false));
    sw.start("array3");
    for (int i = 0; i < 1000; i++) {
        accessor.setPropertyValue("array", input);
    }
    sw.stop();
    assertTrue("Took too long", sw.getLastTaskTimeMillis() < 100);

    accessor.registerCustomEditor(int.class, "array[0].somePath", new CustomNumberEditor(Integer.class, false));
    sw.start("array3");
    for (int i = 0; i < 1000; i++) {
        accessor.setPropertyValue("array", input);
    }
    sw.stop();
    assertTrue("Took too long", sw.getLastTaskTimeMillis() < 100);

    accessor.registerCustomEditor(int.class, new CustomNumberEditor(Integer.class, false));
    sw.start("array4");
    for (int i = 0; i < 100; i++) {
        accessor.setPropertyValue("array", input);
    }
    sw.stop();
    assertEquals(1024, target.getArray().length);
    assertEquals(0, target.getArray()[0]);
    assertTrue("Took too long", sw.getLastTaskTimeMillis() > time1);
}

From source file:org.springframework.beans.BeanWrapperTests.java

@Test
public void testLargeMatchingPrimitiveArray() {
    Assume.group(TestGroup.PERFORMANCE);
    Assume.notLogging(LogFactory.getLog(BeanWrapperTests.class));

    PrimitiveArrayBean tb = new PrimitiveArrayBean();
    BeanWrapper bw = new BeanWrapperImpl(tb);
    int[] input = new int[1024];
    StopWatch sw = new StopWatch();
    sw.start("array1");
    for (int i = 0; i < 1000; i++) {
        bw.setPropertyValue("array", input);
    }//from  www .j  av  a2  s  .  c  om
    sw.stop();
    assertEquals(1024, tb.getArray().length);
    assertEquals(0, tb.getArray()[0]);
    long time1 = sw.getLastTaskTimeMillis();
    assertTrue("Took too long", sw.getLastTaskTimeMillis() < 100);

    bw.registerCustomEditor(String.class, new StringTrimmerEditor(false));
    sw.start("array2");
    for (int i = 0; i < 1000; i++) {
        bw.setPropertyValue("array", input);
    }
    sw.stop();
    assertTrue("Took too long", sw.getLastTaskTimeMillis() < 125);

    bw.registerCustomEditor(int.class, "array.somePath", new CustomNumberEditor(Integer.class, false));
    sw.start("array3");
    for (int i = 0; i < 1000; i++) {
        bw.setPropertyValue("array", input);
    }
    sw.stop();
    assertTrue("Took too long", sw.getLastTaskTimeMillis() < 100);

    bw.registerCustomEditor(int.class, "array[0].somePath", new CustomNumberEditor(Integer.class, false));
    sw.start("array3");
    for (int i = 0; i < 1000; i++) {
        bw.setPropertyValue("array", input);
    }
    sw.stop();
    assertTrue("Took too long", sw.getLastTaskTimeMillis() < 100);

    bw.registerCustomEditor(int.class, new CustomNumberEditor(Integer.class, false));
    sw.start("array4");
    for (int i = 0; i < 100; i++) {
        bw.setPropertyValue("array", input);
    }
    sw.stop();
    assertEquals(1024, tb.getArray().length);
    assertEquals(0, tb.getArray()[0]);
    assertTrue("Took too long", sw.getLastTaskTimeMillis() > time1);
}

From source file:org.springframework.beans.factory.DefaultListableBeanFactoryTests.java

@Test
public void testPrototypeCreationIsFastEnough() {
    Assume.group(TestGroup.PERFORMANCE);
    Assume.notLogging(factoryLog);//w  w  w .j ava  2  s  .c  o  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);
}