Skip to content

Commit 0968f38

Browse files
gsmetmarko-bekhta
authored andcommitted
HV-1831 Specific benchmark infrastructure for predefined scope
1 parent 833ef7f commit 0968f38

File tree

9 files changed

+411
-75
lines changed

9 files changed

+411
-75
lines changed

performance/README.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,9 @@ To allow performance testing of different Hibernate Validator versions there are
1111
Choosing a profile executes the tests against the specified Hibernate Validator or BVal version, respectively. The
1212
defined profiles are:
1313

14-
* hv-current (Hibernate Validator 6.1.0-SNAPSHOT)
15-
* hv-6.0 (Hibernate Validator 6.0.15.Final)
14+
* hv-current (Hibernate Validator 6.2.1-SNAPSHOT)
15+
* hv-6.1 (Hibernate Validator 6.1.7.Final)
16+
* hv-6.0 (Hibernate Validator 6.0.22.Final)
1617
* hv-5.4 (Hibernate Validator 5.4.3.Final)
1718
* hv-5.3 (Hibernate Validator 5.3.4.Final)
1819
* hv-5.2 (Hibernate Validator 5.2.4.Final)
@@ -64,7 +65,7 @@ If you want to run one of those profilers - pass it as parameter when running a
6465

6566
To run a specific benchmark:
6667

67-
java -jar target/hibernate-validator-performance.jar CascadedValidation
68+
java -jar target/hibernate-validator-performance-hv-current.jar CascadedValidation
6869

6970
#### Creating reports for all major Hibernate Validator versions
7071

performance/pom.xml

Lines changed: 64 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -192,12 +192,27 @@
192192
<artifactId>log4j-core</artifactId>
193193
</dependency>
194194
</dependencies>
195-
<!-- adding sources for BV 2.0 tests -->
195+
<!-- adding sources for BV 2.0 and Predefined Scope tests -->
196196
<build>
197197
<plugins>
198198
<plugin>
199199
<groupId>org.codehaus.mojo</groupId>
200200
<artifactId>build-helper-maven-plugin</artifactId>
201+
<executions>
202+
<execution>
203+
<id>add-source</id>
204+
<phase>generate-sources</phase>
205+
<goals>
206+
<goal>add-source</goal>
207+
</goals>
208+
<configuration>
209+
<sources>
210+
<source>${project.basedir}/src/main/java-bv2</source>
211+
<source>${project.basedir}/src/main/java-predefined-scope</source>
212+
</sources>
213+
</configuration>
214+
</execution>
215+
</executions>
201216
</plugin>
202217
</plugins>
203218
</build>
@@ -236,12 +251,27 @@
236251
<artifactId>log4j-core</artifactId>
237252
</dependency>
238253
</dependencies>
239-
<!-- adding sources for BV 2.0 tests -->
254+
<!-- adding sources for BV 2.0 Predefined Scope tests -->
240255
<build>
241256
<plugins>
242257
<plugin>
243258
<groupId>org.codehaus.mojo</groupId>
244259
<artifactId>build-helper-maven-plugin</artifactId>
260+
<executions>
261+
<execution>
262+
<id>add-source</id>
263+
<phase>generate-sources</phase>
264+
<goals>
265+
<goal>add-source</goal>
266+
</goals>
267+
<configuration>
268+
<sources>
269+
<source>${project.basedir}/src/main/java-bv2</source>
270+
<source>${project.basedir}/src/main/java-predefined-scope</source>
271+
</sources>
272+
</configuration>
273+
</execution>
274+
</executions>
245275
</plugin>
246276
</plugins>
247277
</build>
@@ -257,7 +287,7 @@
257287
<properties>
258288
<validation-api.version>2.0.1.Final</validation-api.version>
259289
<beanvalidation-impl.name>Hibernate Validator</beanvalidation-impl.name>
260-
<beanvalidation-impl.version>6.1.2.Final</beanvalidation-impl.version>
290+
<beanvalidation-impl.version>6.1.7.Final</beanvalidation-impl.version>
261291
</properties>
262292
<dependencies>
263293
<dependency>
@@ -280,12 +310,27 @@
280310
<artifactId>log4j-core</artifactId>
281311
</dependency>
282312
</dependencies>
283-
<!-- adding sources for BV 2.0 tests -->
313+
<!-- adding sources for BV 2.0 and Predefined Scope tests -->
284314
<build>
285315
<plugins>
286316
<plugin>
287317
<groupId>org.codehaus.mojo</groupId>
288318
<artifactId>build-helper-maven-plugin</artifactId>
319+
<executions>
320+
<execution>
321+
<id>add-source</id>
322+
<phase>generate-sources</phase>
323+
<goals>
324+
<goal>add-source</goal>
325+
</goals>
326+
<configuration>
327+
<sources>
328+
<source>${project.basedir}/src/main/java-bv2</source>
329+
<source>${project.basedir}/src/main/java-predefined-scope</source>
330+
</sources>
331+
</configuration>
332+
</execution>
333+
</executions>
289334
</plugin>
290335
</plugins>
291336
</build>
@@ -301,7 +346,7 @@
301346
<properties>
302347
<validation-api.version>2.0.1.Final</validation-api.version>
303348
<beanvalidation-impl.name>Hibernate Validator</beanvalidation-impl.name>
304-
<beanvalidation-impl.version>6.0.19.Final</beanvalidation-impl.version>
349+
<beanvalidation-impl.version>6.0.22.Final</beanvalidation-impl.version>
305350
</properties>
306351
<dependencies>
307352
<dependency>
@@ -330,6 +375,20 @@
330375
<plugin>
331376
<groupId>org.codehaus.mojo</groupId>
332377
<artifactId>build-helper-maven-plugin</artifactId>
378+
<executions>
379+
<execution>
380+
<id>add-source</id>
381+
<phase>generate-sources</phase>
382+
<goals>
383+
<goal>add-source</goal>
384+
</goals>
385+
<configuration>
386+
<sources>
387+
<source>${project.basedir}/src/main/java-bv2</source>
388+
</sources>
389+
</configuration>
390+
</execution>
391+
</executions>
333392
</plugin>
334393
</plugins>
335394
</build>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
/*
2+
* Hibernate Validator, declare and validate application constraints
3+
*
4+
* License: Apache License, Version 2.0
5+
* See the license.txt file in the root directory or <http://www.apache.org/licenses/LICENSE-2.0>.
6+
*/
7+
package org.hibernate.validator.performance.cascaded;
8+
9+
import static org.assertj.core.api.Assertions.assertThat;
10+
11+
import java.util.Collections;
12+
import java.util.HashSet;
13+
import java.util.Set;
14+
import java.util.concurrent.TimeUnit;
15+
16+
import javax.validation.ConstraintViolation;
17+
import javax.validation.Valid;
18+
import javax.validation.Validation;
19+
import javax.validation.Validator;
20+
import javax.validation.ValidatorFactory;
21+
import javax.validation.constraints.NotNull;
22+
23+
import org.hibernate.validator.PredefinedScopeHibernateValidator;
24+
import org.openjdk.jmh.annotations.Benchmark;
25+
import org.openjdk.jmh.annotations.BenchmarkMode;
26+
import org.openjdk.jmh.annotations.Fork;
27+
import org.openjdk.jmh.annotations.Measurement;
28+
import org.openjdk.jmh.annotations.Mode;
29+
import org.openjdk.jmh.annotations.OutputTimeUnit;
30+
import org.openjdk.jmh.annotations.Scope;
31+
import org.openjdk.jmh.annotations.State;
32+
import org.openjdk.jmh.annotations.Threads;
33+
import org.openjdk.jmh.annotations.Warmup;
34+
import org.openjdk.jmh.infra.Blackhole;
35+
36+
/**
37+
* @author Hardy Ferentschik
38+
* @author Guillaume Smet
39+
*/
40+
public class PredefinedScopeCascadedValidation {
41+
42+
@State(Scope.Benchmark)
43+
public static class PredefinedScopeCascadedValidationState {
44+
45+
public volatile Validator validator;
46+
public volatile Person person;
47+
48+
public PredefinedScopeCascadedValidationState() {
49+
ValidatorFactory factory = Validation.byProvider( PredefinedScopeHibernateValidator.class )
50+
.configure()
51+
.builtinConstraints( Collections.singleton( NotNull.class.getName() ) )
52+
.initializeBeanMetaData( Collections.singleton( Person.class ) )
53+
.buildValidatorFactory();
54+
validator = factory.getValidator();
55+
56+
// TODO graphs needs to be generated and deeper
57+
Person kermit = new Person( "kermit" );
58+
Person piggy = new Person( "miss piggy" );
59+
Person gonzo = new Person( "gonzo" );
60+
61+
kermit.addFriend( piggy ).addFriend( gonzo );
62+
piggy.addFriend( kermit ).addFriend( gonzo );
63+
gonzo.addFriend( kermit ).addFriend( piggy );
64+
65+
person = kermit;
66+
}
67+
}
68+
69+
@Benchmark
70+
@BenchmarkMode(Mode.Throughput)
71+
@OutputTimeUnit(TimeUnit.MILLISECONDS)
72+
@Fork(value = 1)
73+
@Threads(50)
74+
@Warmup(iterations = 10)
75+
@Measurement(iterations = 20)
76+
public void testPredefinedScopeCascadedValidation(PredefinedScopeCascadedValidationState state, Blackhole bh) {
77+
Set<ConstraintViolation<Person>> violations = state.validator.validate( state.person );
78+
assertThat( violations ).hasSize( 0 );
79+
80+
bh.consume( violations );
81+
}
82+
83+
public static class Person {
84+
85+
@NotNull
86+
String name;
87+
88+
@Valid
89+
Set<Person> friends = new HashSet<>();
90+
91+
public Person(String name) {
92+
this.name = name;
93+
}
94+
95+
public Person addFriend(Person friend) {
96+
friends.add( friend );
97+
return this;
98+
}
99+
}
100+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
/*
2+
* Hibernate Validator, declare and validate application constraints
3+
*
4+
* License: Apache License, Version 2.0
5+
* See the license.txt file in the root directory or <http://www.apache.org/licenses/LICENSE-2.0>.
6+
*/
7+
package org.hibernate.validator.performance.cascaded;
8+
9+
import static org.assertj.core.api.Assertions.assertThat;
10+
11+
import java.util.ArrayList;
12+
import java.util.Arrays;
13+
import java.util.HashSet;
14+
import java.util.List;
15+
import java.util.Set;
16+
import java.util.concurrent.TimeUnit;
17+
18+
import javax.validation.ConstraintViolation;
19+
import javax.validation.Valid;
20+
import javax.validation.Validation;
21+
import javax.validation.Validator;
22+
import javax.validation.ValidatorFactory;
23+
import javax.validation.constraints.NotNull;
24+
import javax.validation.constraints.Size;
25+
26+
import org.hibernate.validator.PredefinedScopeHibernateValidator;
27+
import org.openjdk.jmh.annotations.Benchmark;
28+
import org.openjdk.jmh.annotations.BenchmarkMode;
29+
import org.openjdk.jmh.annotations.Fork;
30+
import org.openjdk.jmh.annotations.Measurement;
31+
import org.openjdk.jmh.annotations.Mode;
32+
import org.openjdk.jmh.annotations.OutputTimeUnit;
33+
import org.openjdk.jmh.annotations.Scope;
34+
import org.openjdk.jmh.annotations.State;
35+
import org.openjdk.jmh.annotations.Threads;
36+
import org.openjdk.jmh.annotations.Warmup;
37+
import org.openjdk.jmh.infra.Blackhole;
38+
39+
/**
40+
* @author Guillaume Smet
41+
*/
42+
public class PredefinedScopeCascadedWithLotsOfItemsAndMoreConstraintsValidation {
43+
44+
private static final int NUMBER_OF_ARTICLES_PER_SHOP = 2000;
45+
46+
@State(Scope.Benchmark)
47+
public static class PredefinedScopeCascadedWithLotsOfItemsValidationState {
48+
49+
public volatile Validator validator;
50+
51+
public volatile Shop shop;
52+
53+
public PredefinedScopeCascadedWithLotsOfItemsValidationState() {
54+
ValidatorFactory factory = Validation.byProvider( PredefinedScopeHibernateValidator.class )
55+
.configure()
56+
.builtinConstraints( new HashSet<>( Arrays.asList( NotNull.class.getName(), Size.class.getName() ) ) )
57+
.initializeBeanMetaData( new HashSet<>( Arrays.asList( Shop.class, Article.class ) ) )
58+
.buildValidatorFactory();
59+
validator = factory.getValidator();
60+
61+
shop = createShop();
62+
}
63+
64+
private Shop createShop() {
65+
Shop shop = new Shop( 1, "Shop" );
66+
67+
for ( int i = 0; i < NUMBER_OF_ARTICLES_PER_SHOP; i++ ) {
68+
shop.addArticle( new Article( i, "Article " + i ) );
69+
}
70+
71+
return shop;
72+
}
73+
}
74+
75+
@Benchmark
76+
@BenchmarkMode(Mode.Throughput)
77+
@OutputTimeUnit(TimeUnit.SECONDS)
78+
@Fork(value = 1)
79+
@Threads(20)
80+
@Warmup(iterations = 10)
81+
@Measurement(iterations = 20)
82+
public void testPredefinedScopeCascadedValidationWithLotsOfItems(PredefinedScopeCascadedWithLotsOfItemsValidationState state, Blackhole bh) {
83+
Set<ConstraintViolation<Shop>> violations = state.validator.validate( state.shop );
84+
assertThat( violations ).hasSize( 0 );
85+
86+
bh.consume( violations );
87+
}
88+
89+
public static class Shop {
90+
91+
@NotNull
92+
private Integer id;
93+
94+
@Size(min = 1)
95+
private String name;
96+
97+
@NotNull
98+
@Valid
99+
private List<Article> articles = new ArrayList<>();
100+
101+
public Shop(Integer id, String name) {
102+
this.id = id;
103+
this.name = name;
104+
}
105+
106+
public void addArticle(Article article) {
107+
articles.add( article );
108+
}
109+
}
110+
111+
public static class Article {
112+
113+
@NotNull
114+
private Integer id;
115+
116+
@Size(min = 1)
117+
private String name;
118+
119+
public Article(Integer id, String name) {
120+
this.id = id;
121+
this.name = name;
122+
}
123+
}
124+
}

0 commit comments

Comments
 (0)