|
| 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.simple; |
| 8 | + |
| 9 | +import static java.lang.annotation.ElementType.TYPE; |
| 10 | +import static java.lang.annotation.RetentionPolicy.RUNTIME; |
| 11 | + |
| 12 | +import java.lang.annotation.Documented; |
| 13 | +import java.lang.annotation.Retention; |
| 14 | +import java.lang.annotation.Target; |
| 15 | +import java.util.Set; |
| 16 | +import java.util.concurrent.TimeUnit; |
| 17 | + |
| 18 | +import jakarta.validation.Constraint; |
| 19 | +import jakarta.validation.ConstraintValidator; |
| 20 | +import jakarta.validation.ConstraintValidatorContext; |
| 21 | +import jakarta.validation.ConstraintViolation; |
| 22 | +import jakarta.validation.Payload; |
| 23 | +import jakarta.validation.Validation; |
| 24 | +import jakarta.validation.Validator; |
| 25 | +import jakarta.validation.ValidatorFactory; |
| 26 | +import jakarta.validation.constraints.NotNull; |
| 27 | +import jakarta.validation.constraints.PositiveOrZero; |
| 28 | + |
| 29 | +import org.openjdk.jmh.annotations.Benchmark; |
| 30 | +import org.openjdk.jmh.annotations.BenchmarkMode; |
| 31 | +import org.openjdk.jmh.annotations.Fork; |
| 32 | +import org.openjdk.jmh.annotations.Measurement; |
| 33 | +import org.openjdk.jmh.annotations.Mode; |
| 34 | +import org.openjdk.jmh.annotations.OutputTimeUnit; |
| 35 | +import org.openjdk.jmh.annotations.Scope; |
| 36 | +import org.openjdk.jmh.annotations.State; |
| 37 | +import org.openjdk.jmh.annotations.Threads; |
| 38 | +import org.openjdk.jmh.annotations.Warmup; |
| 39 | +import org.openjdk.jmh.infra.Blackhole; |
| 40 | + |
| 41 | +/** |
| 42 | + * @author Marko Bekhta |
| 43 | + */ |
| 44 | +public class SimpleClassPropertyValidation { |
| 45 | + |
| 46 | + @State(Scope.Benchmark) |
| 47 | + public static class ValidationState { |
| 48 | + public volatile Validator validator; |
| 49 | + public volatile Driver driver; |
| 50 | + |
| 51 | + { |
| 52 | + ValidatorFactory factory = Validation.buildDefaultValidatorFactory(); |
| 53 | + validator = factory.getValidator(); |
| 54 | + |
| 55 | + driver = new Driver( null, 1000, 100.0 ); |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + @Benchmark |
| 60 | + @BenchmarkMode(Mode.Throughput) |
| 61 | + @OutputTimeUnit(TimeUnit.MILLISECONDS) |
| 62 | + @Fork(value = 1) |
| 63 | + @Threads(50) |
| 64 | + @Warmup(iterations = 10) |
| 65 | + @Measurement(iterations = 20) |
| 66 | + public void testSimpleBeanValidation(ValidationState state, Blackhole bh) { |
| 67 | + Set<ConstraintViolation<Driver>> violations = state.validator.validate( state.driver ); |
| 68 | + bh.consume( violations ); |
| 69 | + } |
| 70 | + |
| 71 | + @SpeedingDriver |
| 72 | + public static class Driver { |
| 73 | + @NotNull |
| 74 | + private final String name; |
| 75 | + |
| 76 | + @PositiveOrZero |
| 77 | + private final long totalDistance; |
| 78 | + |
| 79 | + @PositiveOrZero |
| 80 | + private final double timeDriven; |
| 81 | + |
| 82 | + public Driver(String name, long totalDistance, double timeDriven) { |
| 83 | + this.name = name; |
| 84 | + this.totalDistance = totalDistance; |
| 85 | + this.timeDriven = timeDriven; |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + @Target({ TYPE }) |
| 90 | + @Retention(RUNTIME) |
| 91 | + @Documented |
| 92 | + @Constraint(validatedBy = { SpeedingDriverValidator.class }) |
| 93 | + @interface SpeedingDriver { |
| 94 | + String message() default "message"; |
| 95 | + |
| 96 | + Class<?>[] groups() default { }; |
| 97 | + |
| 98 | + Class<? extends Payload>[] payload() default { }; |
| 99 | + } |
| 100 | + |
| 101 | + public static class SpeedingDriverValidator implements ConstraintValidator<SpeedingDriver, Driver> { |
| 102 | + |
| 103 | + @Override |
| 104 | + public boolean isValid(Driver driver, ConstraintValidatorContext context) { |
| 105 | + return driver.totalDistance / driver.timeDriven > 60.0; |
| 106 | + } |
| 107 | + } |
| 108 | +} |
0 commit comments