List of usage examples for org.springframework.util StopWatch stop
public void stop() throws IllegalStateException
From source file:org.springframework.context.annotation.AnnotationProcessorPerformanceTests.java
@Test public void testPrototypeCreationWithOverriddenResourcePropertiesIsFastEnough() { GenericApplicationContext ctx = new GenericApplicationContext(); AnnotationConfigUtils.registerAnnotationConfigProcessors(ctx); ctx.refresh();/*w ww . j av a 2s. co 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();/* w w w .j a v a 2 s. c o m*/ 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. j ava2 s . c o m*/ 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 va 2 s .c o 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); }
From source file:org.springframework.integration.aggregator.AggregatorTests.java
@Test public void testAggPerf() throws InterruptedException, ExecutionException, TimeoutException { AggregatingMessageHandler handler = new AggregatingMessageHandler( new DefaultAggregatingMessageGroupProcessor()); handler.setCorrelationStrategy(message -> "foo"); handler.setReleaseStrategy(new MessageCountReleaseStrategy(60000)); handler.setExpireGroupsUponCompletion(true); handler.setSendPartialResultOnExpiry(true); DirectChannel outputChannel = new DirectChannel(); handler.setOutputChannel(outputChannel); final CompletableFuture<Collection<?>> resultFuture = new CompletableFuture<>(); outputChannel.subscribe(message -> { Collection<?> payload = (Collection<?>) message.getPayload(); logger.warn("Received " + payload.size()); resultFuture.complete(payload);/*from www .j a v a 2 s. c om*/ }); SimpleMessageStore store = new SimpleMessageStore(); SimpleMessageGroupFactory messageGroupFactory = new SimpleMessageGroupFactory( SimpleMessageGroupFactory.GroupType.BLOCKING_QUEUE); store.setMessageGroupFactory(messageGroupFactory); handler.setMessageStore(store); Message<?> message = new GenericMessage<String>("foo"); StopWatch stopwatch = new StopWatch(); stopwatch.start(); for (int i = 0; i < 120000; i++) { if (i % 10000 == 0) { stopwatch.stop(); logger.warn("Sent " + i + " in " + stopwatch.getTotalTimeSeconds() + " (10k in " + stopwatch.getLastTaskTimeMillis() + "ms)"); stopwatch.start(); } handler.handleMessage(message); } stopwatch.stop(); logger.warn("Sent " + 120000 + " in " + stopwatch.getTotalTimeSeconds() + " (10k in " + stopwatch.getLastTaskTimeMillis() + "ms)"); Collection<?> result = resultFuture.get(10, TimeUnit.SECONDS); assertNotNull(result); assertEquals(60000, result.size()); }
From source file:org.springframework.integration.aggregator.AggregatorTests.java
@Test public void testAggPerfDefaultPartial() throws InterruptedException, ExecutionException, TimeoutException { AggregatingMessageHandler handler = new AggregatingMessageHandler( new DefaultAggregatingMessageGroupProcessor()); handler.setCorrelationStrategy(message -> "foo"); handler.setReleasePartialSequences(true); DirectChannel outputChannel = new DirectChannel(); handler.setOutputChannel(outputChannel); final CompletableFuture<Collection<?>> resultFuture = new CompletableFuture<>(); outputChannel.subscribe(message -> { Collection<?> payload = (Collection<?>) message.getPayload(); logger.warn("Received " + payload.size()); resultFuture.complete(payload);/*from w w w .ja v a 2 s .c o m*/ }); SimpleMessageStore store = new SimpleMessageStore(); SimpleMessageGroupFactory messageGroupFactory = new SimpleMessageGroupFactory( SimpleMessageGroupFactory.GroupType.BLOCKING_QUEUE); store.setMessageGroupFactory(messageGroupFactory); handler.setMessageStore(store); StopWatch stopwatch = new StopWatch(); stopwatch.start(); for (int i = 0; i < 120000; i++) { if (i % 10000 == 0) { stopwatch.stop(); logger.warn("Sent " + i + " in " + stopwatch.getTotalTimeSeconds() + " (10k in " + stopwatch.getLastTaskTimeMillis() + "ms)"); stopwatch.start(); } handler.handleMessage( MessageBuilder.withPayload("foo").setSequenceSize(120000).setSequenceNumber(i + 1).build()); } stopwatch.stop(); logger.warn("Sent " + 120000 + " in " + stopwatch.getTotalTimeSeconds() + " (10k in " + stopwatch.getLastTaskTimeMillis() + "ms)"); Collection<?> result = resultFuture.get(10, TimeUnit.SECONDS); assertNotNull(result); assertEquals(120000, result.size()); assertThat(stopwatch.getTotalTimeSeconds(), lessThan(60.0)); // actually < 2.0, was many minutes }
From source file:org.springframework.integration.aggregator.AggregatorTests.java
@Test public void testCustomAggPerf() throws InterruptedException, ExecutionException, TimeoutException { class CustomHandler extends AbstractMessageHandler { // custom aggregator, only handles a single correlation private final ReentrantLock lock = new ReentrantLock(); private final Collection<Message<?>> messages = new ArrayList<Message<?>>(60000); private final MessageChannel outputChannel; private CustomHandler(MessageChannel outputChannel) { this.outputChannel = outputChannel; }/*from w w w .j a va2 s . c o m*/ @Override public void handleMessageInternal(Message<?> requestMessage) { lock.lock(); try { this.messages.add(requestMessage); if (this.messages.size() == 60000) { List<Object> payloads = new ArrayList<Object>(this.messages.size()); for (Message<?> message : this.messages) { payloads.add(message.getPayload()); } this.messages.clear(); outputChannel.send(getMessageBuilderFactory().withPayload(payloads) .copyHeaders(requestMessage.getHeaders()).build()); } } finally { lock.unlock(); } } } DirectChannel outputChannel = new DirectChannel(); CustomHandler handler = new CustomHandler(outputChannel); final CompletableFuture<Collection<?>> resultFuture = new CompletableFuture<>(); outputChannel.subscribe(message -> { Collection<?> payload = (Collection<?>) message.getPayload(); logger.warn("Received " + payload.size()); resultFuture.complete(payload); }); Message<?> message = new GenericMessage<String>("foo"); StopWatch stopwatch = new StopWatch(); stopwatch.start(); for (int i = 0; i < 120000; i++) { if (i % 10000 == 0) { stopwatch.stop(); logger.warn("Sent " + i + " in " + stopwatch.getTotalTimeSeconds() + " (10k in " + stopwatch.getLastTaskTimeMillis() + "ms)"); stopwatch.start(); } handler.handleMessage(message); } stopwatch.stop(); logger.warn("Sent " + 120000 + " in " + stopwatch.getTotalTimeSeconds() + " (10k in " + stopwatch.getLastTaskTimeMillis() + "ms)"); Collection<?> result = resultFuture.get(10, TimeUnit.SECONDS); assertNotNull(result); assertEquals(60000, result.size()); }
From source file:org.springframework.integration.core.MessageIdGenerationTests.java
@Test @Ignore/*w w w .j a v a2 s . c o m*/ public void performanceTest() { int times = 1000000; StopWatch watch = new StopWatch(); watch.start(); for (int i = 0; i < times; i++) { new GenericMessage<Integer>(0); } watch.stop(); double defaultGeneratorElapsedTime = watch.getTotalTimeSeconds(); Field idGeneratorField = ReflectionUtils.findField(MessageHeaders.class, "idGenerator"); ReflectionUtils.makeAccessible(idGeneratorField); ReflectionUtils.setField(idGeneratorField, null, (IdGenerator) () -> TimeBasedUUIDGenerator.generateId()); watch = new StopWatch(); watch.start(); for (int i = 0; i < times; i++) { new GenericMessage<Integer>(0); } watch.stop(); double timebasedGeneratorElapsedTime = watch.getTotalTimeSeconds(); logger.info("Generated " + times + " messages using default UUID generator " + "in " + defaultGeneratorElapsedTime + " seconds"); logger.info("Generated " + times + " messages using Timebased UUID generator " + "in " + timebasedGeneratorElapsedTime + " seconds"); logger.info("Time-based ID generator is " + defaultGeneratorElapsedTime / timebasedGeneratorElapsedTime + " times faster"); }
From source file:org.springframework.integration.history.MessageHistoryIntegrationTests.java
@Test @Ignore/*from w w w . j a v a2s . c om*/ public void testMessageHistoryWithHistoryPerformance() { ConfigurableApplicationContext acWithHistory = new ClassPathXmlApplicationContext( "perfWithMessageHistory.xml", MessageHistoryIntegrationTests.class); ConfigurableApplicationContext acWithoutHistory = new ClassPathXmlApplicationContext( "perfWithoutMessageHistory.xml", MessageHistoryIntegrationTests.class); SampleGateway gatewayHistory = acWithHistory.getBean("sampleGateway", SampleGateway.class); DirectChannel endOfThePipeChannelHistory = acWithHistory.getBean("endOfThePipeChannel", DirectChannel.class); endOfThePipeChannelHistory.subscribe(message -> { MessageChannel replyChannel = (MessageChannel) message.getHeaders().getReplyChannel(); replyChannel.send(message); }); SampleGateway gateway = acWithoutHistory.getBean("sampleGateway", SampleGateway.class); DirectChannel endOfThePipeChannel = acWithoutHistory.getBean("endOfThePipeChannel", DirectChannel.class); endOfThePipeChannel.subscribe(message -> { MessageChannel replyChannel = (MessageChannel) message.getHeaders().getReplyChannel(); replyChannel.send(message); }); StopWatch stopWatch = new StopWatch(); stopWatch.start(); for (int i = 0; i < 10000; i++) { gatewayHistory.echo("hello"); } stopWatch.stop(); logger.info("Elapsed time with history 10000 calls: " + stopWatch.getTotalTimeSeconds()); stopWatch = new StopWatch(); stopWatch.start(); for (int i = 0; i < 10000; i++) { gateway.echo("hello"); } stopWatch.stop(); logger.info("Elapsed time without history 10000 calls: " + stopWatch.getTotalTimeSeconds()); acWithHistory.close(); acWithoutHistory.close(); }
From source file:org.springframework.integration.jdbc.lock.JdbcLockRegistryDifferentClientTests.java
@Test public void testExclusiveAccess() throws Exception { DefaultLockRepository client1 = new DefaultLockRepository(dataSource); client1.afterPropertiesSet();/*from www . j av a 2 s . com*/ final DefaultLockRepository client2 = new DefaultLockRepository(dataSource); client2.afterPropertiesSet(); Lock lock1 = new JdbcLockRegistry(client1).obtain("foo"); final BlockingQueue<Integer> data = new LinkedBlockingQueue<Integer>(); final CountDownLatch latch1 = new CountDownLatch(1); lock1.lockInterruptibly(); Executors.newSingleThreadExecutor().execute(() -> { Lock lock2 = new JdbcLockRegistry(client2).obtain("foo"); try { latch1.countDown(); StopWatch stopWatch = new StopWatch(); stopWatch.start(); lock2.lockInterruptibly(); stopWatch.stop(); data.add(4); Thread.sleep(10); data.add(5); Thread.sleep(10); data.add(6); } catch (InterruptedException e) { Thread.currentThread().interrupt(); } finally { lock2.unlock(); } }); assertTrue(latch1.await(10, TimeUnit.SECONDS)); data.add(1); Thread.sleep(1000); data.add(2); Thread.sleep(1000); data.add(3); lock1.unlock(); for (int i = 0; i < 6; i++) { Integer integer = data.poll(10, TimeUnit.SECONDS); assertNotNull(integer); assertEquals(i + 1, integer.intValue()); } }