@ -1,5 +1,5 @@
/ *
/ *
* Copyright 2002 - 2016 the original author or authors .
* Copyright 2002 - 2017 the original author or authors .
*
*
* Licensed under the Apache License , Version 2 . 0 ( the "License" ) ;
* Licensed under the Apache License , Version 2 . 0 ( the "License" ) ;
* you may not use this file except in compliance with the License .
* you may not use this file except in compliance with the License .
@ -26,8 +26,9 @@ import static org.junit.Assert.*;
/ * *
/ * *
* Checks the speed of compiled SpEL expressions .
* Checks the speed of compiled SpEL expressions .
* By default these tests are marked Ignore since they can fail on a busy machine because they
*
* compare relative performance of interpreted vs compiled .
* < p > By default these tests are marked @Ignore since they can fail on a busy machine
* because they compare relative performance of interpreted vs compiled .
*
*
* @author Andy Clement
* @author Andy Clement
* @since 4 . 1
* @since 4 . 1
@ -35,37 +36,14 @@ import static org.junit.Assert.*;
@Ignore
@Ignore
public class SpelCompilationPerformanceTests extends AbstractExpressionTests {
public class SpelCompilationPerformanceTests extends AbstractExpressionTests {
int count = 50000 ; // Number of evaluations that are timed in one run
int count = 50000 ; // number of evaluations that are timed in one run
int iterations = 10 ; // Number of times to repeat 'count' evaluations (for averaging)
private final static boolean noisyTests = true ;
Expression expression ;
int iterations = 10 ; // number of times to repeat 'count' evaluations (for averaging)
public static class Payload {
Two [ ] DR = new Two [ ] { new Two ( ) } ;
public Two [ ] getDR ( ) {
return DR ;
}
}
public static class Two {
private final static boolean noisyTests = true ;
Three DRFixedSection = new Three ( ) ;
public Three getDRFixedSection ( ) {
return DRFixedSection ;
}
}
public static class Three {
Expression expression ;
double duration = 0 . 4d ;
public double getDuration ( ) {
return duration ;
}
}
public static class NumberHolder {
public int payload = 36 ;
}
/ * *
/ * *
* This test verifies the new support for compiling mathematical expressions with
* This test verifies the new support for compiling mathematical expressions with
@ -241,7 +219,6 @@ public class SpelCompilationPerformanceTests extends AbstractExpressionTests {
System . out . println ( "One million iterations: " + ( System . currentTimeMillis ( ) - stime ) + "ms" ) ;
System . out . println ( "One million iterations: " + ( System . currentTimeMillis ( ) - stime ) + "ms" ) ;
}
}
@Test
@Test
public void stringConcatenation ( ) throws Exception {
public void stringConcatenation ( ) throws Exception {
expression = parser . parseExpression ( "'hello' + getWorld() + ' spring'" ) ;
expression = parser . parseExpression ( "'hello' + getWorld() + ' spring'" ) ;
@ -287,12 +264,6 @@ public class SpelCompilationPerformanceTests extends AbstractExpressionTests {
System . out . println ( "One million iterations: " + ( System . currentTimeMillis ( ) - stime ) + "ms" ) ;
System . out . println ( "One million iterations: " + ( System . currentTimeMillis ( ) - stime ) + "ms" ) ;
}
}
public static class Greeter {
public String getWorld ( ) {
return "world" ;
}
}
@Test
@Test
public void complexExpressionPerformance ( ) throws Exception {
public void complexExpressionPerformance ( ) throws Exception {
Payload payload = new Payload ( ) ;
Payload payload = new Payload ( ) ;
@ -313,8 +284,8 @@ public class SpelCompilationPerformanceTests extends AbstractExpressionTests {
}
}
long etime = System . currentTimeMillis ( ) ;
long etime = System . currentTimeMillis ( ) ;
long interpretedSpeed = ( etime - stime ) ;
long interpretedSpeed = ( etime - stime ) ;
iTotal + = interpretedSpeed ;
iTotal + = interpretedSpeed ;
log ( interpretedSpeed + "ms " ) ;
log ( interpretedSpeed + "ms " ) ;
}
}
logln ( ) ;
logln ( ) ;
@ -329,8 +300,8 @@ public class SpelCompilationPerformanceTests extends AbstractExpressionTests {
}
}
long etime = System . currentTimeMillis ( ) ;
long etime = System . currentTimeMillis ( ) ;
long compiledSpeed = ( etime - stime ) ;
long compiledSpeed = ( etime - stime ) ;
cTotal + = compiledSpeed ;
cTotal + = compiledSpeed ;
log ( compiledSpeed + "ms " ) ;
log ( compiledSpeed + "ms " ) ;
}
}
logln ( ) ;
logln ( ) ;
@ -376,8 +347,8 @@ public class SpelCompilationPerformanceTests extends AbstractExpressionTests {
}
}
etime = System . currentTimeMillis ( ) ;
etime = System . currentTimeMillis ( ) ;
long interpretedSpeed = ( etime - stime ) ;
long interpretedSpeed = ( etime - stime ) ;
interpretedTotal + = interpretedSpeed ;
interpretedTotal + = interpretedSpeed ;
log ( interpretedSpeed + "ms " ) ;
log ( interpretedSpeed + "ms " ) ;
}
}
logln ( ) ;
logln ( ) ;
@ -392,8 +363,8 @@ public class SpelCompilationPerformanceTests extends AbstractExpressionTests {
}
}
etime = System . currentTimeMillis ( ) ;
etime = System . currentTimeMillis ( ) ;
long compiledSpeed = ( etime - stime ) ;
long compiledSpeed = ( etime - stime ) ;
compiledTotal + = compiledSpeed ;
compiledTotal + = compiledSpeed ;
log ( compiledSpeed + "ms " ) ;
log ( compiledSpeed + "ms " ) ;
}
}
logln ( ) ;
logln ( ) ;
@ -407,28 +378,6 @@ public class SpelCompilationPerformanceTests extends AbstractExpressionTests {
public static class TestClass2 {
public String name = "Santa" ;
private String name2 = "foobar" ;
public String getName2 ( ) {
return name2 ;
}
public Foo foo = new Foo ( ) ;
public static class Foo {
public Bar bar = new Bar ( ) ;
Bar b = new Bar ( ) ;
public Bar getBaz ( ) {
return b ;
}
public Bar bay ( ) {
return b ;
}
}
public static class Bar {
public String boo = "oranges" ;
}
}
@Test
@Test
public void compilingPropertyReferenceField ( ) throws Exception {
public void compilingPropertyReferenceField ( ) throws Exception {
long interpretedTotal = 0 , compiledTotal = 0 , stime , etime ;
long interpretedTotal = 0 , compiledTotal = 0 , stime , etime ;
@ -450,8 +399,8 @@ public class SpelCompilationPerformanceTests extends AbstractExpressionTests {
}
}
etime = System . currentTimeMillis ( ) ;
etime = System . currentTimeMillis ( ) ;
long interpretedSpeed = ( etime - stime ) ;
long interpretedSpeed = ( etime - stime ) ;
interpretedTotal + = interpretedSpeed ;
interpretedTotal + = interpretedSpeed ;
log ( interpretedSpeed + "ms " ) ;
log ( interpretedSpeed + "ms " ) ;
}
}
logln ( ) ;
logln ( ) ;
@ -466,8 +415,8 @@ public class SpelCompilationPerformanceTests extends AbstractExpressionTests {
}
}
etime = System . currentTimeMillis ( ) ;
etime = System . currentTimeMillis ( ) ;
long compiledSpeed = ( etime - stime ) ;
long compiledSpeed = ( etime - stime ) ;
compiledTotal + = compiledSpeed ;
compiledTotal + = compiledSpeed ;
log ( compiledSpeed + "ms " ) ;
log ( compiledSpeed + "ms " ) ;
}
}
logln ( ) ;
logln ( ) ;
@ -481,7 +430,6 @@ public class SpelCompilationPerformanceTests extends AbstractExpressionTests {
String interpretedResult = null , compiledResult = null ;
String interpretedResult = null , compiledResult = null ;
TestClass2 testdata = new TestClass2 ( ) ;
TestClass2 testdata = new TestClass2 ( ) ;
Expression expression = parser . parseExpression ( "foo.bar.boo" ) ;
Expression expression = parser . parseExpression ( "foo.bar.boo" ) ;
// warmup
// warmup
@ -497,8 +445,8 @@ public class SpelCompilationPerformanceTests extends AbstractExpressionTests {
}
}
etime = System . currentTimeMillis ( ) ;
etime = System . currentTimeMillis ( ) ;
long interpretedSpeed = ( etime - stime ) ;
long interpretedSpeed = ( etime - stime ) ;
interpretedTotal + = interpretedSpeed ;
interpretedTotal + = interpretedSpeed ;
log ( interpretedSpeed + "ms " ) ;
log ( interpretedSpeed + "ms " ) ;
}
}
logln ( ) ;
logln ( ) ;
@ -513,8 +461,8 @@ public class SpelCompilationPerformanceTests extends AbstractExpressionTests {
}
}
etime = System . currentTimeMillis ( ) ;
etime = System . currentTimeMillis ( ) ;
long compiledSpeed = ( etime - stime ) ;
long compiledSpeed = ( etime - stime ) ;
compiledTotal + = compiledSpeed ;
compiledTotal + = compiledSpeed ;
log ( compiledSpeed + "ms " ) ;
log ( compiledSpeed + "ms " ) ;
}
}
logln ( ) ;
logln ( ) ;
@ -542,8 +490,8 @@ public class SpelCompilationPerformanceTests extends AbstractExpressionTests {
}
}
etime = System . currentTimeMillis ( ) ;
etime = System . currentTimeMillis ( ) ;
long interpretedSpeed = ( etime - stime ) ;
long interpretedSpeed = ( etime - stime ) ;
interpretedTotal + = interpretedSpeed ;
interpretedTotal + = interpretedSpeed ;
log ( interpretedSpeed + "ms " ) ;
log ( interpretedSpeed + "ms " ) ;
}
}
logln ( ) ;
logln ( ) ;
@ -558,8 +506,8 @@ public class SpelCompilationPerformanceTests extends AbstractExpressionTests {
}
}
etime = System . currentTimeMillis ( ) ;
etime = System . currentTimeMillis ( ) ;
long compiledSpeed = ( etime - stime ) ;
long compiledSpeed = ( etime - stime ) ;
compiledTotal + = compiledSpeed ;
compiledTotal + = compiledSpeed ;
log ( compiledSpeed + "ms " ) ;
log ( compiledSpeed + "ms " ) ;
}
}
logln ( ) ;
logln ( ) ;
@ -635,8 +583,8 @@ public class SpelCompilationPerformanceTests extends AbstractExpressionTests {
}
}
etime = System . currentTimeMillis ( ) ;
etime = System . currentTimeMillis ( ) ;
long interpretedSpeed = ( etime - stime ) ;
long interpretedSpeed = ( etime - stime ) ;
interpretedTotal + = interpretedSpeed ;
interpretedTotal + = interpretedSpeed ;
log ( interpretedSpeed + "ms " ) ;
log ( interpretedSpeed + "ms " ) ;
}
}
logln ( ) ;
logln ( ) ;
@ -652,8 +600,8 @@ public class SpelCompilationPerformanceTests extends AbstractExpressionTests {
}
}
etime = System . currentTimeMillis ( ) ;
etime = System . currentTimeMillis ( ) ;
long compiledSpeed = ( etime - stime ) ;
long compiledSpeed = ( etime - stime ) ;
compiledTotal + = compiledSpeed ;
compiledTotal + = compiledSpeed ;
log ( compiledSpeed + "ms " ) ;
log ( compiledSpeed + "ms " ) ;
}
}
logln ( ) ;
logln ( ) ;
@ -661,21 +609,22 @@ public class SpelCompilationPerformanceTests extends AbstractExpressionTests {
assertEquals ( interpretedResult , compiledResult ) ;
assertEquals ( interpretedResult , compiledResult ) ;
reportPerformance ( "property reference (getter)" , interpretedTotal , compiledTotal ) ;
reportPerformance ( "property reference (getter)" , interpretedTotal , compiledTotal ) ;
if ( compiledTotal > = interpretedTotal ) {
if ( compiledTotal > = interpretedTotal ) {
fail ( "Compiled version is slower than interpreted!" ) ;
fail ( "Compiled version is slower than interpreted!" ) ;
}
}
}
}
// ---
private void reportPerformance ( String title , long interpretedTotal , long compiledTotal ) {
private void reportPerformance ( String title , long interpretedTotal , long compiledTotal ) {
double averageInterpreted = interpretedTotal / ( iterations ) ;
double averageInterpreted = interpretedTotal / iterations ;
double averageCompiled = compiledTotal / ( iterations ) ;
double averageCompiled = compiledTotal / iterations ;
double ratio = ( averageCompiled / averageInterpreted ) * 100 . 0d ;
double ratio = ( averageCompiled / averageInterpreted ) * 100 . 0d ;
logln ( ">>" + title + ": average for " + count + ": compiled=" + averageCompiled + "ms interpreted=" + averageInterpreted + "ms: compiled takes " + ( ( int ) ratio ) + "% of the interpreted time" ) ;
logln ( ">>" + title + ": average for " + count + ": compiled=" + averageCompiled +
if ( averageCompiled > averageInterpreted ) {
"ms interpreted=" + averageInterpreted + "ms: compiled takes " +
fail ( "Compiled version took longer than interpreted! CompiledSpeed=~" + averageCompiled +
( ( int ) ratio ) + "% of the interpreted time" ) ;
"ms InterpretedSpeed=" + averageInterpreted + "ms" ) ;
if ( averageCompiled > averageInterpreted ) {
fail ( "Compiled version took longer than interpreted! CompiledSpeed=~" + averageCompiled +
"ms InterpretedSpeed=" + averageInterpreted + "ms" ) ;
}
}
logln ( ) ;
logln ( ) ;
}
}
@ -688,7 +637,7 @@ public class SpelCompilationPerformanceTests extends AbstractExpressionTests {
private void logln ( String . . . message ) {
private void logln ( String . . . message ) {
if ( noisyTests ) {
if ( noisyTests ) {
if ( message ! = null & & message . length > 0 ) {
if ( message . length > 0 ) {
System . out . println ( message [ 0 ] ) ;
System . out . println ( message [ 0 ] ) ;
}
}
else {
else {
@ -700,4 +649,84 @@ public class SpelCompilationPerformanceTests extends AbstractExpressionTests {
private void compile ( Expression expression ) {
private void compile ( Expression expression ) {
assertTrue ( SpelCompiler . compile ( expression ) ) ;
assertTrue ( SpelCompiler . compile ( expression ) ) ;
}
}
public static class Payload {
Two [ ] DR = new Two [ ] { new Two ( ) } ;
public Two [ ] getDR ( ) {
return DR ;
}
}
public static class Two {
Three DRFixedSection = new Three ( ) ;
public Three getDRFixedSection ( ) {
return DRFixedSection ;
}
}
public static class Three {
double duration = 0 . 4d ;
public double getDuration ( ) {
return duration ;
}
}
public static class NumberHolder {
public int payload = 36 ;
}
public static class Greeter {
public String getWorld ( ) {
return "world" ;
}
}
public static class TestClass2 {
public String name = "Santa" ;
private String name2 = "foobar" ;
public String getName2 ( ) {
return name2 ;
}
public Foo foo = new Foo ( ) ;
}
public static class Foo {
public Bar bar = new Bar ( ) ;
Bar b = new Bar ( ) ;
public Bar getBaz ( ) {
return b ;
}
public Bar bay ( ) {
return b ;
}
}
public static class Bar {
public String boo = "oranges" ;
}
}
}