Skip to content

Instantly share code, notes, and snippets.

@artembilan
Created October 20, 2013 17:16
Show Gist options
  • Save artembilan/7072431 to your computer and use it in GitHub Desktop.
Save artembilan/7072431 to your computer and use it in GitHub Desktop.
GroovyScriptMessageProcessor Vs Jsr223ScriptMessageProcessor
/*
* Copyright 2013 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.integration.groovy;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import org.springframework.integration.Message;
import org.springframework.integration.handler.MessageProcessor;
import org.springframework.integration.message.GenericMessage;
import org.springframework.integration.scripting.jsr223.DefaultScriptExecutor;
import org.springframework.integration.scripting.jsr223.ScriptExecutingMessageProcessor;
import org.springframework.scripting.support.StaticScriptSource;
import org.springframework.util.StopWatch;
/**
* @author Artem Bilan
* @since 3.0
*/
public class GroovyScriptEnginesPerformanceTests {
private static final MessageProcessor[] MESSAGE_PROCESSORS = new MessageProcessor[] {
new ScriptExecutingMessageProcessor(new StaticScriptSource("payload", "TestClass2"), new DefaultScriptExecutor("Groovy")),
new GroovyScriptExecutingMessageProcessor(new StaticScriptSource("payload", "TestClass1"))
};
public static void main(String[] args) {
runTests(1, "Warm-up", false);
runTests(100000, "100K", false);
runTests(100000, "100K Concurrent", true);
}
private static void runTests(int count, String description, boolean concurrent) {
StopWatch watch = new StopWatch(description);
for (MessageProcessor messageProcessor : MESSAGE_PROCESSORS) {
runProcessor(messageProcessor, watch, count, concurrent);
}
System.out.println(watch.prettyPrint());
}
private static void runProcessor(final MessageProcessor messageProcessor, StopWatch watch, int count, boolean concurrent) {
ExecutorService executorService = Executors.newFixedThreadPool(100);
final Message<?> testMessage = new GenericMessage<Object>("test");
Runnable task = new Runnable() {
@Override
public void run() {
messageProcessor.processMessage(testMessage);
}
};
watch.start(messageProcessor.getClass().getSimpleName());
for (int i = 0; i < count; i++) {
if(concurrent) {
executorService.execute(task);
}
else {
task.run();
}
}
if (concurrent) {
executorService.shutdown();
try {
executorService.awaitTermination(10, TimeUnit.SECONDS);
}
catch (InterruptedException e) {
e.printStackTrace();
}
}
watch.stop();
}
}
------------------
StopWatch 'Warm-up': running time (millis) = 342
-----------------------------------------
ms % Task name
-----------------------------------------
00332 097% ScriptExecutingMessageProcessor
00010 003% GroovyScriptExecutingMessageProcessor
StopWatch '100K': running time (millis) = 1846
-----------------------------------------
ms % Task name
-----------------------------------------
01803 098% ScriptExecutingMessageProcessor
00043 002% GroovyScriptExecutingMessageProcessor
StopWatch '100K Concurrent': running time (millis) = 792
-----------------------------------------
ms % Task name
-----------------------------------------
00725 092% ScriptExecutingMessageProcessor
00067 008% GroovyScriptExecutingMessageProcessor
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment