|
| 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.List; |
| 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.NotEmpty; |
| 22 | +import javax.validation.constraints.NotNull; |
| 23 | + |
| 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 Guillaume Smet |
| 38 | + */ |
| 39 | +public class CascadedWithLotsOfItemsAndMoreConstraintsValidation { |
| 40 | + |
| 41 | + private static final int NUMBER_OF_ARTICLES_PER_SHOP = 2000; |
| 42 | + |
| 43 | + @State(Scope.Benchmark) |
| 44 | + public static class CascadedWithLotsOfItemsValidationState { |
| 45 | + public volatile Validator validator; |
| 46 | + |
| 47 | + public volatile Shop shop; |
| 48 | + |
| 49 | + public CascadedWithLotsOfItemsValidationState() { |
| 50 | + ValidatorFactory factory = Validation.buildDefaultValidatorFactory(); |
| 51 | + validator = factory.getValidator(); |
| 52 | + |
| 53 | + shop = createShop(); |
| 54 | + } |
| 55 | + |
| 56 | + private Shop createShop() { |
| 57 | + Shop shop = new Shop( 1, "Shop" ); |
| 58 | + |
| 59 | + for ( int i = 0; i < NUMBER_OF_ARTICLES_PER_SHOP; i++ ) { |
| 60 | + shop.addArticle( new Article( i, "Article " + i ) ); |
| 61 | + } |
| 62 | + |
| 63 | + return shop; |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + @Benchmark |
| 68 | + @BenchmarkMode(Mode.Throughput) |
| 69 | + @OutputTimeUnit(TimeUnit.SECONDS) |
| 70 | + @Fork(value = 1) |
| 71 | + @Threads(20) |
| 72 | + @Warmup(iterations = 10) |
| 73 | + @Measurement(iterations = 20) |
| 74 | + public void testCascadedValidationWithLotsOfItems(CascadedWithLotsOfItemsValidationState state, Blackhole bh) { |
| 75 | + Set<ConstraintViolation<Shop>> violations = state.validator.validate( state.shop ); |
| 76 | + assertThat( violations ).hasSize( 0 ); |
| 77 | + |
| 78 | + bh.consume( violations ); |
| 79 | + } |
| 80 | + |
| 81 | + public static class Shop { |
| 82 | + @NotNull |
| 83 | + private Integer id; |
| 84 | + |
| 85 | + @NotEmpty |
| 86 | + private String name; |
| 87 | + |
| 88 | + @NotNull |
| 89 | + @Valid |
| 90 | + private List<Article> articles = new ArrayList<>(); |
| 91 | + |
| 92 | + public Shop(Integer id, String name) { |
| 93 | + this.id = id; |
| 94 | + this.name = name; |
| 95 | + } |
| 96 | + |
| 97 | + public void addArticle(Article article) { |
| 98 | + articles.add( article ); |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + public static class Article { |
| 103 | + @NotNull |
| 104 | + private Integer id; |
| 105 | + |
| 106 | + @NotEmpty |
| 107 | + private String name; |
| 108 | + |
| 109 | + public Article(Integer id, String name) { |
| 110 | + this.id = id; |
| 111 | + this.name = name; |
| 112 | + } |
| 113 | + } |
| 114 | +} |
0 commit comments