diff --git a/README.md b/README.md index 728d210..7cfaebc 100644 --- a/README.md +++ b/README.md @@ -39,6 +39,7 @@ article; each module's own README has that article's version table, quickstart, | [`streams`](streams/) | Java Stream API Tutorial with Examples + Collectors Cookbook (laziness, collectors, primitive and parallel streams, Gatherers) | | [`classloaders`](classloaders/) | Java ClassLoaders Explained: Delegation, Custom Loaders, ClassNotFoundException vs NoClassDefFoundError | | [`process-api`](process-api/) | Running Shell Commands from Java Safely: ProcessBuilder, Process API and Timeouts | +| [`bean-mapping`](bean-mapping/) | Dozer Bean Mapping in Java: Practical Guide, Pitfalls, a Migration Path to MapStruct and a Real Benchmark | ## License diff --git a/bean-mapping/README.md b/bean-mapping/README.md new file mode 100644 index 0000000..8bdf8a3 --- /dev/null +++ b/bean-mapping/README.md @@ -0,0 +1,42 @@ +# bean-mapping + +Companion code for the ankurm.com post *"Dozer Bean Mapping in Java: Practical Guide, Pitfalls, a Migration +Path to MapStruct and a Real Benchmark."* Module `bean-mapping` in `java-core-examples`. + +All explanation lives in the post; this module holds the runnable evidence and the captured output. + +## Versions + +| Component | Version | +|---|---| +| JDK | 25.0.5.1+2 (Temurin, LTS) | +| Dozer (`dozer-core`) | 7.0.0 (Maven Central, 2024-03-05; newest release) | +| MapStruct | 1.6.3 (newest GA; 1.7.0.Beta2 exists but is a beta) | +| JMH | 1.37 | +| JUnit Jupiter | 5.11.0 | + +## Quickstart + +```bash +export JDK25_HOME=/path/to/jdk-25 +./scripts/run-all.sh # rebuilds, runs the tests, regenerates everything in output/ (benchmark: ~4 min) +SKIP_BENCHMARK=1 ./scripts/run-all.sh # everything except the benchmark +./scripts/run-benchmark.sh -f 1 -wi 1 -i 1 # a quick benchmark smoke run; extra args go to JMH +``` + +## What is in here + +| File | Shows | Output | +|---|---|---| +| `DozerTour` | basic, nested, collection, bidirectional, null handling, `mapNull(false)` | `01` | +| `DozerPitfalls` | typo in a field name, silently unmapped fields, records, a global converter that is ignored on lists, lazy getters and `exclude` | `02` | +| `MapStructTour` | the same mappings in MapStruct, `@InheritInverseConfiguration`, update with `IGNORE`, records, lazy getters and `ignore` | `03` | +| `UserMapper` | the MapStruct mapper; its generated implementation is captured in output `04` | `04` | +| `src/broken/*.java` | two mappers that do not compile (an unmapped column; an ambiguous inverse); outside the Maven build, driven by `scripts/compile-broken.sh` | `05` | +| `BeanMappingClaimsTest` | 14 assertions behind the claims in the post, including the Dozer / MapStruct / manual parity test used for migration | `06` | +| `MappingBenchmark` | manual vs MapStruct vs Dozer on a flat and a nested object, plus the Dozer mapper-per-call antipattern | `07` | + +Timings in `07` depend on the machine and on what else it is running; the order of magnitude between libraries +does not. The `@Generated(date=...)` line in `04` changes on every build. The Spring snippets from the post +(`DozerSpringConfig`, `DozerUserService`, `SpringUserMapper`, `MapStructUserService`) are not in this module; +they are downloadable from the post. diff --git a/bean-mapping/output/00-environment.txt b/bean-mapping/output/00-environment.txt new file mode 100644 index 0000000..abf8e09 --- /dev/null +++ b/bean-mapping/output/00-environment.txt @@ -0,0 +1,13 @@ +$ java -version +openjdk version "25.0.5.1" 2026-11-17 LTS +OpenJDK Runtime Environment Temurin-25.0.5.1+2 (build 25.0.5.1+2-LTS) +OpenJDK 64-Bit Server VM Temurin-25.0.5.1+2 (build 25.0.5.1+2-LTS, mixed mode, sharing) + +$ uname -sr +Linux 6.18.44-fc-v1045 +$ nproc +2 +$ grep -m1 "model name" /proc/cpuinfo +model name : Intel(R) Xeon(R) Processor @ 2.10GHz + +Dozer 7.0.0, MapStruct 1.6.3, JMH 1.37 diff --git a/bean-mapping/output/01-dozer-tour.txt b/bean-mapping/output/01-dozer-tour.txt new file mode 100644 index 0000000..57de5e2 --- /dev/null +++ b/bean-mapping/output/01-dozer-tour.txt @@ -0,0 +1,23 @@ +== 1. Basic mapping: id and username by name, email -> emailAddress by configuration +ID: 1 +User: ankurm +Email: admin@ankurm.com + +== 2. Nested object: no configuration at all +profile class: ProfileEntity +first name: John + +== 3. Collection: List -> List through the custom converter +Java +Spring Boot +Dozer + +== 4. Bidirectional: the same configuration maps the entity back +round trip equals original: true +UserDTO[id=1, username=ankurm, email=admin@ankurm.com, profile=ProfileDTO[John Doe], tags=[Java, Spring Boot, Dozer, MapStruct, JMH]] + +== 5. Null handling: default Dozer copies null over an existing value +after mapping a DTO with null email and null profile: emailAddress=null, profile=null + +== 6. Null handling: TypeMappingOptions.mapNull(false) keeps the existing value +after the same update: emailAddress=admin@ankurm.com, profile=ProfileEntity[John Doe] diff --git a/bean-mapping/output/02-dozer-pitfalls.txt b/bean-mapping/output/02-dozer-pitfalls.txt new file mode 100644 index 0000000..bc8c7de --- /dev/null +++ b/bean-mapping/output/02-dozer-pitfalls.txt @@ -0,0 +1,26 @@ +== 1. A typo in a field name: when does it fail? (not at compile time) +failed while building the mapper: MappingException: No read or write method found for field (emial) in class (class com.ankurm.beanmapping.model.UserDTO) + +== 2. A field nobody maps: email -> emailAddress without the configuration line +no exception, no log line. emailAddress=null + +== 3. A new column on the entity (phone): does Dozer notice? +no exception, no log line. UserEntityV2[id=1, username=ankurm, emailAddress=null, phone=null] + +== 4. Records: as destination, then as source +record destination failed: MappingException: java.lang.NoSuchMethodException: com.ankurm.beanmapping.model.UserRecord.() [root cause NoSuchMethodException: com.ankurm.beanmapping.model.UserRecord.()] +record source: no exception, but look at the result: UserDTO[id=0, username=null, email=null, profile=null, tags=[]] + +== 5. A converter registered globally, applied to a list, entity -> DTO +DTO to entity worked: [Tag[Java], Tag[Spring Boot], Tag[Dozer], Tag[MapStruct], Tag[JMH]] +entity to DTO, no exception: tags=[Tag[Java], Tag[Spring Boot], Tag[Dozer], Tag[MapStruct], Tag[JMH]] +convertFrom() was never used: the elements are TagEntity.toString() output +with a field-level converter: tags=[Java, Spring Boot, Dozer, MapStruct, JMH] + +== 6. A lazy getter: does Dozer call it? +map() failed: MappingException: java.lang.reflect.InvocationTargetException [root cause IllegalStateException: could not initialize proxy - no Session (simulated lazy collection)] +getOrders() was called 1 time(s) + +== 7. Same kind of entity, orders excluded in the mapping +map() succeeded: LazyUserView[id=6, name=lazy2, orders=null] +getOrders() was called 0 time(s) diff --git a/bean-mapping/output/03-mapstruct-tour.txt b/bean-mapping/output/03-mapstruct-tour.txt new file mode 100644 index 0000000..2bb27ba --- /dev/null +++ b/bean-mapping/output/03-mapstruct-tour.txt @@ -0,0 +1,18 @@ +== 1. Basic + nested + collection in one mapper +UserEntity[id=1, username=ankurm, emailAddress=admin@ankurm.com, profile=ProfileEntity[John Doe], tags=[Tag[Java], Tag[Spring Boot], Tag[Dozer], Tag[MapStruct], Tag[JMH]]] + +== 2. Bidirectional through @InheritInverseConfiguration +round trip equals original: true + +== 3. Null handling: the plain method copies null, the update method with IGNORE does not +new object from DTO with nulls: emailAddress=null, profile=null +update() of an existing entity: emailAddress=admin@ankurm.com, profile=ProfileEntity[John Doe] + +== 4. Records, both directions +record destination: UserRecord[id=1, username=ankurm] +record source: UserDTO[id=7, username=rec, email=null, profile=null, tags=[]] + +== 5. A lazy getter: MapStruct calls it too, unless you ignore the property +map failed: IllegalStateException: could not initialize proxy - no Session (simulated lazy collection) +without ignore, getOrders() was called 1 time(s) +with ignore: LazyUserView[id=6, name=lazy2, orders=null], getOrders() called 0 time(s) diff --git a/bean-mapping/output/04-UserMapperImpl.java.txt b/bean-mapping/output/04-UserMapperImpl.java.txt new file mode 100644 index 0000000..a992127 --- /dev/null +++ b/bean-mapping/output/04-UserMapperImpl.java.txt @@ -0,0 +1,137 @@ +package com.ankurm.beanmapping.mapstruct; + +import com.ankurm.beanmapping.model.ProfileDTO; +import com.ankurm.beanmapping.model.ProfileEntity; +import com.ankurm.beanmapping.model.TagEntity; +import com.ankurm.beanmapping.model.UserDTO; +import com.ankurm.beanmapping.model.UserEntity; +import java.util.ArrayList; +import java.util.List; +import javax.annotation.processing.Generated; + +@Generated( + value = "org.mapstruct.ap.MappingProcessor", + date = "2026-12-01T08:07:17+0530", + comments = "version: 1.6.3, compiler: javac, environment: Java 25.0.5.1 (Eclipse Adoptium)" +) +public class UserMapperImpl implements UserMapper { + + @Override + public UserEntity toEntity(UserDTO dto) { + if ( dto == null ) { + return null; + } + + UserEntity userEntity = new UserEntity(); + + userEntity.setEmailAddress( dto.getEmail() ); + userEntity.setId( dto.getId() ); + userEntity.setUsername( dto.getUsername() ); + userEntity.setProfile( toEntity( dto.getProfile() ) ); + userEntity.setTags( stringListToTagEntityList( dto.getTags() ) ); + + return userEntity; + } + + @Override + public UserDTO toDto(UserEntity entity) { + if ( entity == null ) { + return null; + } + + UserDTO userDTO = new UserDTO(); + + userDTO.setEmail( entity.getEmailAddress() ); + userDTO.setId( entity.getId() ); + userDTO.setUsername( entity.getUsername() ); + userDTO.setProfile( toDto( entity.getProfile() ) ); + userDTO.setTags( tagEntityListToStringList( entity.getTags() ) ); + + return userDTO; + } + + @Override + public ProfileEntity toEntity(ProfileDTO profile) { + if ( profile == null ) { + return null; + } + + ProfileEntity profileEntity = new ProfileEntity(); + + profileEntity.setFirstName( profile.getFirstName() ); + profileEntity.setLastName( profile.getLastName() ); + + return profileEntity; + } + + @Override + public ProfileDTO toDto(ProfileEntity profile) { + if ( profile == null ) { + return null; + } + + ProfileDTO profileDTO = new ProfileDTO(); + + profileDTO.setFirstName( profile.getFirstName() ); + profileDTO.setLastName( profile.getLastName() ); + + return profileDTO; + } + + @Override + public void update(UserEntity target, UserDTO source) { + if ( source == null ) { + return; + } + + if ( source.getEmail() != null ) { + target.setEmailAddress( source.getEmail() ); + } + target.setId( source.getId() ); + if ( source.getUsername() != null ) { + target.setUsername( source.getUsername() ); + } + if ( source.getProfile() != null ) { + target.setProfile( toEntity( source.getProfile() ) ); + } + if ( target.getTags() != null ) { + List list = stringListToTagEntityList( source.getTags() ); + if ( list != null ) { + target.getTags().clear(); + target.getTags().addAll( list ); + } + } + else { + List list = stringListToTagEntityList( source.getTags() ); + if ( list != null ) { + target.setTags( list ); + } + } + } + + protected List stringListToTagEntityList(List list) { + if ( list == null ) { + return null; + } + + List list1 = new ArrayList( list.size() ); + for ( String string : list ) { + list1.add( toTag( string ) ); + } + + return list1; + } + + protected List tagEntityListToStringList(List list) { + if ( list == null ) { + return null; + } + + List list1 = new ArrayList( list.size() ); + for ( TagEntity tagEntity : list ) { + list1.add( fromTag( tagEntity ) ); + } + + return list1; + } +} diff --git a/bean-mapping/output/05-broken-compile.txt b/bean-mapping/output/05-broken-compile.txt new file mode 100644 index 0000000..c09bbae --- /dev/null +++ b/bean-mapping/output/05-broken-compile.txt @@ -0,0 +1,18 @@ +$ javac -processorpath target/processor/mapstruct-processor-*.jar -cp target/classes:target/lib/mapstruct-*.jar src/broken/java/BrokenInverseMapper.java +src/broken/java/BrokenInverseMapper.java:21: error: Several matching inverse methods exist: toEntity(), update(). Specify a name explicitly. + @InheritInverseConfiguration + ^ +src/broken/java/BrokenInverseMapper.java:22: warning: Unmapped target property: "email". + UserDTO toDto(UserEntity entity); + ^ +1 error +1 warning +javac exit code: 1 + +$ javac -processorpath target/processor/mapstruct-processor-*.jar -cp target/classes:target/lib/mapstruct-*.jar src/broken/java/BrokenUserMapper.java +src/broken/java/BrokenUserMapper.java:14: error: Unmapped target property: "phone". + UserEntityV2 toEntity(UserDTO dto); + ^ +1 error +javac exit code: 1 + diff --git a/bean-mapping/output/06-tests.txt b/bean-mapping/output/06-tests.txt new file mode 100644 index 0000000..11329bb --- /dev/null +++ b/bean-mapping/output/06-tests.txt @@ -0,0 +1,4 @@ +------------------------------------------------------------------------------- +Test set: com.ankurm.beanmapping.BeanMappingClaimsTest +------------------------------------------------------------------------------- +Tests run: 14, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.468 s -- in com.ankurm.beanmapping.BeanMappingClaimsTest diff --git a/bean-mapping/output/07-benchmark.json b/bean-mapping/output/07-benchmark.json new file mode 100644 index 0000000..4cc76be --- /dev/null +++ b/bean-mapping/output/07-benchmark.json @@ -0,0 +1,585 @@ +[ + { + "jmhVersion" : "1.37", + "benchmark" : "com.ankurm.beanmapping.bench.MappingBenchmark.dozerFlat", + "mode" : "thrpt", + "threads" : 1, + "forks" : 3, + "jvm" : "/tmp/tools/jdk-25.0.5.1+2/bin/java", + "jvmArgs" : [ + "-Djavax.net.ssl.trustStore=/etc/ssl/certs/java/cacerts", + "-Djavax.net.ssl.trustStoreType=JKS", + "-Dhttps.proxyHost=127.0.0.1", + "-Dhttps.proxyPort=41629", + "-Dhttp.nonProxyHosts=localhost|127.0.0.1|::1|127.*|0.*|::|169.254.*|api.anthropic.com|api-staging.anthropic.com|api-pr-preview.anthropic.com|mcp-proxy.anthropic.com|mcp-proxy-staging.anthropic.com|registry.npmjs.org|jsr.io|npm.jsr.io|pypi.org|files.pythonhosted.org|index.crates.io|proxy.golang.org|host.docker.internal|10.*|172.16.*|172.17.*|172.18.*|172.19.*|172.20.*|172.21.*|172.22.*|172.23.*|172.24.*|172.25.*|172.26.*|172.27.*|172.28.*|172.29.*|172.30.*|172.31.*|192.168.*|100.64.0.0/10|*.svc.cluster.local|*.svc.cluster.local", + "-Djdk.http.auth.tunneling.disabledSchemes=", + "-Djdk.http.auth.proxying.disabledSchemes=", + "-XX:+UnlockDiagnosticVMOptions", + "-XX:DisableIntrinsic=_base64_encodeBlock,_base64_decodeBlock" + ], + "jdkVersion" : "25.0.5.1", + "vmName" : "OpenJDK 64-Bit Server VM", + "vmVersion" : "25.0.5.1+2-LTS", + "warmupIterations" : 3, + "warmupTime" : "1 s", + "warmupBatchSize" : 1, + "measurementIterations" : 8, + "measurementTime" : "1 s", + "measurementBatchSize" : 1, + "primaryMetric" : { + "score" : 221.15079518270628, + "scoreError" : 83.37701602851097, + "scoreConfidence" : [ + 137.77377915419532, + 304.5278112112172 + ], + "scorePercentiles" : { + "0.0" : 29.950808062188617, + "50.0" : 201.99227949431673, + "90.0" : 362.5805546202363, + "95.0" : 374.9859328523041, + "99.0" : 375.27587601099776, + "99.9" : 375.27587601099776, + "99.99" : 375.27587601099776, + "99.999" : 375.27587601099776, + "99.9999" : 375.27587601099776, + "100.0" : 375.27587601099776 + }, + "scoreUnit" : "ops/ms", + "rawData" : [ + [ + 162.80935776974255, + 159.30514271118957, + 209.0469208257009, + 374.11610337622295, + 351.0450058642496, + 375.27587601099776, + 322.6550757936784, + 141.99450543556222 + ], + [ + 64.72840723548235, + 90.8368311100669, + 58.724186393037535, + 287.28397669926323, + 284.02414132204586, + 291.7042172128254, + 194.93763816293253, + 158.03844475423315 + ], + [ + 29.950808062188617, + 98.06581473898368, + 191.82718035511962, + 301.68550806706435, + 335.74852200504904, + 344.12451119273175, + 305.2200358367662, + 174.47087344981554 + ] + ] + }, + "secondaryMetrics" : { + } + }, + { + "jmhVersion" : "1.37", + "benchmark" : "com.ankurm.beanmapping.bench.MappingBenchmark.dozerNested", + "mode" : "thrpt", + "threads" : 1, + "forks" : 3, + "jvm" : "/tmp/tools/jdk-25.0.5.1+2/bin/java", + "jvmArgs" : [ + "-Djavax.net.ssl.trustStore=/etc/ssl/certs/java/cacerts", + "-Djavax.net.ssl.trustStoreType=JKS", + "-Dhttps.proxyHost=127.0.0.1", + "-Dhttps.proxyPort=41629", + "-Dhttp.nonProxyHosts=localhost|127.0.0.1|::1|127.*|0.*|::|169.254.*|api.anthropic.com|api-staging.anthropic.com|api-pr-preview.anthropic.com|mcp-proxy.anthropic.com|mcp-proxy-staging.anthropic.com|registry.npmjs.org|jsr.io|npm.jsr.io|pypi.org|files.pythonhosted.org|index.crates.io|proxy.golang.org|host.docker.internal|10.*|172.16.*|172.17.*|172.18.*|172.19.*|172.20.*|172.21.*|172.22.*|172.23.*|172.24.*|172.25.*|172.26.*|172.27.*|172.28.*|172.29.*|172.30.*|172.31.*|192.168.*|100.64.0.0/10|*.svc.cluster.local|*.svc.cluster.local", + "-Djdk.http.auth.tunneling.disabledSchemes=", + "-Djdk.http.auth.proxying.disabledSchemes=", + "-XX:+UnlockDiagnosticVMOptions", + "-XX:DisableIntrinsic=_base64_encodeBlock,_base64_decodeBlock" + ], + "jdkVersion" : "25.0.5.1", + "vmName" : "OpenJDK 64-Bit Server VM", + "vmVersion" : "25.0.5.1+2-LTS", + "warmupIterations" : 3, + "warmupTime" : "1 s", + "warmupBatchSize" : 1, + "measurementIterations" : 8, + "measurementTime" : "1 s", + "measurementBatchSize" : 1, + "primaryMetric" : { + "score" : 159.81265797229125, + "scoreError" : 42.01270749681554, + "scoreConfidence" : [ + 117.79995047547571, + 201.82536546910677 + ], + "scorePercentiles" : { + "0.0" : 56.28656476628815, + "50.0" : 163.1302842450439, + "90.0" : 246.44824276107357, + "95.0" : 254.10889309804801, + "99.0" : 254.85479830823056, + "99.9" : 254.85479830823056, + "99.99" : 254.85479830823056, + "99.999" : 254.85479830823056, + "99.9999" : 254.85479830823056, + "100.0" : 254.85479830823056 + }, + "scoreUnit" : "ops/ms", + "rawData" : [ + [ + 188.91147878320257, + 251.8711774675004, + 204.80805807839263, + 229.46131000863835, + 203.79758071247724, + 254.85479830823056, + 132.12019662732925, + 241.02530805464676 + ], + [ + 162.3316802383285, + 155.20719711595157, + 127.75351797236094, + 163.9288882517593, + 136.41229225821795, + 106.52200706460687, + 168.64666067900976, + 178.70597225941933 + ], + [ + 56.28656476628815, + 134.86000896181233, + 95.35893170555761, + 112.87165253246849, + 66.16653210131724, + 112.40650802563378, + 174.40088077752037, + 176.79458858431974 + ] + ] + }, + "secondaryMetrics" : { + } + }, + { + "jmhVersion" : "1.37", + "benchmark" : "com.ankurm.beanmapping.bench.MappingBenchmark.dozerNewMapperPerCallFlat", + "mode" : "thrpt", + "threads" : 1, + "forks" : 3, + "jvm" : "/tmp/tools/jdk-25.0.5.1+2/bin/java", + "jvmArgs" : [ + "-Djavax.net.ssl.trustStore=/etc/ssl/certs/java/cacerts", + "-Djavax.net.ssl.trustStoreType=JKS", + "-Dhttps.proxyHost=127.0.0.1", + "-Dhttps.proxyPort=41629", + "-Dhttp.nonProxyHosts=localhost|127.0.0.1|::1|127.*|0.*|::|169.254.*|api.anthropic.com|api-staging.anthropic.com|api-pr-preview.anthropic.com|mcp-proxy.anthropic.com|mcp-proxy-staging.anthropic.com|registry.npmjs.org|jsr.io|npm.jsr.io|pypi.org|files.pythonhosted.org|index.crates.io|proxy.golang.org|host.docker.internal|10.*|172.16.*|172.17.*|172.18.*|172.19.*|172.20.*|172.21.*|172.22.*|172.23.*|172.24.*|172.25.*|172.26.*|172.27.*|172.28.*|172.29.*|172.30.*|172.31.*|192.168.*|100.64.0.0/10|*.svc.cluster.local|*.svc.cluster.local", + "-Djdk.http.auth.tunneling.disabledSchemes=", + "-Djdk.http.auth.proxying.disabledSchemes=", + "-XX:+UnlockDiagnosticVMOptions", + "-XX:DisableIntrinsic=_base64_encodeBlock,_base64_decodeBlock" + ], + "jdkVersion" : "25.0.5.1", + "vmName" : "OpenJDK 64-Bit Server VM", + "vmVersion" : "25.0.5.1+2-LTS", + "warmupIterations" : 3, + "warmupTime" : "1 s", + "warmupBatchSize" : 1, + "measurementIterations" : 8, + "measurementTime" : "1 s", + "measurementBatchSize" : 1, + "primaryMetric" : { + "score" : 9.221032039202589, + "scoreError" : 4.06391604871615, + "scoreConfidence" : [ + 5.157115990486439, + 13.284948087918739 + ], + "scorePercentiles" : { + "0.0" : 1.9608445741405718, + "50.0" : 8.596891663404232, + "90.0" : 17.78738095301896, + "95.0" : 18.916680690005357, + "99.0" : 19.25930860743531, + "99.9" : 19.25930860743531, + "99.99" : 19.25930860743531, + "99.999" : 19.25930860743531, + "99.9999" : 19.25930860743531, + "100.0" : 19.25930860743531 + }, + "scoreUnit" : "ops/ms", + "rawData" : [ + [ + 5.197913912233286, + 6.863226083195238, + 8.416361826367531, + 8.897873634947418, + 9.276573887128896, + 13.764427250618908, + 17.685964968322427, + 17.888796937715497 + ], + [ + 3.0031192571596845, + 6.329671923106893, + 11.834490915973934, + 11.988478515517281, + 11.539411525765868, + 12.752461640318572, + 17.668771460940654, + 19.25930860743531 + ], + [ + 2.698114866070608, + 2.044443788324168, + 4.34391774140169, + 4.717761494543523, + 1.9608445741405718, + 8.093516231461402, + 6.301896397731904, + 8.777421500440933 + ] + ] + }, + "secondaryMetrics" : { + } + }, + { + "jmhVersion" : "1.37", + "benchmark" : "com.ankurm.beanmapping.bench.MappingBenchmark.manualFlat", + "mode" : "thrpt", + "threads" : 1, + "forks" : 3, + "jvm" : "/tmp/tools/jdk-25.0.5.1+2/bin/java", + "jvmArgs" : [ + "-Djavax.net.ssl.trustStore=/etc/ssl/certs/java/cacerts", + "-Djavax.net.ssl.trustStoreType=JKS", + "-Dhttps.proxyHost=127.0.0.1", + "-Dhttps.proxyPort=41629", + "-Dhttp.nonProxyHosts=localhost|127.0.0.1|::1|127.*|0.*|::|169.254.*|api.anthropic.com|api-staging.anthropic.com|api-pr-preview.anthropic.com|mcp-proxy.anthropic.com|mcp-proxy-staging.anthropic.com|registry.npmjs.org|jsr.io|npm.jsr.io|pypi.org|files.pythonhosted.org|index.crates.io|proxy.golang.org|host.docker.internal|10.*|172.16.*|172.17.*|172.18.*|172.19.*|172.20.*|172.21.*|172.22.*|172.23.*|172.24.*|172.25.*|172.26.*|172.27.*|172.28.*|172.29.*|172.30.*|172.31.*|192.168.*|100.64.0.0/10|*.svc.cluster.local|*.svc.cluster.local", + "-Djdk.http.auth.tunneling.disabledSchemes=", + "-Djdk.http.auth.proxying.disabledSchemes=", + "-XX:+UnlockDiagnosticVMOptions", + "-XX:DisableIntrinsic=_base64_encodeBlock,_base64_decodeBlock" + ], + "jdkVersion" : "25.0.5.1", + "vmName" : "OpenJDK 64-Bit Server VM", + "vmVersion" : "25.0.5.1+2-LTS", + "warmupIterations" : 3, + "warmupTime" : "1 s", + "warmupBatchSize" : 1, + "measurementIterations" : 8, + "measurementTime" : "1 s", + "measurementBatchSize" : 1, + "primaryMetric" : { + "score" : 36658.50931424832, + "scoreError" : 11549.811525596124, + "scoreConfidence" : [ + 25108.697788652193, + 48208.32083984444 + ], + "scorePercentiles" : { + "0.0" : 8616.564820674375, + "50.0" : 34869.59046592402, + "90.0" : 53913.734619957104, + "95.0" : 56333.08008254007, + "99.0" : 56811.389754247124, + "99.9" : 56811.389754247124, + "99.99" : 56811.389754247124, + "99.999" : 56811.389754247124, + "99.9999" : 56811.389754247124, + "100.0" : 56811.389754247124 + }, + "scoreUnit" : "ops/ms", + "rawData" : [ + [ + 52929.31817249529, + 51538.43581529059, + 51921.472877423614, + 21211.946114687296, + 18317.43907644728, + 8616.564820674375, + 11358.478794878209, + 21576.385191304056 + ], + [ + 51185.85055533155, + 54898.15106741891, + 56811.389754247124, + 50796.970273459236, + 42961.41484744766, + 51347.39132927612, + 51193.033015103225, + 45944.57031667923 + ], + [ + 35151.81329069875, + 30181.05694069575, + 26316.96548966959, + 32440.747793242997, + 28489.900576837113, + 21442.281584605935, + 34587.36764114928, + 28585.278202896396 + ] + ] + }, + "secondaryMetrics" : { + } + }, + { + "jmhVersion" : "1.37", + "benchmark" : "com.ankurm.beanmapping.bench.MappingBenchmark.manualNested", + "mode" : "thrpt", + "threads" : 1, + "forks" : 3, + "jvm" : "/tmp/tools/jdk-25.0.5.1+2/bin/java", + "jvmArgs" : [ + "-Djavax.net.ssl.trustStore=/etc/ssl/certs/java/cacerts", + "-Djavax.net.ssl.trustStoreType=JKS", + "-Dhttps.proxyHost=127.0.0.1", + "-Dhttps.proxyPort=41629", + "-Dhttp.nonProxyHosts=localhost|127.0.0.1|::1|127.*|0.*|::|169.254.*|api.anthropic.com|api-staging.anthropic.com|api-pr-preview.anthropic.com|mcp-proxy.anthropic.com|mcp-proxy-staging.anthropic.com|registry.npmjs.org|jsr.io|npm.jsr.io|pypi.org|files.pythonhosted.org|index.crates.io|proxy.golang.org|host.docker.internal|10.*|172.16.*|172.17.*|172.18.*|172.19.*|172.20.*|172.21.*|172.22.*|172.23.*|172.24.*|172.25.*|172.26.*|172.27.*|172.28.*|172.29.*|172.30.*|172.31.*|192.168.*|100.64.0.0/10|*.svc.cluster.local|*.svc.cluster.local", + "-Djdk.http.auth.tunneling.disabledSchemes=", + "-Djdk.http.auth.proxying.disabledSchemes=", + "-XX:+UnlockDiagnosticVMOptions", + "-XX:DisableIntrinsic=_base64_encodeBlock,_base64_decodeBlock" + ], + "jdkVersion" : "25.0.5.1", + "vmName" : "OpenJDK 64-Bit Server VM", + "vmVersion" : "25.0.5.1+2-LTS", + "warmupIterations" : 3, + "warmupTime" : "1 s", + "warmupBatchSize" : 1, + "measurementIterations" : 8, + "measurementTime" : "1 s", + "measurementBatchSize" : 1, + "primaryMetric" : { + "score" : 12397.935082219037, + "scoreError" : 3307.0988529853003, + "scoreConfidence" : [ + 9090.836229233737, + 15705.033935204337 + ], + "scorePercentiles" : { + "0.0" : 4408.159358319243, + "50.0" : 12233.911614112778, + "90.0" : 18948.28737971707, + "95.0" : 19508.580419549864, + "99.0" : 19657.44456013049, + "99.9" : 19657.44456013049, + "99.99" : 19657.44456013049, + "99.999" : 19657.44456013049, + "99.9999" : 19657.44456013049, + "100.0" : 19657.44456013049 + }, + "scoreUnit" : "ops/ms", + "rawData" : [ + [ + 10665.623223941877, + 11545.818692688285, + 15453.808847656579, + 12528.467869445092, + 11771.402132556617, + 15525.849648506868, + 15721.214316794807, + 13907.483171519409 + ], + [ + 15010.272183776586, + 16785.843176258153, + 19657.44456013049, + 18834.586761626146, + 19061.987997807988, + 12487.743718798134, + 4408.159358319243, + 8350.43468336297 + ], + [ + 8712.362488971254, + 15450.291146243984, + 7158.29218979867, + 9385.493684868008, + 4466.870092406491, + 8184.311482164494, + 10496.601036187225, + 11980.07950942742 + ] + ] + }, + "secondaryMetrics" : { + } + }, + { + "jmhVersion" : "1.37", + "benchmark" : "com.ankurm.beanmapping.bench.MappingBenchmark.mapstructFlat", + "mode" : "thrpt", + "threads" : 1, + "forks" : 3, + "jvm" : "/tmp/tools/jdk-25.0.5.1+2/bin/java", + "jvmArgs" : [ + "-Djavax.net.ssl.trustStore=/etc/ssl/certs/java/cacerts", + "-Djavax.net.ssl.trustStoreType=JKS", + "-Dhttps.proxyHost=127.0.0.1", + "-Dhttps.proxyPort=41629", + "-Dhttp.nonProxyHosts=localhost|127.0.0.1|::1|127.*|0.*|::|169.254.*|api.anthropic.com|api-staging.anthropic.com|api-pr-preview.anthropic.com|mcp-proxy.anthropic.com|mcp-proxy-staging.anthropic.com|registry.npmjs.org|jsr.io|npm.jsr.io|pypi.org|files.pythonhosted.org|index.crates.io|proxy.golang.org|host.docker.internal|10.*|172.16.*|172.17.*|172.18.*|172.19.*|172.20.*|172.21.*|172.22.*|172.23.*|172.24.*|172.25.*|172.26.*|172.27.*|172.28.*|172.29.*|172.30.*|172.31.*|192.168.*|100.64.0.0/10|*.svc.cluster.local|*.svc.cluster.local", + "-Djdk.http.auth.tunneling.disabledSchemes=", + "-Djdk.http.auth.proxying.disabledSchemes=", + "-XX:+UnlockDiagnosticVMOptions", + "-XX:DisableIntrinsic=_base64_encodeBlock,_base64_decodeBlock" + ], + "jdkVersion" : "25.0.5.1", + "vmName" : "OpenJDK 64-Bit Server VM", + "vmVersion" : "25.0.5.1+2-LTS", + "warmupIterations" : 3, + "warmupTime" : "1 s", + "warmupBatchSize" : 1, + "measurementIterations" : 8, + "measurementTime" : "1 s", + "measurementBatchSize" : 1, + "primaryMetric" : { + "score" : 31096.421756318723, + "scoreError" : 6517.096774234391, + "scoreConfidence" : [ + 24579.324982084334, + 37613.51853055311 + ], + "scorePercentiles" : { + "0.0" : 15873.046652856061, + "50.0" : 33991.333938806754, + "90.0" : 41851.091546542055, + "95.0" : 45738.35159030021, + "99.0" : 46349.47716372606, + "99.9" : 46349.47716372606, + "99.99" : 46349.47716372606, + "99.999" : 46349.47716372606, + "99.9999" : 46349.47716372606, + "100.0" : 46349.47716372606 + }, + "scoreUnit" : "ops/ms", + "rawData" : [ + [ + 15873.046652856061, + 21228.62581640453, + 22082.510170756155, + 18314.139231942652, + 30257.616688173817, + 33625.70745144038, + 38672.52293936087, + 35243.923402639586 + ], + [ + 21795.334251630855, + 27627.533238147644, + 39797.20822306143, + 36114.3090001513, + 24839.657197755372, + 43904.97487002268, + 26649.67000426847, + 17581.4019670752 + ], + [ + 37464.802328680926, + 46349.47716372606, + 36049.75627453405, + 37930.3406963263, + 30043.247088662778, + 34356.96042617312, + 35651.3451420274, + 34860.011925831706 + ] + ] + }, + "secondaryMetrics" : { + } + }, + { + "jmhVersion" : "1.37", + "benchmark" : "com.ankurm.beanmapping.bench.MappingBenchmark.mapstructNested", + "mode" : "thrpt", + "threads" : 1, + "forks" : 3, + "jvm" : "/tmp/tools/jdk-25.0.5.1+2/bin/java", + "jvmArgs" : [ + "-Djavax.net.ssl.trustStore=/etc/ssl/certs/java/cacerts", + "-Djavax.net.ssl.trustStoreType=JKS", + "-Dhttps.proxyHost=127.0.0.1", + "-Dhttps.proxyPort=41629", + "-Dhttp.nonProxyHosts=localhost|127.0.0.1|::1|127.*|0.*|::|169.254.*|api.anthropic.com|api-staging.anthropic.com|api-pr-preview.anthropic.com|mcp-proxy.anthropic.com|mcp-proxy-staging.anthropic.com|registry.npmjs.org|jsr.io|npm.jsr.io|pypi.org|files.pythonhosted.org|index.crates.io|proxy.golang.org|host.docker.internal|10.*|172.16.*|172.17.*|172.18.*|172.19.*|172.20.*|172.21.*|172.22.*|172.23.*|172.24.*|172.25.*|172.26.*|172.27.*|172.28.*|172.29.*|172.30.*|172.31.*|192.168.*|100.64.0.0/10|*.svc.cluster.local|*.svc.cluster.local", + "-Djdk.http.auth.tunneling.disabledSchemes=", + "-Djdk.http.auth.proxying.disabledSchemes=", + "-XX:+UnlockDiagnosticVMOptions", + "-XX:DisableIntrinsic=_base64_encodeBlock,_base64_decodeBlock" + ], + "jdkVersion" : "25.0.5.1", + "vmName" : "OpenJDK 64-Bit Server VM", + "vmVersion" : "25.0.5.1+2-LTS", + "warmupIterations" : 3, + "warmupTime" : "1 s", + "warmupBatchSize" : 1, + "measurementIterations" : 8, + "measurementTime" : "1 s", + "measurementBatchSize" : 1, + "primaryMetric" : { + "score" : 16205.345660035842, + "scoreError" : 2772.961050076817, + "scoreConfidence" : [ + 13432.384609959025, + 18978.306710112658 + ], + "scorePercentiles" : { + "0.0" : 5234.956518533699, + "50.0" : 16676.016400835335, + "90.0" : 19922.511815969265, + "95.0" : 20317.947503559906, + "99.0" : 20400.899920080494, + "99.9" : 20400.899920080494, + "99.99" : 20400.899920080494, + "99.999" : 20400.899920080494, + "99.9999" : 20400.899920080494, + "100.0" : 20400.899920080494 + }, + "scoreUnit" : "ops/ms", + "rawData" : [ + [ + 19494.370822686196, + 16749.274061004857, + 18860.277429876813, + 20069.090253998143, + 19614.200706449967, + 18657.195552152112, + 16197.474160203068, + 17816.94923062462 + ], + [ + 5234.956518533699, + 17848.744784729886, + 11217.631598692515, + 16602.758740665813, + 14791.715605670659, + 15380.682115192976, + 13429.601893140147, + 12463.30987314028 + ], + [ + 11548.353519284816, + 16361.331839542689, + 13394.407680723036, + 15554.882527815755, + 19775.933377940382, + 18552.136490034794, + 20400.899920080494, + 18912.117138676527 + ] + ] + }, + "secondaryMetrics" : { + } + } +] + + diff --git a/bean-mapping/output/07-benchmark.txt b/bean-mapping/output/07-benchmark.txt new file mode 100644 index 0000000..13ad8e8 --- /dev/null +++ b/bean-mapping/output/07-benchmark.txt @@ -0,0 +1,539 @@ +WARNING: A terminally deprecated method in sun.misc.Unsafe has been called +WARNING: sun.misc.Unsafe::objectFieldOffset has been called by org.openjdk.jmh.util.Utils (file:/tmp/work/java-core-examples/bean-mapping/target/lib/jmh-core-1.37.jar) +WARNING: Please consider reporting this to the maintainers of class org.openjdk.jmh.util.Utils +WARNING: sun.misc.Unsafe::objectFieldOffset will be removed in a future release +# JMH version: 1.37 +# VM version: JDK 25.0.5.1, OpenJDK 64-Bit Server VM, 25.0.5.1+2-LTS +# VM invoker: /tmp/tools/jdk-25.0.5.1+2/bin/java +# VM options: -Djavax.net.ssl.trustStore=/etc/ssl/certs/java/cacerts -Djavax.net.ssl.trustStoreType=JKS -Dhttps.proxyHost=127.0.0.1 -Dhttps.proxyPort=41629 -Dhttp.nonProxyHosts=localhost|127.0.0.1|::1|127.*|0.*|::|169.254.*|api.anthropic.com|api-staging.anthropic.com|api-pr-preview.anthropic.com|mcp-proxy.anthropic.com|mcp-proxy-staging.anthropic.com|registry.npmjs.org|jsr.io|npm.jsr.io|pypi.org|files.pythonhosted.org|index.crates.io|proxy.golang.org|host.docker.internal|10.*|172.16.*|172.17.*|172.18.*|172.19.*|172.20.*|172.21.*|172.22.*|172.23.*|172.24.*|172.25.*|172.26.*|172.27.*|172.28.*|172.29.*|172.30.*|172.31.*|192.168.*|100.64.0.0/10|*.svc.cluster.local|*.svc.cluster.local -Djdk.http.auth.tunneling.disabledSchemes= -Djdk.http.auth.proxying.disabledSchemes= -XX:+UnlockDiagnosticVMOptions -XX:DisableIntrinsic=_base64_encodeBlock,_base64_decodeBlock +# Blackhole mode: compiler (auto-detected, use -Djmh.blackhole.autoDetect=false to disable) +# Warmup: 3 iterations, 1 s each +# Measurement: 8 iterations, 1 s each +# Timeout: 10 min per iteration +# Threads: 1 thread, will synchronize iterations +# Benchmark mode: Throughput, ops/time +# Benchmark: com.ankurm.beanmapping.bench.MappingBenchmark.dozerFlat + +# Run progress: 0.00% complete, ETA 00:03:51 +# Fork: 1 of 3 +WARNING: A terminally deprecated method in sun.misc.Unsafe has been called +WARNING: sun.misc.Unsafe::objectFieldOffset has been called by org.openjdk.jmh.util.Utils (file:/tmp/work/java-core-examples/bean-mapping/target/lib/jmh-core-1.37.jar) +WARNING: Please consider reporting this to the maintainers of class org.openjdk.jmh.util.Utils +WARNING: sun.misc.Unsafe::objectFieldOffset will be removed in a future release +# Warmup Iteration 1: 74.562 ops/ms +# Warmup Iteration 2: 215.036 ops/ms +# Warmup Iteration 3: 123.121 ops/ms +Iteration 1: 162.809 ops/ms +Iteration 2: 159.305 ops/ms +Iteration 3: 209.047 ops/ms +Iteration 4: 374.116 ops/ms +Iteration 5: 351.045 ops/ms +Iteration 6: 375.276 ops/ms +Iteration 7: 322.655 ops/ms +Iteration 8: 141.995 ops/ms + +# Run progress: 4.76% complete, ETA 00:03:58 +# Fork: 2 of 3 +WARNING: A terminally deprecated method in sun.misc.Unsafe has been called +WARNING: sun.misc.Unsafe::objectFieldOffset has been called by org.openjdk.jmh.util.Utils (file:/tmp/work/java-core-examples/bean-mapping/target/lib/jmh-core-1.37.jar) +WARNING: Please consider reporting this to the maintainers of class org.openjdk.jmh.util.Utils +WARNING: sun.misc.Unsafe::objectFieldOffset will be removed in a future release +# Warmup Iteration 1: 32.554 ops/ms +# Warmup Iteration 2: 37.714 ops/ms +# Warmup Iteration 3: 43.491 ops/ms +Iteration 1: 64.728 ops/ms +Iteration 2: 90.837 ops/ms +Iteration 3: 58.724 ops/ms +Iteration 4: 287.284 ops/ms +Iteration 5: 284.024 ops/ms +Iteration 6: 291.704 ops/ms +Iteration 7: 194.938 ops/ms +Iteration 8: 158.038 ops/ms + +# Run progress: 9.52% complete, ETA 00:03:52 +# Fork: 3 of 3 +WARNING: A terminally deprecated method in sun.misc.Unsafe has been called +WARNING: sun.misc.Unsafe::objectFieldOffset has been called by org.openjdk.jmh.util.Utils (file:/tmp/work/java-core-examples/bean-mapping/target/lib/jmh-core-1.37.jar) +WARNING: Please consider reporting this to the maintainers of class org.openjdk.jmh.util.Utils +WARNING: sun.misc.Unsafe::objectFieldOffset will be removed in a future release +# Warmup Iteration 1: 22.510 ops/ms +# Warmup Iteration 2: 49.928 ops/ms +# Warmup Iteration 3: 54.992 ops/ms +Iteration 1: 29.951 ops/ms +Iteration 2: 98.066 ops/ms +Iteration 3: 191.827 ops/ms +Iteration 4: 301.686 ops/ms +Iteration 5: 335.749 ops/ms +Iteration 6: 344.125 ops/ms +Iteration 7: 305.220 ops/ms +Iteration 8: 174.471 ops/ms + + +Result "com.ankurm.beanmapping.bench.MappingBenchmark.dozerFlat": + 221.151 ±(99.9%) 83.377 ops/ms [Average] + (min, avg, max) = (29.951, 221.151, 375.276), stdev = 108.414 + CI (99.9%): [137.774, 304.528] (assumes normal distribution) + + +# JMH version: 1.37 +# VM version: JDK 25.0.5.1, OpenJDK 64-Bit Server VM, 25.0.5.1+2-LTS +# VM invoker: /tmp/tools/jdk-25.0.5.1+2/bin/java +# VM options: -Djavax.net.ssl.trustStore=/etc/ssl/certs/java/cacerts -Djavax.net.ssl.trustStoreType=JKS -Dhttps.proxyHost=127.0.0.1 -Dhttps.proxyPort=41629 -Dhttp.nonProxyHosts=localhost|127.0.0.1|::1|127.*|0.*|::|169.254.*|api.anthropic.com|api-staging.anthropic.com|api-pr-preview.anthropic.com|mcp-proxy.anthropic.com|mcp-proxy-staging.anthropic.com|registry.npmjs.org|jsr.io|npm.jsr.io|pypi.org|files.pythonhosted.org|index.crates.io|proxy.golang.org|host.docker.internal|10.*|172.16.*|172.17.*|172.18.*|172.19.*|172.20.*|172.21.*|172.22.*|172.23.*|172.24.*|172.25.*|172.26.*|172.27.*|172.28.*|172.29.*|172.30.*|172.31.*|192.168.*|100.64.0.0/10|*.svc.cluster.local|*.svc.cluster.local -Djdk.http.auth.tunneling.disabledSchemes= -Djdk.http.auth.proxying.disabledSchemes= -XX:+UnlockDiagnosticVMOptions -XX:DisableIntrinsic=_base64_encodeBlock,_base64_decodeBlock +# Blackhole mode: compiler (auto-detected, use -Djmh.blackhole.autoDetect=false to disable) +# Warmup: 3 iterations, 1 s each +# Measurement: 8 iterations, 1 s each +# Timeout: 10 min per iteration +# Threads: 1 thread, will synchronize iterations +# Benchmark mode: Throughput, ops/time +# Benchmark: com.ankurm.beanmapping.bench.MappingBenchmark.dozerNested + +# Run progress: 14.29% complete, ETA 00:03:42 +# Fork: 1 of 3 +WARNING: A terminally deprecated method in sun.misc.Unsafe has been called +WARNING: sun.misc.Unsafe::objectFieldOffset has been called by org.openjdk.jmh.util.Utils (file:/tmp/work/java-core-examples/bean-mapping/target/lib/jmh-core-1.37.jar) +WARNING: Please consider reporting this to the maintainers of class org.openjdk.jmh.util.Utils +WARNING: sun.misc.Unsafe::objectFieldOffset will be removed in a future release +# Warmup Iteration 1: 21.066 ops/ms +# Warmup Iteration 2: 54.979 ops/ms +# Warmup Iteration 3: 184.689 ops/ms +Iteration 1: 188.911 ops/ms +Iteration 2: 251.871 ops/ms +Iteration 3: 204.808 ops/ms +Iteration 4: 229.461 ops/ms +Iteration 5: 203.798 ops/ms +Iteration 6: 254.855 ops/ms +Iteration 7: 132.120 ops/ms +Iteration 8: 241.025 ops/ms + +# Run progress: 19.05% complete, ETA 00:03:30 +# Fork: 2 of 3 +WARNING: A terminally deprecated method in sun.misc.Unsafe has been called +WARNING: sun.misc.Unsafe::objectFieldOffset has been called by org.openjdk.jmh.util.Utils (file:/tmp/work/java-core-examples/bean-mapping/target/lib/jmh-core-1.37.jar) +WARNING: Please consider reporting this to the maintainers of class org.openjdk.jmh.util.Utils +WARNING: sun.misc.Unsafe::objectFieldOffset will be removed in a future release +# Warmup Iteration 1: 9.771 ops/ms +# Warmup Iteration 2: 117.113 ops/ms +# Warmup Iteration 3: 228.645 ops/ms +Iteration 1: 162.332 ops/ms +Iteration 2: 155.207 ops/ms +Iteration 3: 127.754 ops/ms +Iteration 4: 163.929 ops/ms +Iteration 5: 136.412 ops/ms +Iteration 6: 106.522 ops/ms +Iteration 7: 168.647 ops/ms +Iteration 8: 178.706 ops/ms + +# Run progress: 23.81% complete, ETA 00:03:17 +# Fork: 3 of 3 +WARNING: A terminally deprecated method in sun.misc.Unsafe has been called +WARNING: sun.misc.Unsafe::objectFieldOffset has been called by org.openjdk.jmh.util.Utils (file:/tmp/work/java-core-examples/bean-mapping/target/lib/jmh-core-1.37.jar) +WARNING: Please consider reporting this to the maintainers of class org.openjdk.jmh.util.Utils +WARNING: sun.misc.Unsafe::objectFieldOffset will be removed in a future release +# Warmup Iteration 1: 11.403 ops/ms +# Warmup Iteration 2: 29.984 ops/ms +# Warmup Iteration 3: 45.793 ops/ms +Iteration 1: 56.287 ops/ms +Iteration 2: 134.860 ops/ms +Iteration 3: 95.359 ops/ms +Iteration 4: 112.872 ops/ms +Iteration 5: 66.167 ops/ms +Iteration 6: 112.407 ops/ms +Iteration 7: 174.401 ops/ms +Iteration 8: 176.795 ops/ms + + +Result "com.ankurm.beanmapping.bench.MappingBenchmark.dozerNested": + 159.813 ±(99.9%) 42.013 ops/ms [Average] + (min, avg, max) = (56.287, 159.813, 254.855), stdev = 54.628 + CI (99.9%): [117.800, 201.825] (assumes normal distribution) + + +# JMH version: 1.37 +# VM version: JDK 25.0.5.1, OpenJDK 64-Bit Server VM, 25.0.5.1+2-LTS +# VM invoker: /tmp/tools/jdk-25.0.5.1+2/bin/java +# VM options: -Djavax.net.ssl.trustStore=/etc/ssl/certs/java/cacerts -Djavax.net.ssl.trustStoreType=JKS -Dhttps.proxyHost=127.0.0.1 -Dhttps.proxyPort=41629 -Dhttp.nonProxyHosts=localhost|127.0.0.1|::1|127.*|0.*|::|169.254.*|api.anthropic.com|api-staging.anthropic.com|api-pr-preview.anthropic.com|mcp-proxy.anthropic.com|mcp-proxy-staging.anthropic.com|registry.npmjs.org|jsr.io|npm.jsr.io|pypi.org|files.pythonhosted.org|index.crates.io|proxy.golang.org|host.docker.internal|10.*|172.16.*|172.17.*|172.18.*|172.19.*|172.20.*|172.21.*|172.22.*|172.23.*|172.24.*|172.25.*|172.26.*|172.27.*|172.28.*|172.29.*|172.30.*|172.31.*|192.168.*|100.64.0.0/10|*.svc.cluster.local|*.svc.cluster.local -Djdk.http.auth.tunneling.disabledSchemes= -Djdk.http.auth.proxying.disabledSchemes= -XX:+UnlockDiagnosticVMOptions -XX:DisableIntrinsic=_base64_encodeBlock,_base64_decodeBlock +# Blackhole mode: compiler (auto-detected, use -Djmh.blackhole.autoDetect=false to disable) +# Warmup: 3 iterations, 1 s each +# Measurement: 8 iterations, 1 s each +# Timeout: 10 min per iteration +# Threads: 1 thread, will synchronize iterations +# Benchmark mode: Throughput, ops/time +# Benchmark: com.ankurm.beanmapping.bench.MappingBenchmark.dozerNewMapperPerCallFlat + +# Run progress: 28.57% complete, ETA 00:03:04 +# Fork: 1 of 3 +WARNING: A terminally deprecated method in sun.misc.Unsafe has been called +WARNING: sun.misc.Unsafe::objectFieldOffset has been called by org.openjdk.jmh.util.Utils (file:/tmp/work/java-core-examples/bean-mapping/target/lib/jmh-core-1.37.jar) +WARNING: Please consider reporting this to the maintainers of class org.openjdk.jmh.util.Utils +WARNING: sun.misc.Unsafe::objectFieldOffset will be removed in a future release +# Warmup Iteration 1: 0.264 ops/ms +# Warmup Iteration 2: 0.742 ops/ms +# Warmup Iteration 3: 1.271 ops/ms +Iteration 1: 5.198 ops/ms +Iteration 2: 6.863 ops/ms +Iteration 3: 8.416 ops/ms +Iteration 4: 8.898 ops/ms +Iteration 5: 9.277 ops/ms +Iteration 6: 13.764 ops/ms +Iteration 7: 17.686 ops/ms +Iteration 8: 17.889 ops/ms + +# Run progress: 33.33% complete, ETA 00:02:52 +# Fork: 2 of 3 +WARNING: A terminally deprecated method in sun.misc.Unsafe has been called +WARNING: sun.misc.Unsafe::objectFieldOffset has been called by org.openjdk.jmh.util.Utils (file:/tmp/work/java-core-examples/bean-mapping/target/lib/jmh-core-1.37.jar) +WARNING: Please consider reporting this to the maintainers of class org.openjdk.jmh.util.Utils +WARNING: sun.misc.Unsafe::objectFieldOffset will be removed in a future release +# Warmup Iteration 1: 0.186 ops/ms +# Warmup Iteration 2: 4.832 ops/ms +# Warmup Iteration 3: 1.705 ops/ms +Iteration 1: 3.003 ops/ms +Iteration 2: 6.330 ops/ms +Iteration 3: 11.834 ops/ms +Iteration 4: 11.988 ops/ms +Iteration 5: 11.539 ops/ms +Iteration 6: 12.752 ops/ms +Iteration 7: 17.669 ops/ms +Iteration 8: 19.259 ops/ms + +# Run progress: 38.10% complete, ETA 00:02:39 +# Fork: 3 of 3 +WARNING: A terminally deprecated method in sun.misc.Unsafe has been called +WARNING: sun.misc.Unsafe::objectFieldOffset has been called by org.openjdk.jmh.util.Utils (file:/tmp/work/java-core-examples/bean-mapping/target/lib/jmh-core-1.37.jar) +WARNING: Please consider reporting this to the maintainers of class org.openjdk.jmh.util.Utils +WARNING: sun.misc.Unsafe::objectFieldOffset will be removed in a future release +# Warmup Iteration 1: 0.377 ops/ms +# Warmup Iteration 2: 1.755 ops/ms +# Warmup Iteration 3: 2.782 ops/ms +Iteration 1: 2.698 ops/ms +Iteration 2: 2.044 ops/ms +Iteration 3: 4.344 ops/ms +Iteration 4: 4.718 ops/ms +Iteration 5: 1.961 ops/ms +Iteration 6: 8.094 ops/ms +Iteration 7: 6.302 ops/ms +Iteration 8: 8.777 ops/ms + + +Result "com.ankurm.beanmapping.bench.MappingBenchmark.dozerNewMapperPerCallFlat": + 9.221 ±(99.9%) 4.064 ops/ms [Average] + (min, avg, max) = (1.961, 9.221, 19.259), stdev = 5.284 + CI (99.9%): [5.157, 13.285] (assumes normal distribution) + + +# JMH version: 1.37 +# VM version: JDK 25.0.5.1, OpenJDK 64-Bit Server VM, 25.0.5.1+2-LTS +# VM invoker: /tmp/tools/jdk-25.0.5.1+2/bin/java +# VM options: -Djavax.net.ssl.trustStore=/etc/ssl/certs/java/cacerts -Djavax.net.ssl.trustStoreType=JKS -Dhttps.proxyHost=127.0.0.1 -Dhttps.proxyPort=41629 -Dhttp.nonProxyHosts=localhost|127.0.0.1|::1|127.*|0.*|::|169.254.*|api.anthropic.com|api-staging.anthropic.com|api-pr-preview.anthropic.com|mcp-proxy.anthropic.com|mcp-proxy-staging.anthropic.com|registry.npmjs.org|jsr.io|npm.jsr.io|pypi.org|files.pythonhosted.org|index.crates.io|proxy.golang.org|host.docker.internal|10.*|172.16.*|172.17.*|172.18.*|172.19.*|172.20.*|172.21.*|172.22.*|172.23.*|172.24.*|172.25.*|172.26.*|172.27.*|172.28.*|172.29.*|172.30.*|172.31.*|192.168.*|100.64.0.0/10|*.svc.cluster.local|*.svc.cluster.local -Djdk.http.auth.tunneling.disabledSchemes= -Djdk.http.auth.proxying.disabledSchemes= -XX:+UnlockDiagnosticVMOptions -XX:DisableIntrinsic=_base64_encodeBlock,_base64_decodeBlock +# Blackhole mode: compiler (auto-detected, use -Djmh.blackhole.autoDetect=false to disable) +# Warmup: 3 iterations, 1 s each +# Measurement: 8 iterations, 1 s each +# Timeout: 10 min per iteration +# Threads: 1 thread, will synchronize iterations +# Benchmark mode: Throughput, ops/time +# Benchmark: com.ankurm.beanmapping.bench.MappingBenchmark.manualFlat + +# Run progress: 42.86% complete, ETA 00:02:27 +# Fork: 1 of 3 +WARNING: A terminally deprecated method in sun.misc.Unsafe has been called +WARNING: sun.misc.Unsafe::objectFieldOffset has been called by org.openjdk.jmh.util.Utils (file:/tmp/work/java-core-examples/bean-mapping/target/lib/jmh-core-1.37.jar) +WARNING: Please consider reporting this to the maintainers of class org.openjdk.jmh.util.Utils +WARNING: sun.misc.Unsafe::objectFieldOffset will be removed in a future release +# Warmup Iteration 1: 27055.353 ops/ms +# Warmup Iteration 2: 35857.111 ops/ms +# Warmup Iteration 3: 46621.701 ops/ms +Iteration 1: 52929.318 ops/ms +Iteration 2: 51538.436 ops/ms +Iteration 3: 51921.473 ops/ms +Iteration 4: 21211.946 ops/ms +Iteration 5: 18317.439 ops/ms +Iteration 6: 8616.565 ops/ms +Iteration 7: 11358.479 ops/ms +Iteration 8: 21576.385 ops/ms + +# Run progress: 47.62% complete, ETA 00:02:15 +# Fork: 2 of 3 +WARNING: A terminally deprecated method in sun.misc.Unsafe has been called +WARNING: sun.misc.Unsafe::objectFieldOffset has been called by org.openjdk.jmh.util.Utils (file:/tmp/work/java-core-examples/bean-mapping/target/lib/jmh-core-1.37.jar) +WARNING: Please consider reporting this to the maintainers of class org.openjdk.jmh.util.Utils +WARNING: sun.misc.Unsafe::objectFieldOffset will be removed in a future release +# Warmup Iteration 1: 20171.333 ops/ms +# Warmup Iteration 2: 34919.455 ops/ms +# Warmup Iteration 3: 37890.418 ops/ms +Iteration 1: 51185.851 ops/ms +Iteration 2: 54898.151 ops/ms +Iteration 3: 56811.390 ops/ms +Iteration 4: 50796.970 ops/ms +Iteration 5: 42961.415 ops/ms +Iteration 6: 51347.391 ops/ms +Iteration 7: 51193.033 ops/ms +Iteration 8: 45944.570 ops/ms + +# Run progress: 52.38% complete, ETA 00:02:03 +# Fork: 3 of 3 +WARNING: A terminally deprecated method in sun.misc.Unsafe has been called +WARNING: sun.misc.Unsafe::objectFieldOffset has been called by org.openjdk.jmh.util.Utils (file:/tmp/work/java-core-examples/bean-mapping/target/lib/jmh-core-1.37.jar) +WARNING: Please consider reporting this to the maintainers of class org.openjdk.jmh.util.Utils +WARNING: sun.misc.Unsafe::objectFieldOffset will be removed in a future release +# Warmup Iteration 1: 29055.566 ops/ms +# Warmup Iteration 2: 51279.215 ops/ms +# Warmup Iteration 3: 53259.444 ops/ms +Iteration 1: 35151.813 ops/ms +Iteration 2: 30181.057 ops/ms +Iteration 3: 26316.965 ops/ms +Iteration 4: 32440.748 ops/ms +Iteration 5: 28489.901 ops/ms +Iteration 6: 21442.282 ops/ms +Iteration 7: 34587.368 ops/ms +Iteration 8: 28585.278 ops/ms + + +Result "com.ankurm.beanmapping.bench.MappingBenchmark.manualFlat": + 36658.509 ±(99.9%) 11549.812 ops/ms [Average] + (min, avg, max) = (8616.565, 36658.509, 56811.390), stdev = 15018.019 + CI (99.9%): [25108.698, 48208.321] (assumes normal distribution) + + +# JMH version: 1.37 +# VM version: JDK 25.0.5.1, OpenJDK 64-Bit Server VM, 25.0.5.1+2-LTS +# VM invoker: /tmp/tools/jdk-25.0.5.1+2/bin/java +# VM options: -Djavax.net.ssl.trustStore=/etc/ssl/certs/java/cacerts -Djavax.net.ssl.trustStoreType=JKS -Dhttps.proxyHost=127.0.0.1 -Dhttps.proxyPort=41629 -Dhttp.nonProxyHosts=localhost|127.0.0.1|::1|127.*|0.*|::|169.254.*|api.anthropic.com|api-staging.anthropic.com|api-pr-preview.anthropic.com|mcp-proxy.anthropic.com|mcp-proxy-staging.anthropic.com|registry.npmjs.org|jsr.io|npm.jsr.io|pypi.org|files.pythonhosted.org|index.crates.io|proxy.golang.org|host.docker.internal|10.*|172.16.*|172.17.*|172.18.*|172.19.*|172.20.*|172.21.*|172.22.*|172.23.*|172.24.*|172.25.*|172.26.*|172.27.*|172.28.*|172.29.*|172.30.*|172.31.*|192.168.*|100.64.0.0/10|*.svc.cluster.local|*.svc.cluster.local -Djdk.http.auth.tunneling.disabledSchemes= -Djdk.http.auth.proxying.disabledSchemes= -XX:+UnlockDiagnosticVMOptions -XX:DisableIntrinsic=_base64_encodeBlock,_base64_decodeBlock +# Blackhole mode: compiler (auto-detected, use -Djmh.blackhole.autoDetect=false to disable) +# Warmup: 3 iterations, 1 s each +# Measurement: 8 iterations, 1 s each +# Timeout: 10 min per iteration +# Threads: 1 thread, will synchronize iterations +# Benchmark mode: Throughput, ops/time +# Benchmark: com.ankurm.beanmapping.bench.MappingBenchmark.manualNested + +# Run progress: 57.14% complete, ETA 00:01:51 +# Fork: 1 of 3 +WARNING: A terminally deprecated method in sun.misc.Unsafe has been called +WARNING: sun.misc.Unsafe::objectFieldOffset has been called by org.openjdk.jmh.util.Utils (file:/tmp/work/java-core-examples/bean-mapping/target/lib/jmh-core-1.37.jar) +WARNING: Please consider reporting this to the maintainers of class org.openjdk.jmh.util.Utils +WARNING: sun.misc.Unsafe::objectFieldOffset will be removed in a future release +# Warmup Iteration 1: 9619.721 ops/ms +# Warmup Iteration 2: 8689.439 ops/ms +# Warmup Iteration 3: 10481.773 ops/ms +Iteration 1: 10665.623 ops/ms +Iteration 2: 11545.819 ops/ms +Iteration 3: 15453.809 ops/ms +Iteration 4: 12528.468 ops/ms +Iteration 5: 11771.402 ops/ms +Iteration 6: 15525.850 ops/ms +Iteration 7: 15721.214 ops/ms +Iteration 8: 13907.483 ops/ms + +# Run progress: 61.90% complete, ETA 00:01:38 +# Fork: 2 of 3 +WARNING: A terminally deprecated method in sun.misc.Unsafe has been called +WARNING: sun.misc.Unsafe::objectFieldOffset has been called by org.openjdk.jmh.util.Utils (file:/tmp/work/java-core-examples/bean-mapping/target/lib/jmh-core-1.37.jar) +WARNING: Please consider reporting this to the maintainers of class org.openjdk.jmh.util.Utils +WARNING: sun.misc.Unsafe::objectFieldOffset will be removed in a future release +# Warmup Iteration 1: 8709.272 ops/ms +# Warmup Iteration 2: 13926.631 ops/ms +# Warmup Iteration 3: 17914.073 ops/ms +Iteration 1: 15010.272 ops/ms +Iteration 2: 16785.843 ops/ms +Iteration 3: 19657.445 ops/ms +Iteration 4: 18834.587 ops/ms +Iteration 5: 19061.988 ops/ms +Iteration 6: 12487.744 ops/ms +Iteration 7: 4408.159 ops/ms +Iteration 8: 8350.435 ops/ms + +# Run progress: 66.67% complete, ETA 00:01:26 +# Fork: 3 of 3 +WARNING: A terminally deprecated method in sun.misc.Unsafe has been called +WARNING: sun.misc.Unsafe::objectFieldOffset has been called by org.openjdk.jmh.util.Utils (file:/tmp/work/java-core-examples/bean-mapping/target/lib/jmh-core-1.37.jar) +WARNING: Please consider reporting this to the maintainers of class org.openjdk.jmh.util.Utils +WARNING: sun.misc.Unsafe::objectFieldOffset will be removed in a future release +# Warmup Iteration 1: 6878.772 ops/ms +# Warmup Iteration 2: 5410.401 ops/ms +# Warmup Iteration 3: 10296.224 ops/ms +Iteration 1: 8712.362 ops/ms +Iteration 2: 15450.291 ops/ms +Iteration 3: 7158.292 ops/ms +Iteration 4: 9385.494 ops/ms +Iteration 5: 4466.870 ops/ms +Iteration 6: 8184.311 ops/ms +Iteration 7: 10496.601 ops/ms +Iteration 8: 11980.080 ops/ms + + +Result "com.ankurm.beanmapping.bench.MappingBenchmark.manualNested": + 12397.935 ±(99.9%) 3307.099 ops/ms [Average] + (min, avg, max) = (4408.159, 12397.935, 19657.445), stdev = 4300.163 + CI (99.9%): [9090.836, 15705.034] (assumes normal distribution) + + +# JMH version: 1.37 +# VM version: JDK 25.0.5.1, OpenJDK 64-Bit Server VM, 25.0.5.1+2-LTS +# VM invoker: /tmp/tools/jdk-25.0.5.1+2/bin/java +# VM options: -Djavax.net.ssl.trustStore=/etc/ssl/certs/java/cacerts -Djavax.net.ssl.trustStoreType=JKS -Dhttps.proxyHost=127.0.0.1 -Dhttps.proxyPort=41629 -Dhttp.nonProxyHosts=localhost|127.0.0.1|::1|127.*|0.*|::|169.254.*|api.anthropic.com|api-staging.anthropic.com|api-pr-preview.anthropic.com|mcp-proxy.anthropic.com|mcp-proxy-staging.anthropic.com|registry.npmjs.org|jsr.io|npm.jsr.io|pypi.org|files.pythonhosted.org|index.crates.io|proxy.golang.org|host.docker.internal|10.*|172.16.*|172.17.*|172.18.*|172.19.*|172.20.*|172.21.*|172.22.*|172.23.*|172.24.*|172.25.*|172.26.*|172.27.*|172.28.*|172.29.*|172.30.*|172.31.*|192.168.*|100.64.0.0/10|*.svc.cluster.local|*.svc.cluster.local -Djdk.http.auth.tunneling.disabledSchemes= -Djdk.http.auth.proxying.disabledSchemes= -XX:+UnlockDiagnosticVMOptions -XX:DisableIntrinsic=_base64_encodeBlock,_base64_decodeBlock +# Blackhole mode: compiler (auto-detected, use -Djmh.blackhole.autoDetect=false to disable) +# Warmup: 3 iterations, 1 s each +# Measurement: 8 iterations, 1 s each +# Timeout: 10 min per iteration +# Threads: 1 thread, will synchronize iterations +# Benchmark mode: Throughput, ops/time +# Benchmark: com.ankurm.beanmapping.bench.MappingBenchmark.mapstructFlat + +# Run progress: 71.43% complete, ETA 00:01:14 +# Fork: 1 of 3 +WARNING: A terminally deprecated method in sun.misc.Unsafe has been called +WARNING: sun.misc.Unsafe::objectFieldOffset has been called by org.openjdk.jmh.util.Utils (file:/tmp/work/java-core-examples/bean-mapping/target/lib/jmh-core-1.37.jar) +WARNING: Please consider reporting this to the maintainers of class org.openjdk.jmh.util.Utils +WARNING: sun.misc.Unsafe::objectFieldOffset will be removed in a future release +# Warmup Iteration 1: 15709.227 ops/ms +# Warmup Iteration 2: 28973.008 ops/ms +# Warmup Iteration 3: 8540.419 ops/ms +Iteration 1: 15873.047 ops/ms +Iteration 2: 21228.626 ops/ms +Iteration 3: 22082.510 ops/ms +Iteration 4: 18314.139 ops/ms +Iteration 5: 30257.617 ops/ms +Iteration 6: 33625.707 ops/ms +Iteration 7: 38672.523 ops/ms +Iteration 8: 35243.923 ops/ms + +# Run progress: 76.19% complete, ETA 00:01:02 +# Fork: 2 of 3 +WARNING: A terminally deprecated method in sun.misc.Unsafe has been called +WARNING: sun.misc.Unsafe::objectFieldOffset has been called by org.openjdk.jmh.util.Utils (file:/tmp/work/java-core-examples/bean-mapping/target/lib/jmh-core-1.37.jar) +WARNING: Please consider reporting this to the maintainers of class org.openjdk.jmh.util.Utils +WARNING: sun.misc.Unsafe::objectFieldOffset will be removed in a future release +# Warmup Iteration 1: 16509.230 ops/ms +# Warmup Iteration 2: 32734.248 ops/ms +# Warmup Iteration 3: 18962.288 ops/ms +Iteration 1: 21795.334 ops/ms +Iteration 2: 27627.533 ops/ms +Iteration 3: 39797.208 ops/ms +Iteration 4: 36114.309 ops/ms +Iteration 5: 24839.657 ops/ms +Iteration 6: 43904.975 ops/ms +Iteration 7: 26649.670 ops/ms +Iteration 8: 17581.402 ops/ms + +# Run progress: 80.95% complete, ETA 00:00:49 +# Fork: 3 of 3 +WARNING: A terminally deprecated method in sun.misc.Unsafe has been called +WARNING: sun.misc.Unsafe::objectFieldOffset has been called by org.openjdk.jmh.util.Utils (file:/tmp/work/java-core-examples/bean-mapping/target/lib/jmh-core-1.37.jar) +WARNING: Please consider reporting this to the maintainers of class org.openjdk.jmh.util.Utils +WARNING: sun.misc.Unsafe::objectFieldOffset will be removed in a future release +# Warmup Iteration 1: 14464.623 ops/ms +# Warmup Iteration 2: 25986.606 ops/ms +# Warmup Iteration 3: 32261.067 ops/ms +Iteration 1: 37464.802 ops/ms +Iteration 2: 46349.477 ops/ms +Iteration 3: 36049.756 ops/ms +Iteration 4: 37930.341 ops/ms +Iteration 5: 30043.247 ops/ms +Iteration 6: 34356.960 ops/ms +Iteration 7: 35651.345 ops/ms +Iteration 8: 34860.012 ops/ms + + +Result "com.ankurm.beanmapping.bench.MappingBenchmark.mapstructFlat": + 31096.422 ±(99.9%) 6517.097 ops/ms [Average] + (min, avg, max) = (15873.047, 31096.422, 46349.477), stdev = 8474.067 + CI (99.9%): [24579.325, 37613.519] (assumes normal distribution) + + +# JMH version: 1.37 +# VM version: JDK 25.0.5.1, OpenJDK 64-Bit Server VM, 25.0.5.1+2-LTS +# VM invoker: /tmp/tools/jdk-25.0.5.1+2/bin/java +# VM options: -Djavax.net.ssl.trustStore=/etc/ssl/certs/java/cacerts -Djavax.net.ssl.trustStoreType=JKS -Dhttps.proxyHost=127.0.0.1 -Dhttps.proxyPort=41629 -Dhttp.nonProxyHosts=localhost|127.0.0.1|::1|127.*|0.*|::|169.254.*|api.anthropic.com|api-staging.anthropic.com|api-pr-preview.anthropic.com|mcp-proxy.anthropic.com|mcp-proxy-staging.anthropic.com|registry.npmjs.org|jsr.io|npm.jsr.io|pypi.org|files.pythonhosted.org|index.crates.io|proxy.golang.org|host.docker.internal|10.*|172.16.*|172.17.*|172.18.*|172.19.*|172.20.*|172.21.*|172.22.*|172.23.*|172.24.*|172.25.*|172.26.*|172.27.*|172.28.*|172.29.*|172.30.*|172.31.*|192.168.*|100.64.0.0/10|*.svc.cluster.local|*.svc.cluster.local -Djdk.http.auth.tunneling.disabledSchemes= -Djdk.http.auth.proxying.disabledSchemes= -XX:+UnlockDiagnosticVMOptions -XX:DisableIntrinsic=_base64_encodeBlock,_base64_decodeBlock +# Blackhole mode: compiler (auto-detected, use -Djmh.blackhole.autoDetect=false to disable) +# Warmup: 3 iterations, 1 s each +# Measurement: 8 iterations, 1 s each +# Timeout: 10 min per iteration +# Threads: 1 thread, will synchronize iterations +# Benchmark mode: Throughput, ops/time +# Benchmark: com.ankurm.beanmapping.bench.MappingBenchmark.mapstructNested + +# Run progress: 85.71% complete, ETA 00:00:37 +# Fork: 1 of 3 +WARNING: A terminally deprecated method in sun.misc.Unsafe has been called +WARNING: sun.misc.Unsafe::objectFieldOffset has been called by org.openjdk.jmh.util.Utils (file:/tmp/work/java-core-examples/bean-mapping/target/lib/jmh-core-1.37.jar) +WARNING: Please consider reporting this to the maintainers of class org.openjdk.jmh.util.Utils +WARNING: sun.misc.Unsafe::objectFieldOffset will be removed in a future release +# Warmup Iteration 1: 6811.175 ops/ms +# Warmup Iteration 2: 17851.168 ops/ms +# Warmup Iteration 3: 16877.612 ops/ms +Iteration 1: 19494.371 ops/ms +Iteration 2: 16749.274 ops/ms +Iteration 3: 18860.277 ops/ms +Iteration 4: 20069.090 ops/ms +Iteration 5: 19614.201 ops/ms +Iteration 6: 18657.196 ops/ms +Iteration 7: 16197.474 ops/ms +Iteration 8: 17816.949 ops/ms + +# Run progress: 90.48% complete, ETA 00:00:24 +# Fork: 2 of 3 +WARNING: A terminally deprecated method in sun.misc.Unsafe has been called +WARNING: sun.misc.Unsafe::objectFieldOffset has been called by org.openjdk.jmh.util.Utils (file:/tmp/work/java-core-examples/bean-mapping/target/lib/jmh-core-1.37.jar) +WARNING: Please consider reporting this to the maintainers of class org.openjdk.jmh.util.Utils +WARNING: sun.misc.Unsafe::objectFieldOffset will be removed in a future release +# Warmup Iteration 1: 3070.643 ops/ms +# Warmup Iteration 2: 4318.426 ops/ms +# Warmup Iteration 3: 2286.492 ops/ms +Iteration 1: 5234.957 ops/ms +Iteration 2: 17848.745 ops/ms +Iteration 3: 11217.632 ops/ms +Iteration 4: 16602.759 ops/ms +Iteration 5: 14791.716 ops/ms +Iteration 6: 15380.682 ops/ms +Iteration 7: 13429.602 ops/ms +Iteration 8: 12463.310 ops/ms + +# Run progress: 95.24% complete, ETA 00:00:12 +# Fork: 3 of 3 +WARNING: A terminally deprecated method in sun.misc.Unsafe has been called +WARNING: sun.misc.Unsafe::objectFieldOffset has been called by org.openjdk.jmh.util.Utils (file:/tmp/work/java-core-examples/bean-mapping/target/lib/jmh-core-1.37.jar) +WARNING: Please consider reporting this to the maintainers of class org.openjdk.jmh.util.Utils +WARNING: sun.misc.Unsafe::objectFieldOffset will be removed in a future release +# Warmup Iteration 1: 9593.683 ops/ms +# Warmup Iteration 2: 6820.043 ops/ms +# Warmup Iteration 3: 10792.027 ops/ms +Iteration 1: 11548.354 ops/ms +Iteration 2: 16361.332 ops/ms +Iteration 3: 13394.408 ops/ms +Iteration 4: 15554.883 ops/ms +Iteration 5: 19775.933 ops/ms +Iteration 6: 18552.136 ops/ms +Iteration 7: 20400.900 ops/ms +Iteration 8: 18912.117 ops/ms + + +Result "com.ankurm.beanmapping.bench.MappingBenchmark.mapstructNested": + 16205.346 ±(99.9%) 2772.961 ops/ms [Average] + (min, avg, max) = (5234.957, 16205.346, 20400.900), stdev = 3605.633 + CI (99.9%): [13432.385, 18978.307] (assumes normal distribution) + + +# Run complete. Total time: 00:04:20 + +REMEMBER: The numbers below are just data. To gain reusable insights, you need to follow up on +why the numbers are the way they are. Use profilers (see -prof, -lprof), design factorial +experiments, perform baseline and negative tests that provide experimental control, make sure +the benchmarking environment is safe on JVM/OS/HW level, ask for reviews from the domain experts. +Do not assume the numbers tell you what you want them to tell. + +NOTE: Current JVM experimentally supports Compiler Blackholes, and they are in use. Please exercise +extra caution when trusting the results, look into the generated code to check the benchmark still +works, and factor in a small probability of new VM bugs. Additionally, while comparisons between +different JVMs are already problematic, the performance difference caused by different Blackhole +modes can be very significant. Please make sure you use the consistent Blackhole mode for comparisons. + +Benchmark Mode Cnt Score Error Units +MappingBenchmark.dozerFlat thrpt 24 221.151 ± 83.377 ops/ms +MappingBenchmark.dozerNested thrpt 24 159.813 ± 42.013 ops/ms +MappingBenchmark.dozerNewMapperPerCallFlat thrpt 24 9.221 ± 4.064 ops/ms +MappingBenchmark.manualFlat thrpt 24 36658.509 ± 11549.812 ops/ms +MappingBenchmark.manualNested thrpt 24 12397.935 ± 3307.099 ops/ms +MappingBenchmark.mapstructFlat thrpt 24 31096.422 ± 6517.097 ops/ms +MappingBenchmark.mapstructNested thrpt 24 16205.346 ± 2772.961 ops/ms + +Benchmark result is saved to output/07-benchmark.json diff --git a/bean-mapping/pom.xml b/bean-mapping/pom.xml new file mode 100644 index 0000000..316f364 --- /dev/null +++ b/bean-mapping/pom.xml @@ -0,0 +1,113 @@ + + + 4.0.0 + + + com.ankurm + java-core-examples + 1.0 + + + bean-mapping + bean-mapping + Dozer (reflection) vs MapStruct (compile-time) bean mapping: runnable examples, failure modes, a migration parity test and a JMH benchmark. + + + 7.0.0 + 1.6.3 + 1.37 + + + + + com.github.dozermapper + dozer-core + ${dozer.version} + + + org.mapstruct + mapstruct + ${mapstruct.version} + + + org.openjdk.jmh + jmh-core + ${jmh.version} + + + + org.slf4j + slf4j-nop + 2.0.17 + + + org.junit.jupiter + junit-jupiter + 5.11.0 + test + + + + + + + org.apache.maven.plugins + maven-compiler-plugin + 3.13.0 + + 25 + + + org.mapstruct + mapstruct-processor + ${mapstruct.version} + + + org.openjdk.jmh + jmh-generator-annprocess + ${jmh.version} + + + + + + org.apache.maven.plugins + maven-surefire-plugin + 3.2.5 + + + org.apache.maven.plugins + maven-dependency-plugin + 3.8.1 + + + copy-runtime-libs + package + copy-dependencies + + ${project.build.directory}/lib + runtime + + + + copy-processor + package + copy + + ${project.build.directory}/processor + + + org.mapstruct + mapstruct-processor + ${mapstruct.version} + + + + + + + + + diff --git a/bean-mapping/scripts/compile-broken.sh b/bean-mapping/scripts/compile-broken.sh new file mode 100755 index 0000000..3ab756e --- /dev/null +++ b/bean-mapping/scripts/compile-broken.sh @@ -0,0 +1,15 @@ +#!/usr/bin/env bash +# Compiles each file in src/broken/ (outside the Maven build) and prints the compiler's own message. +# Needs: mvn package already run (target/lib, target/processor) and JAVA_HOME or JDK25_HOME. +set -uo pipefail +cd "$(dirname "$0")/.." +JAVAC="${JDK25_HOME:+$JDK25_HOME/bin/}javac" +for f in src/broken/java/*.java; do + OUTDIR=$(mktemp -d) + echo "\$ javac -processorpath target/processor/mapstruct-processor-*.jar -cp target/classes:target/lib/mapstruct-*.jar $f" + "$JAVAC" -d "$OUTDIR" -proc:full -processorpath "$(ls target/processor/mapstruct-processor-*.jar)" \ + -cp "target/classes:$(ls target/lib/mapstruct-1*.jar)" "$f" 2>&1 | grep -v "Picked up" + echo "javac exit code: ${PIPESTATUS[0]}" + echo + rm -rf "$OUTDIR" +done diff --git a/bean-mapping/scripts/run-all.sh b/bean-mapping/scripts/run-all.sh new file mode 100755 index 0000000..6a3a33a --- /dev/null +++ b/bean-mapping/scripts/run-all.sh @@ -0,0 +1,27 @@ +#!/usr/bin/env bash +# Regenerates every file in ../output/. Requires JDK25_HOME. Linux/macOS only. +set -euo pipefail +[[ -z "${JDK25_HOME:-}" ]] && { echo "JDK25_HOME must be set" >&2; exit 1; } +cd "$(dirname "$0")/.." +OUT=output; mkdir -p "$OUT" +export JAVA_HOME="$JDK25_HOME" +mvn -q -f ../pom.xml -pl bean-mapping -am package +J="$JDK25_HOME/bin/java" +CP="target/classes:target/lib/*" +run() { echo "==> $1"; "$J" -cp "$CP" "$1" 2>&1 | grep -v "Picked up" > "$OUT/$2"; } +{ echo '$ java -version'; "$J" -version 2>&1 | grep -v "Picked up"; echo + echo '$ uname -sr'; uname -sr + echo '$ nproc'; nproc + echo '$ grep -m1 "model name" /proc/cpuinfo'; grep -m1 "model name" /proc/cpuinfo || true + echo; echo 'Dozer 7.0.0, MapStruct 1.6.3, JMH 1.37'; } > "$OUT/00-environment.txt" +run com.ankurm.beanmapping.dozer.DozerTour 01-dozer-tour.txt +run com.ankurm.beanmapping.dozer.DozerPitfalls 02-dozer-pitfalls.txt +run com.ankurm.beanmapping.mapstruct.MapStructTour 03-mapstruct-tour.txt +cp "$(find target/generated-sources -name UserMapperImpl.java)" "$OUT/04-UserMapperImpl.java.txt" +./scripts/compile-broken.sh > "$OUT/05-broken-compile.txt" 2>&1 || true +cp target/surefire-reports/com.ankurm.beanmapping.BeanMappingClaimsTest.txt "$OUT/06-tests.txt" +if [[ -z "${SKIP_BENCHMARK:-}" ]]; then + echo "==> benchmark (about four minutes; set SKIP_BENCHMARK=1 to skip)" + ./scripts/run-benchmark.sh | tee "$OUT/07-benchmark.txt" > /dev/null +fi +echo Done diff --git a/bean-mapping/scripts/run-benchmark.sh b/bean-mapping/scripts/run-benchmark.sh new file mode 100755 index 0000000..1c16f76 --- /dev/null +++ b/bean-mapping/scripts/run-benchmark.sh @@ -0,0 +1,9 @@ +#!/usr/bin/env bash +# Runs the JMH benchmark. Defaults: 3 forks, 3 x 1s warmup, 8 x 1s measurement. Takes about four minutes. +# Usage: ./scripts/run-benchmark.sh [extra JMH args] +set -euo pipefail +cd "$(dirname "$0")/.." +J="${JDK25_HOME:+$JDK25_HOME/bin/}java" +mkdir -p output +"$J" -cp "target/classes:target/lib/*" org.openjdk.jmh.Main MappingBenchmark \ + -f 3 -wi 3 -w 1s -i 8 -r 1s -rf json -rff output/07-benchmark.json "$@" 2>&1 | grep -v "Picked up" diff --git a/bean-mapping/src/broken/java/BrokenInverseMapper.java b/bean-mapping/src/broken/java/BrokenInverseMapper.java new file mode 100644 index 0000000..7e0c844 --- /dev/null +++ b/bean-mapping/src/broken/java/BrokenInverseMapper.java @@ -0,0 +1,31 @@ +import com.ankurm.beanmapping.model.TagEntity; +import com.ankurm.beanmapping.model.UserDTO; +import com.ankurm.beanmapping.model.UserEntity; +import org.mapstruct.BeanMapping; +import org.mapstruct.InheritInverseConfiguration; +import org.mapstruct.Mapper; +import org.mapstruct.Mapping; +import org.mapstruct.MappingTarget; +import org.mapstruct.NullValuePropertyMappingStrategy; + +/** + * Deliberately does not compile. The first attempt at UserMapper: @InheritInverseConfiguration without a name + * is fine until an update method with the same types exists, because then two methods look like the inverse. + * Outside the Maven build; driven by scripts/compile-broken.sh. + */ +@Mapper +public interface BrokenInverseMapper { + @Mapping(source = "email", target = "emailAddress") + UserEntity toEntity(UserDTO dto); + + @InheritInverseConfiguration + UserDTO toDto(UserEntity entity); + + @BeanMapping(nullValuePropertyMappingStrategy = NullValuePropertyMappingStrategy.IGNORE) + @Mapping(source = "email", target = "emailAddress") + void update(@MappingTarget UserEntity target, UserDTO source); + + default TagEntity toTag(String name) { return name == null ? null : new TagEntity(name); } + + default String fromTag(TagEntity tag) { return tag == null ? null : tag.getName(); } +} diff --git a/bean-mapping/src/broken/java/BrokenUserMapper.java b/bean-mapping/src/broken/java/BrokenUserMapper.java new file mode 100644 index 0000000..c0be19e --- /dev/null +++ b/bean-mapping/src/broken/java/BrokenUserMapper.java @@ -0,0 +1,15 @@ +import com.ankurm.beanmapping.model.UserDTO; +import com.ankurm.beanmapping.model.UserEntityV2; +import org.mapstruct.Mapper; +import org.mapstruct.Mapping; +import org.mapstruct.ReportingPolicy; + +/** + * Deliberately does not compile. UserEntityV2 gained a "phone" column; nothing maps it. + * This directory is not part of the Maven build: scripts/compile-broken.sh compiles it and captures the message. + */ +@Mapper(unmappedTargetPolicy = ReportingPolicy.ERROR) +public interface BrokenUserMapper { + @Mapping(source = "email", target = "emailAddress") + UserEntityV2 toEntity(UserDTO dto); +} diff --git a/bean-mapping/src/main/java/com/ankurm/beanmapping/ManualMapper.java b/bean-mapping/src/main/java/com/ankurm/beanmapping/ManualMapper.java new file mode 100644 index 0000000..6b7e191 --- /dev/null +++ b/bean-mapping/src/main/java/com/ankurm/beanmapping/ManualMapper.java @@ -0,0 +1,45 @@ +package com.ankurm.beanmapping; + +import com.ankurm.beanmapping.model.*; + +import java.util.ArrayList; +import java.util.List; + +/** The baseline every library is measured against: hand-written getter/setter copying. */ +public final class ManualMapper { + private ManualMapper() { } + + public static UserEntity toEntity(UserDTO d) { + if (d == null) return null; + UserEntity e = new UserEntity(); + e.setId(d.getId()); + e.setUsername(d.getUsername()); + e.setEmailAddress(d.getEmail()); + if (d.getProfile() != null) { + e.setProfile(new ProfileEntity(d.getProfile().getFirstName(), d.getProfile().getLastName())); + } + if (d.getTags() != null) { + List tags = new ArrayList<>(d.getTags().size()); + for (String t : d.getTags()) tags.add(new TagEntity(t)); + e.setTags(tags); + } + return e; + } + + public static UserDTO toDto(UserEntity e) { + if (e == null) return null; + UserDTO d = new UserDTO(); + d.setId(e.getId()); + d.setUsername(e.getUsername()); + d.setEmail(e.getEmailAddress()); + if (e.getProfile() != null) { + d.setProfile(new ProfileDTO(e.getProfile().getFirstName(), e.getProfile().getLastName())); + } + if (e.getTags() != null) { + List tags = new ArrayList<>(e.getTags().size()); + for (TagEntity t : e.getTags()) tags.add(t.getName()); + d.setTags(tags); + } + return d; + } +} diff --git a/bean-mapping/src/main/java/com/ankurm/beanmapping/bench/MappingBenchmark.java b/bean-mapping/src/main/java/com/ankurm/beanmapping/bench/MappingBenchmark.java new file mode 100644 index 0000000..ccfc4d1 --- /dev/null +++ b/bean-mapping/src/main/java/com/ankurm/beanmapping/bench/MappingBenchmark.java @@ -0,0 +1,47 @@ +package com.ankurm.beanmapping.bench; + +import com.ankurm.beanmapping.ManualMapper; +import com.ankurm.beanmapping.dozer.DozerSetup; +import com.ankurm.beanmapping.mapstruct.UserMapper; +import com.ankurm.beanmapping.model.Samples; +import com.ankurm.beanmapping.model.UserDTO; +import com.ankurm.beanmapping.model.UserEntity; +import com.github.dozermapper.core.Mapper; +import org.openjdk.jmh.annotations.*; + +import java.util.concurrent.TimeUnit; + +/** + * Manual vs MapStruct vs Dozer, on a flat object (3 fields) and a nested one (profile + 5 tags). + * Inputs are never mutated and results are returned, so JMH's Blackhole consumes them. + */ +@State(Scope.Benchmark) +@BenchmarkMode(Mode.Throughput) +@OutputTimeUnit(TimeUnit.MILLISECONDS) +public class MappingBenchmark { + + UserDTO flat; + UserDTO nested; + Mapper dozer; + UserMapper mapstruct; + + @Setup + public void setup() { + flat = Samples.flat(); + nested = Samples.nested(); + dozer = DozerSetup.standard(); + mapstruct = UserMapper.INSTANCE; + // Dozer parses its class metadata lazily on first use; warmup iterations cover that cost. + } + + @Benchmark public UserEntity manualFlat() { return ManualMapper.toEntity(flat); } + @Benchmark public UserEntity mapstructFlat() { return mapstruct.toEntity(flat); } + @Benchmark public UserEntity dozerFlat() { return dozer.map(flat, UserEntity.class); } + + @Benchmark public UserEntity manualNested() { return ManualMapper.toEntity(nested); } + @Benchmark public UserEntity mapstructNested() { return mapstruct.toEntity(nested); } + @Benchmark public UserEntity dozerNested() { return dozer.map(nested, UserEntity.class); } + + /** The antipattern from the post: a new Mapper for every call. */ + @Benchmark public UserEntity dozerNewMapperPerCallFlat() { return DozerSetup.standard().map(flat, UserEntity.class); } +} diff --git a/bean-mapping/src/main/java/com/ankurm/beanmapping/dozer/DozerPitfalls.java b/bean-mapping/src/main/java/com/ankurm/beanmapping/dozer/DozerPitfalls.java new file mode 100644 index 0000000..90bcac3 --- /dev/null +++ b/bean-mapping/src/main/java/com/ankurm/beanmapping/dozer/DozerPitfalls.java @@ -0,0 +1,109 @@ +package com.ankurm.beanmapping.dozer; + +import com.ankurm.beanmapping.model.*; +import com.github.dozermapper.core.DozerBeanMapperBuilder; +import com.github.dozermapper.core.Mapper; +import com.github.dozermapper.core.loader.api.BeanMappingBuilder; + +/** What goes wrong with a reflection mapper, and when you find out. */ +public class DozerPitfalls { + + public static void main(String[] args) { + System.out.println("== 1. A typo in a field name: when does it fail? (not at compile time)"); + BeanMappingBuilder typo = new BeanMappingBuilder() { + @Override + protected void configure() { + mapping(UserDTO.class, UserEntity.class).fields("emial", "emailAddress"); + } + }; + Mapper typoMapper = null; + try { + typoMapper = DozerBeanMapperBuilder.create().withMappingBuilder(typo).build(); + System.out.println("mapper built without complaint"); + } catch (Exception e) { + System.out.println("failed while building the mapper: " + describe(e)); + } + if (typoMapper != null) { + try { + typoMapper.map(Samples.flat(), UserEntity.class); + System.out.println("map() succeeded"); + } catch (Exception e) { + System.out.println("failed on the first map() call: " + describe(e)); + } + } + + System.out.println(); + System.out.println("== 2. A field nobody maps: email -> emailAddress without the configuration line"); + UserEntity silent = DozerSetup.conventionOnly().map(Samples.flat(), UserEntity.class); + System.out.println("no exception, no log line. emailAddress=" + silent.getEmailAddress()); + + System.out.println(); + System.out.println("== 3. A new column on the entity (phone): does Dozer notice?"); + UserEntityV2 v2 = DozerSetup.standard().map(Samples.flat(), UserEntityV2.class); + System.out.println("no exception, no log line. " + v2); + + System.out.println(); + System.out.println("== 4. Records: as destination, then as source"); + try { + UserRecord r = DozerSetup.standard().map(Samples.flat(), UserRecord.class); + System.out.println("record destination worked: " + r); + } catch (Exception e) { + System.out.println("record destination failed: " + describe(e)); + } + try { + UserDTO fromRecord = DozerSetup.standard().map(new UserRecord(7L, "rec"), UserDTO.class); + System.out.println("record source: no exception, but look at the result: " + fromRecord); + } catch (Exception e) { + System.out.println("record source failed: " + describe(e)); + } + + System.out.println(); + System.out.println("== 5. A converter registered globally, applied to a list, entity -> DTO"); + UserEntity withTags = DozerSetup.standard().map(Samples.nested(), UserEntity.class); + UserDTO wrong = DozerSetup.globalConverterOnly().map(withTags, UserDTO.class); + System.out.println("DTO to entity worked: " + DozerSetup.globalConverterOnly().map(Samples.nested(), UserEntity.class).getTags()); + System.out.println("entity to DTO, no exception: tags=" + wrong.getTags()); + System.out.println("convertFrom() was never used: the elements are TagEntity.toString() output"); + UserDTO right = DozerSetup.standard().map(withTags, UserDTO.class); + System.out.println("with a field-level converter: tags=" + right.getTags()); + + System.out.println(); + System.out.println("== 6. A lazy getter: does Dozer call it?"); + LazyUser lazy = new LazyUser(); + lazy.setId(5L); + lazy.setName("lazy"); + try { + DozerSetup.conventionOnly().map(lazy, LazyUserView.class); + System.out.println("map() succeeded"); + } catch (Exception e) { + System.out.println("map() failed: " + describe(e)); + } + System.out.println("getOrders() was called " + lazy.orderReads() + " time(s)"); + + System.out.println(); + System.out.println("== 7. Same kind of entity, orders excluded in the mapping"); + BeanMappingBuilder exclude = new BeanMappingBuilder() { + @Override + protected void configure() { + mapping(LazyUser.class, LazyUserView.class).exclude("orders"); + } + }; + Mapper excluding = DozerBeanMapperBuilder.create().withMappingBuilder(exclude).build(); + LazyUser lazy2 = new LazyUser(); + lazy2.setId(6L); + lazy2.setName("lazy2"); + LazyUserView view = excluding.map(lazy2, LazyUserView.class); + System.out.println("map() succeeded: " + view); + System.out.println("getOrders() was called " + lazy2.orderReads() + " time(s)"); + } + + /** Exception class and first line of the message, so the transcript stays short and stable. */ + static String describe(Throwable t) { + Throwable root = t; + while (root.getCause() != null && root.getCause() != root) root = root.getCause(); + String first = String.valueOf(t.getMessage()).lines().findFirst().orElse(""); + String rootLine = String.valueOf(root.getMessage()).lines().findFirst().orElse(""); + return t.getClass().getSimpleName() + ": " + first + + (root != t ? " [root cause " + root.getClass().getSimpleName() + ": " + rootLine + "]" : ""); + } +} diff --git a/bean-mapping/src/main/java/com/ankurm/beanmapping/dozer/DozerSetup.java b/bean-mapping/src/main/java/com/ankurm/beanmapping/dozer/DozerSetup.java new file mode 100644 index 0000000..6750752 --- /dev/null +++ b/bean-mapping/src/main/java/com/ankurm/beanmapping/dozer/DozerSetup.java @@ -0,0 +1,63 @@ +package com.ankurm.beanmapping.dozer; + +import com.ankurm.beanmapping.model.UserDTO; +import com.ankurm.beanmapping.model.UserEntity; +import com.github.dozermapper.core.DozerBeanMapperBuilder; +import com.github.dozermapper.core.Mapper; +import com.github.dozermapper.core.loader.api.BeanMappingBuilder; +import com.github.dozermapper.core.loader.api.FieldsMappingOptions; +import com.github.dozermapper.core.loader.api.TypeMappingOptions; + +/** Every way this module builds a Dozer {@link Mapper}. Build once, reuse: see the benchmark for why. */ +public final class DozerSetup { + private DozerSetup() { } + + /** + * The configuration used everywhere unless a demo says otherwise: the email rename, plus a field-level + * converter for the tag list. The field-level converter is what makes entity to DTO work; + * see {@link #globalConverterOnly()} for what happens without it. + */ + public static Mapper standard() { + BeanMappingBuilder builder = new BeanMappingBuilder() { + @Override + protected void configure() { + mapping(UserDTO.class, UserEntity.class) + .fields("email", "emailAddress") + .fields("tags", "tags", FieldsMappingOptions.customConverter(TagListConverter.class)); + } + }; + return DozerBeanMapperBuilder.create().withMappingBuilder(builder).build(); + } + + /** The version most tutorials show: a global {@link StringToTagConverter} and nothing on the field. */ + public static Mapper globalConverterOnly() { + BeanMappingBuilder builder = new BeanMappingBuilder() { + @Override + protected void configure() { + mapping(UserDTO.class, UserEntity.class).fields("email", "emailAddress"); + } + }; + return DozerBeanMapperBuilder.create() + .withMappingBuilder(builder) + .withCustomConverter(new StringToTagConverter()) + .build(); + } + + /** Same as {@link #standard()}, but a null source value does not overwrite the destination (partial updates). */ + public static Mapper skippingNulls() { + BeanMappingBuilder builder = new BeanMappingBuilder() { + @Override + protected void configure() { + mapping(UserDTO.class, UserEntity.class, TypeMappingOptions.mapNull(false)) + .fields("email", "emailAddress") + .fields("tags", "tags", FieldsMappingOptions.customConverter(TagListConverter.class)); + } + }; + return DozerBeanMapperBuilder.create().withMappingBuilder(builder).build(); + } + + /** No mapping builder at all: only same-named fields are copied. */ + public static Mapper conventionOnly() { + return DozerBeanMapperBuilder.buildDefault(); + } +} diff --git a/bean-mapping/src/main/java/com/ankurm/beanmapping/dozer/DozerTour.java b/bean-mapping/src/main/java/com/ankurm/beanmapping/dozer/DozerTour.java new file mode 100644 index 0000000..378ec8d --- /dev/null +++ b/bean-mapping/src/main/java/com/ankurm/beanmapping/dozer/DozerTour.java @@ -0,0 +1,56 @@ +package com.ankurm.beanmapping.dozer; + +import com.ankurm.beanmapping.model.*; +import com.github.dozermapper.core.Mapper; + +/** The happy path: basic, nested, collection, bidirectional, null handling, partial update. */ +public class DozerTour { + + public static void main(String[] args) { + Mapper mapper = DozerSetup.standard(); + + System.out.println("== 1. Basic mapping: id and username by name, email -> emailAddress by configuration"); + UserDTO dto = Samples.flat(); + UserEntity entity = mapper.map(dto, UserEntity.class); + System.out.println("ID: " + entity.getId()); + System.out.println("User: " + entity.getUsername()); + System.out.println("Email: " + entity.getEmailAddress()); + + System.out.println(); + System.out.println("== 2. Nested object: no configuration at all"); + UserDTO nested = new UserDTO(); + nested.setUsername("jdoe"); + nested.setProfile(new ProfileDTO("John", "Doe")); + UserEntity nestedEntity = mapper.map(nested, UserEntity.class); + System.out.println("profile class: " + nestedEntity.getProfile().getClass().getSimpleName()); + System.out.println("first name: " + nestedEntity.getProfile().getFirstName()); + + System.out.println(); + System.out.println("== 3. Collection: List -> List through the custom converter"); + UserDTO tagged = new UserDTO(); + tagged.setTags(java.util.Arrays.asList("Java", "Spring Boot", "Dozer")); + // the tutorial setup: StringToTagConverter registered globally. DTO -> entity works; the reverse trap is in DozerPitfalls. + UserEntity taggedEntity = DozerSetup.globalConverterOnly().map(tagged, UserEntity.class); + taggedEntity.getTags().forEach(tag -> System.out.println(tag.getName())); + + System.out.println(); + System.out.println("== 4. Bidirectional: the same configuration maps the entity back"); + UserDTO back = mapper.map(mapper.map(Samples.nested(), UserEntity.class), UserDTO.class); + System.out.println("round trip equals original: " + back.equals(Samples.nested())); + System.out.println(back); + + System.out.println(); + System.out.println("== 5. Null handling: default Dozer copies null over an existing value"); + UserEntity existing = mapper.map(Samples.nested(), UserEntity.class); + mapper.map(Samples.withNulls(), existing); + System.out.println("after mapping a DTO with null email and null profile: emailAddress=" + existing.getEmailAddress() + + ", profile=" + existing.getProfile()); + + System.out.println(); + System.out.println("== 6. Null handling: TypeMappingOptions.mapNull(false) keeps the existing value"); + Mapper skipping = DozerSetup.skippingNulls(); + UserEntity kept = skipping.map(Samples.nested(), UserEntity.class); + skipping.map(Samples.withNulls(), kept); + System.out.println("after the same update: emailAddress=" + kept.getEmailAddress() + ", profile=" + kept.getProfile()); + } +} diff --git a/bean-mapping/src/main/java/com/ankurm/beanmapping/dozer/StringToTagConverter.java b/bean-mapping/src/main/java/com/ankurm/beanmapping/dozer/StringToTagConverter.java new file mode 100644 index 0000000..fdd2577 --- /dev/null +++ b/bean-mapping/src/main/java/com/ankurm/beanmapping/dozer/StringToTagConverter.java @@ -0,0 +1,25 @@ +package com.ankurm.beanmapping.dozer; + +import com.ankurm.beanmapping.model.TagEntity; +import com.github.dozermapper.core.DozerConverter; + +/** Dozer custom converter: String on one side, TagEntity on the other. */ +public class StringToTagConverter extends DozerConverter { + + public StringToTagConverter() { + super(String.class, TagEntity.class); + } + + @Override + public TagEntity convertTo(String source, TagEntity destination) { + if (source == null) return null; + TagEntity tag = new TagEntity(); + tag.setName(source); + return tag; + } + + @Override + public String convertFrom(TagEntity source, String destination) { + return source != null ? source.getName() : null; + } +} diff --git a/bean-mapping/src/main/java/com/ankurm/beanmapping/dozer/TagListConverter.java b/bean-mapping/src/main/java/com/ankurm/beanmapping/dozer/TagListConverter.java new file mode 100644 index 0000000..1336bd4 --- /dev/null +++ b/bean-mapping/src/main/java/com/ankurm/beanmapping/dozer/TagListConverter.java @@ -0,0 +1,28 @@ +package com.ankurm.beanmapping.dozer; + +import com.ankurm.beanmapping.model.TagEntity; +import com.github.dozermapper.core.CustomConverter; + +import java.util.ArrayList; +import java.util.List; + +/** + * Field-level converter for the {@code tags} list, used in both directions: Dozer calls the same + * {@code convert} method and the element type tells us which way we are going. + */ +public class TagListConverter implements CustomConverter { + + @Override + public Object convert(Object existingDestinationFieldValue, Object sourceFieldValue, + Class destinationClass, Class sourceClass) { + if (sourceFieldValue == null) return null; + List in = (List) sourceFieldValue; + List out = new ArrayList<>(in.size()); + for (Object element : in) { + if (element instanceof String name) out.add(new TagEntity(name)); + else if (element instanceof TagEntity tag) out.add(tag.getName()); + else throw new IllegalArgumentException("unexpected tag element: " + element); + } + return out; + } +} diff --git a/bean-mapping/src/main/java/com/ankurm/beanmapping/mapstruct/LazyMapper.java b/bean-mapping/src/main/java/com/ankurm/beanmapping/mapstruct/LazyMapper.java new file mode 100644 index 0000000..54c4579 --- /dev/null +++ b/bean-mapping/src/main/java/com/ankurm/beanmapping/mapstruct/LazyMapper.java @@ -0,0 +1,19 @@ +package com.ankurm.beanmapping.mapstruct; + +import com.ankurm.beanmapping.model.LazyUser; +import com.ankurm.beanmapping.model.LazyUserView; +import org.mapstruct.Mapper; +import org.mapstruct.Mapping; +import org.mapstruct.factory.Mappers; + +/** MapStruct also calls getters. {@code ignore = true} is its version of Dozer's exclude. */ +@Mapper +public interface LazyMapper { + LazyMapper INSTANCE = Mappers.getMapper(LazyMapper.class); + + @Mapping(target = "orders", ignore = true) + LazyUserView toView(LazyUser user); + + /** No ignore: the generated code calls getOrders(). */ + LazyUserView toViewWithOrders(LazyUser user); +} diff --git a/bean-mapping/src/main/java/com/ankurm/beanmapping/mapstruct/MapStructTour.java b/bean-mapping/src/main/java/com/ankurm/beanmapping/mapstruct/MapStructTour.java new file mode 100644 index 0000000..0cb7792 --- /dev/null +++ b/bean-mapping/src/main/java/com/ankurm/beanmapping/mapstruct/MapStructTour.java @@ -0,0 +1,51 @@ +package com.ankurm.beanmapping.mapstruct; + +import com.ankurm.beanmapping.model.*; + +/** The MapStruct counterpart of DozerTour, plus the two things Dozer could not do: records and a failing build. */ +public class MapStructTour { + + public static void main(String[] args) { + UserMapper mapper = UserMapper.INSTANCE; + + System.out.println("== 1. Basic + nested + collection in one mapper"); + UserEntity entity = mapper.toEntity(Samples.nested()); + System.out.println(entity); + + System.out.println(); + System.out.println("== 2. Bidirectional through @InheritInverseConfiguration"); + UserDTO back = mapper.toDto(entity); + System.out.println("round trip equals original: " + back.equals(Samples.nested())); + + System.out.println(); + System.out.println("== 3. Null handling: the plain method copies null, the update method with IGNORE does not"); + UserEntity viaNew = mapper.toEntity(Samples.withNulls()); + System.out.println("new object from DTO with nulls: emailAddress=" + viaNew.getEmailAddress() + ", profile=" + viaNew.getProfile()); + UserEntity existing = mapper.toEntity(Samples.nested()); + mapper.update(existing, Samples.withNulls()); + System.out.println("update() of an existing entity: emailAddress=" + existing.getEmailAddress() + ", profile=" + existing.getProfile()); + + System.out.println(); + System.out.println("== 4. Records, both directions"); + UserRecord rec = RecordMapper.INSTANCE.toRecord(Samples.flat()); + System.out.println("record destination: " + rec); + System.out.println("record source: " + RecordMapper.INSTANCE.toDto(new UserRecord(7L, "rec"))); + + System.out.println(); + System.out.println("== 5. A lazy getter: MapStruct calls it too, unless you ignore the property"); + LazyUser lazy = new LazyUser(); + lazy.setId(5L); + lazy.setName("lazy"); + try { + LazyMapper.INSTANCE.toViewWithOrders(lazy); + System.out.println("map succeeded"); + } catch (Exception e) { + System.out.println("map failed: " + e.getClass().getSimpleName() + ": " + e.getMessage()); + } + System.out.println("without ignore, getOrders() was called " + lazy.orderReads() + " time(s)"); + LazyUser lazy2 = new LazyUser(); + lazy2.setId(6L); + lazy2.setName("lazy2"); + System.out.println("with ignore: " + LazyMapper.INSTANCE.toView(lazy2) + ", getOrders() called " + lazy2.orderReads() + " time(s)"); + } +} diff --git a/bean-mapping/src/main/java/com/ankurm/beanmapping/mapstruct/RecordMapper.java b/bean-mapping/src/main/java/com/ankurm/beanmapping/mapstruct/RecordMapper.java new file mode 100644 index 0000000..66481ba --- /dev/null +++ b/bean-mapping/src/main/java/com/ankurm/beanmapping/mapstruct/RecordMapper.java @@ -0,0 +1,17 @@ +package com.ankurm.beanmapping.mapstruct; + +import com.ankurm.beanmapping.model.UserDTO; +import com.ankurm.beanmapping.model.UserRecord; +import org.mapstruct.Mapper; +import org.mapstruct.ReportingPolicy; +import org.mapstruct.factory.Mappers; + +/** MapStruct maps to and from records through the canonical constructor. */ +@Mapper(unmappedTargetPolicy = ReportingPolicy.IGNORE) +public interface RecordMapper { + RecordMapper INSTANCE = Mappers.getMapper(RecordMapper.class); + + UserRecord toRecord(UserDTO dto); + + UserDTO toDto(UserRecord record); +} diff --git a/bean-mapping/src/main/java/com/ankurm/beanmapping/mapstruct/UserMapper.java b/bean-mapping/src/main/java/com/ankurm/beanmapping/mapstruct/UserMapper.java new file mode 100644 index 0000000..f1d5a4d --- /dev/null +++ b/bean-mapping/src/main/java/com/ankurm/beanmapping/mapstruct/UserMapper.java @@ -0,0 +1,43 @@ +package com.ankurm.beanmapping.mapstruct; + +import com.ankurm.beanmapping.model.*; +import org.mapstruct.*; +import org.mapstruct.factory.Mappers; + +/** + * The MapStruct equivalent of {@code DozerSetup.standard()}. + * {@code unmappedTargetPolicy = ERROR} turns "a target field nobody fills" into a compile error. + */ +@Mapper(unmappedTargetPolicy = ReportingPolicy.ERROR) +public interface UserMapper { + + UserMapper INSTANCE = Mappers.getMapper(UserMapper.class); + + @Mapping(source = "email", target = "emailAddress") + UserEntity toEntity(UserDTO dto); + + /** + * One line replaces Dozer's implicit bidirectionality. The name is required once an update method + * exists: without it MapStruct reports "Several matching inverse methods exist: toEntity(), update()". + */ + @InheritInverseConfiguration(name = "toEntity") + UserDTO toDto(UserEntity entity); + + ProfileEntity toEntity(ProfileDTO profile); + + ProfileDTO toDto(ProfileEntity profile); + + /** Dozer needed a DozerConverter class for this; MapStruct picks up any method with the right types. */ + default TagEntity toTag(String name) { + return name == null ? null : new TagEntity(name); + } + + default String fromTag(TagEntity tag) { + return tag == null ? null : tag.getName(); + } + + /** Partial update: a null in the DTO leaves the entity's value alone (Dozer: mapNull(false)). */ + @BeanMapping(nullValuePropertyMappingStrategy = NullValuePropertyMappingStrategy.IGNORE) + @Mapping(source = "email", target = "emailAddress") + void update(@MappingTarget UserEntity target, UserDTO source); +} diff --git a/bean-mapping/src/main/java/com/ankurm/beanmapping/model/LazyUser.java b/bean-mapping/src/main/java/com/ankurm/beanmapping/model/LazyUser.java new file mode 100644 index 0000000..72f9d8f --- /dev/null +++ b/bean-mapping/src/main/java/com/ankurm/beanmapping/model/LazyUser.java @@ -0,0 +1,28 @@ +package com.ankurm.beanmapping.model; + +import java.util.List; +import java.util.concurrent.atomic.AtomicInteger; + +/** + * Stand-in for a Hibernate entity with a lazy collection. This is a simulation, not Hibernate: + * {@link #getOrders()} throws what an uninitialised lazy collection throws once the session is closed, + * and counts how often someone asked. + */ +public class LazyUser { + private long id; + private String name; + private final AtomicInteger orderReads = new AtomicInteger(); + + public long getId() { return id; } + public void setId(long id) { this.id = id; } + public String getName() { return name; } + public void setName(String name) { this.name = name; } + + public List getOrders() { + orderReads.incrementAndGet(); + throw new IllegalStateException("could not initialize proxy - no Session (simulated lazy collection)"); + } + public void setOrders(List orders) { /* not needed for the demo */ } + + public int orderReads() { return orderReads.get(); } +} diff --git a/bean-mapping/src/main/java/com/ankurm/beanmapping/model/LazyUserView.java b/bean-mapping/src/main/java/com/ankurm/beanmapping/model/LazyUserView.java new file mode 100644 index 0000000..87eb2f7 --- /dev/null +++ b/bean-mapping/src/main/java/com/ankurm/beanmapping/model/LazyUserView.java @@ -0,0 +1,19 @@ +package com.ankurm.beanmapping.model; + +import java.util.List; + +/** What the API wants to return for a {@link LazyUser}. */ +public class LazyUserView { + private long id; + private String name; + private List orders; + + public long getId() { return id; } + public void setId(long id) { this.id = id; } + public String getName() { return name; } + public void setName(String name) { this.name = name; } + public List getOrders() { return orders; } + public void setOrders(List orders) { this.orders = orders; } + + @Override public String toString() { return "LazyUserView[id=" + id + ", name=" + name + ", orders=" + orders + "]"; } +} diff --git a/bean-mapping/src/main/java/com/ankurm/beanmapping/model/ProfileDTO.java b/bean-mapping/src/main/java/com/ankurm/beanmapping/model/ProfileDTO.java new file mode 100644 index 0000000..7f3ca21 --- /dev/null +++ b/bean-mapping/src/main/java/com/ankurm/beanmapping/model/ProfileDTO.java @@ -0,0 +1,23 @@ +package com.ankurm.beanmapping.model; + +import java.util.Objects; + +/** Nested API-side object. Plain JavaBean: Dozer needs the no-arg constructor and the setters. */ +public class ProfileDTO { + private String firstName; + private String lastName; + + public ProfileDTO() { } + public ProfileDTO(String firstName, String lastName) { this.firstName = firstName; this.lastName = lastName; } + + public String getFirstName() { return firstName; } + public void setFirstName(String firstName) { this.firstName = firstName; } + public String getLastName() { return lastName; } + public void setLastName(String lastName) { this.lastName = lastName; } + + @Override public boolean equals(Object o) { + return o instanceof ProfileDTO p && Objects.equals(firstName, p.firstName) && Objects.equals(lastName, p.lastName); + } + @Override public int hashCode() { return Objects.hash(firstName, lastName); } + @Override public String toString() { return "ProfileDTO[" + firstName + " " + lastName + "]"; } +} diff --git a/bean-mapping/src/main/java/com/ankurm/beanmapping/model/ProfileEntity.java b/bean-mapping/src/main/java/com/ankurm/beanmapping/model/ProfileEntity.java new file mode 100644 index 0000000..4d5bb89 --- /dev/null +++ b/bean-mapping/src/main/java/com/ankurm/beanmapping/model/ProfileEntity.java @@ -0,0 +1,23 @@ +package com.ankurm.beanmapping.model; + +import java.util.Objects; + +/** Nested persistence-side object, same shape as {@link ProfileDTO}. */ +public class ProfileEntity { + private String firstName; + private String lastName; + + public ProfileEntity() { } + public ProfileEntity(String firstName, String lastName) { this.firstName = firstName; this.lastName = lastName; } + + public String getFirstName() { return firstName; } + public void setFirstName(String firstName) { this.firstName = firstName; } + public String getLastName() { return lastName; } + public void setLastName(String lastName) { this.lastName = lastName; } + + @Override public boolean equals(Object o) { + return o instanceof ProfileEntity p && Objects.equals(firstName, p.firstName) && Objects.equals(lastName, p.lastName); + } + @Override public int hashCode() { return Objects.hash(firstName, lastName); } + @Override public String toString() { return "ProfileEntity[" + firstName + " " + lastName + "]"; } +} diff --git a/bean-mapping/src/main/java/com/ankurm/beanmapping/model/Samples.java b/bean-mapping/src/main/java/com/ankurm/beanmapping/model/Samples.java new file mode 100644 index 0000000..1f02bd0 --- /dev/null +++ b/bean-mapping/src/main/java/com/ankurm/beanmapping/model/Samples.java @@ -0,0 +1,33 @@ +package com.ankurm.beanmapping.model; + +import java.util.List; + +/** The same three inputs are used by the demos, the tests and the benchmark. */ +public final class Samples { + private Samples() { } + + /** Only id, username, email: no profile, no tags. What the benchmark calls "flat". */ + public static UserDTO flat() { + UserDTO d = new UserDTO(); + d.setId(1L); + d.setUsername("ankurm"); + d.setEmail("admin@ankurm.com"); + return d; + } + + /** Flat fields plus a nested profile and five tags. What the benchmark calls "nested". */ + public static UserDTO nested() { + UserDTO d = flat(); + d.setProfile(new ProfileDTO("John", "Doe")); + d.setTags(List.of("Java", "Spring Boot", "Dozer", "MapStruct", "JMH")); + return d; + } + + /** Same as {@link #nested()} but with null email and null profile: exercises null handling. */ + public static UserDTO withNulls() { + UserDTO d = nested(); + d.setEmail(null); + d.setProfile(null); + return d; + } +} diff --git a/bean-mapping/src/main/java/com/ankurm/beanmapping/model/TagEntity.java b/bean-mapping/src/main/java/com/ankurm/beanmapping/model/TagEntity.java new file mode 100644 index 0000000..ae77c32 --- /dev/null +++ b/bean-mapping/src/main/java/com/ankurm/beanmapping/model/TagEntity.java @@ -0,0 +1,18 @@ +package com.ankurm.beanmapping.model; + +import java.util.Objects; + +/** The element type that a {@code List} on the DTO has to turn into. */ +public class TagEntity { + private String name; + + public TagEntity() { } + public TagEntity(String name) { this.name = name; } + + public String getName() { return name; } + public void setName(String name) { this.name = name; } + + @Override public boolean equals(Object o) { return o instanceof TagEntity t && Objects.equals(name, t.name); } + @Override public int hashCode() { return Objects.hashCode(name); } + @Override public String toString() { return "Tag[" + name + "]"; } +} diff --git a/bean-mapping/src/main/java/com/ankurm/beanmapping/model/UserDTO.java b/bean-mapping/src/main/java/com/ankurm/beanmapping/model/UserDTO.java new file mode 100644 index 0000000..0bed2e4 --- /dev/null +++ b/bean-mapping/src/main/java/com/ankurm/beanmapping/model/UserDTO.java @@ -0,0 +1,34 @@ +package com.ankurm.beanmapping.model; + +import java.util.ArrayList; +import java.util.List; +import java.util.Objects; + +/** API-side user. Note {@code email}: the entity calls the same thing {@code emailAddress}. */ +public class UserDTO { + private long id; + private String username; + private String email; + private ProfileDTO profile; + private List tags = new ArrayList<>(); + + public long getId() { return id; } + public void setId(long id) { this.id = id; } + public String getUsername() { return username; } + public void setUsername(String username) { this.username = username; } + public String getEmail() { return email; } + public void setEmail(String email) { this.email = email; } + public ProfileDTO getProfile() { return profile; } + public void setProfile(ProfileDTO profile) { this.profile = profile; } + public List getTags() { return tags; } + public void setTags(List tags) { this.tags = tags; } + + @Override public boolean equals(Object o) { + return o instanceof UserDTO u && id == u.id && Objects.equals(username, u.username) + && Objects.equals(email, u.email) && Objects.equals(profile, u.profile) && Objects.equals(tags, u.tags); + } + @Override public int hashCode() { return Objects.hash(id, username, email, profile, tags); } + @Override public String toString() { + return "UserDTO[id=" + id + ", username=" + username + ", email=" + email + ", profile=" + profile + ", tags=" + tags + "]"; + } +} diff --git a/bean-mapping/src/main/java/com/ankurm/beanmapping/model/UserEntity.java b/bean-mapping/src/main/java/com/ankurm/beanmapping/model/UserEntity.java new file mode 100644 index 0000000..0fb60f6 --- /dev/null +++ b/bean-mapping/src/main/java/com/ankurm/beanmapping/model/UserEntity.java @@ -0,0 +1,35 @@ +package com.ankurm.beanmapping.model; + +import java.util.ArrayList; +import java.util.List; +import java.util.Objects; + +/** Persistence-side user: {@code emailAddress} instead of {@code email}, {@link TagEntity} instead of String. */ +public class UserEntity { + private long id; + private String username; + private String emailAddress; + private ProfileEntity profile; + private List tags = new ArrayList<>(); + + public long getId() { return id; } + public void setId(long id) { this.id = id; } + public String getUsername() { return username; } + public void setUsername(String username) { this.username = username; } + public String getEmailAddress() { return emailAddress; } + public void setEmailAddress(String emailAddress) { this.emailAddress = emailAddress; } + public ProfileEntity getProfile() { return profile; } + public void setProfile(ProfileEntity profile) { this.profile = profile; } + public List getTags() { return tags; } + public void setTags(List tags) { this.tags = tags; } + + @Override public boolean equals(Object o) { + return o instanceof UserEntity u && id == u.id && Objects.equals(username, u.username) + && Objects.equals(emailAddress, u.emailAddress) && Objects.equals(profile, u.profile) && Objects.equals(tags, u.tags); + } + @Override public int hashCode() { return Objects.hash(id, username, emailAddress, profile, tags); } + @Override public String toString() { + return "UserEntity[id=" + id + ", username=" + username + ", emailAddress=" + emailAddress + + ", profile=" + profile + ", tags=" + tags + "]"; + } +} diff --git a/bean-mapping/src/main/java/com/ankurm/beanmapping/model/UserEntityV2.java b/bean-mapping/src/main/java/com/ankurm/beanmapping/model/UserEntityV2.java new file mode 100644 index 0000000..4c0c2ea --- /dev/null +++ b/bean-mapping/src/main/java/com/ankurm/beanmapping/model/UserEntityV2.java @@ -0,0 +1,25 @@ +package com.ankurm.beanmapping.model; + +/** + * {@link UserEntity} after someone added a column: {@code phone} has no counterpart on the DTO. + * Used to show who notices a new, unmapped target property: Dozer never does, MapStruct can fail the build. + */ +public class UserEntityV2 { + private long id; + private String username; + private String emailAddress; + private String phone; + + public long getId() { return id; } + public void setId(long id) { this.id = id; } + public String getUsername() { return username; } + public void setUsername(String username) { this.username = username; } + public String getEmailAddress() { return emailAddress; } + public void setEmailAddress(String emailAddress) { this.emailAddress = emailAddress; } + public String getPhone() { return phone; } + public void setPhone(String phone) { this.phone = phone; } + + @Override public String toString() { + return "UserEntityV2[id=" + id + ", username=" + username + ", emailAddress=" + emailAddress + ", phone=" + phone + "]"; + } +} diff --git a/bean-mapping/src/main/java/com/ankurm/beanmapping/model/UserRecord.java b/bean-mapping/src/main/java/com/ankurm/beanmapping/model/UserRecord.java new file mode 100644 index 0000000..9299ba6 --- /dev/null +++ b/bean-mapping/src/main/java/com/ankurm/beanmapping/model/UserRecord.java @@ -0,0 +1,4 @@ +package com.ankurm.beanmapping.model; + +/** A Java record: immutable, no setters, no no-arg constructor. */ +public record UserRecord(long id, String username) { } diff --git a/bean-mapping/src/test/java/com/ankurm/beanmapping/BeanMappingClaimsTest.java b/bean-mapping/src/test/java/com/ankurm/beanmapping/BeanMappingClaimsTest.java new file mode 100644 index 0000000..f1cc519 --- /dev/null +++ b/bean-mapping/src/test/java/com/ankurm/beanmapping/BeanMappingClaimsTest.java @@ -0,0 +1,167 @@ +package com.ankurm.beanmapping; + +import com.ankurm.beanmapping.dozer.DozerSetup; +import com.ankurm.beanmapping.mapstruct.LazyMapper; +import com.ankurm.beanmapping.mapstruct.RecordMapper; +import com.ankurm.beanmapping.mapstruct.UserMapper; +import com.ankurm.beanmapping.model.*; +import com.github.dozermapper.core.DozerBeanMapperBuilder; +import com.github.dozermapper.core.Mapper; +import com.github.dozermapper.core.MappingException; +import com.github.dozermapper.core.loader.api.BeanMappingBuilder; +import com.github.dozermapper.core.loader.api.TypeMappingOptions; +import org.junit.jupiter.api.Test; + +import java.util.List; + +import static org.junit.jupiter.api.Assertions.*; + +/** Every sentence in the post that says "Dozer does X" or "MapStruct does Y" is one of these. */ +class BeanMappingClaimsTest { + + static final List INPUTS = List.of(Samples.flat(), Samples.nested(), Samples.withNulls()); + + // ---- migration parity: the test that lets you move one mapper at a time ---- + + @Test + void dozerMapStructAndManualAgreeOnDtoToEntity() { + Mapper dozer = DozerSetup.standard(); + for (UserDTO in : INPUTS) { + UserEntity expected = ManualMapper.toEntity(in); + assertEquals(expected, dozer.map(in, UserEntity.class), "dozer vs manual for " + in); + assertEquals(expected, UserMapper.INSTANCE.toEntity(in), "mapstruct vs manual for " + in); + } + } + + @Test + void dozerMapStructAndManualAgreeOnEntityToDto() { + Mapper dozer = DozerSetup.standard(); + for (UserDTO in : INPUTS) { + UserEntity entity = ManualMapper.toEntity(in); + UserDTO expected = ManualMapper.toDto(entity); + assertEquals(expected, dozer.map(entity, UserDTO.class), "dozer vs manual for " + entity); + assertEquals(expected, UserMapper.INSTANCE.toDto(entity), "mapstruct vs manual for " + entity); + } + } + + @Test + void roundTripIsLosslessForBothLibraries() { + UserDTO original = Samples.nested(); + assertEquals(original, DozerSetup.standard().map(DozerSetup.standard().map(original, UserEntity.class), UserDTO.class)); + assertEquals(original, UserMapper.INSTANCE.toDto(UserMapper.INSTANCE.toEntity(original))); + } + + @Test + void globalConverterIsNotUsedForListElementsEntityToDto() { + UserEntity entity = ManualMapper.toEntity(Samples.nested()); + UserDTO wrong = DozerSetup.globalConverterOnly().map(entity, UserDTO.class); + assertEquals(List.of("Tag[Java]", "Tag[Spring Boot]", "Tag[Dozer]", "Tag[MapStruct]", "Tag[JMH]"), wrong.getTags(), + "TagEntity.toString(), not StringToTagConverter.convertFrom()"); + assertEquals(Samples.nested().getTags(), DozerSetup.standard().map(entity, UserDTO.class).getTags()); + } + + // ---- null handling ---- + + @Test + void dozerCopiesNullByDefaultAndSkipsThemWithMapNullFalse() { + UserEntity overwritten = DozerSetup.standard().map(Samples.nested(), UserEntity.class); + DozerSetup.standard().map(Samples.withNulls(), overwritten); + assertNull(overwritten.getEmailAddress()); + assertNull(overwritten.getProfile()); + + Mapper skipping = DozerSetup.skippingNulls(); + UserEntity kept = skipping.map(Samples.nested(), UserEntity.class); + skipping.map(Samples.withNulls(), kept); + assertEquals("admin@ankurm.com", kept.getEmailAddress()); + assertNotNull(kept.getProfile()); + } + + @Test + void mapStructUpdateMethodWithIgnoreKeepsExistingValues() { + UserEntity kept = UserMapper.INSTANCE.toEntity(Samples.nested()); + UserMapper.INSTANCE.update(kept, Samples.withNulls()); + assertEquals("admin@ankurm.com", kept.getEmailAddress()); + assertNotNull(kept.getProfile()); + } + + // ---- what Dozer lets through silently ---- + + @Test + void unmappedRenamedFieldIsSilentlyNullInDozer() { + UserEntity e = DozerSetup.conventionOnly().map(Samples.flat(), UserEntity.class); + assertNull(e.getEmailAddress()); + assertEquals("ankurm", e.getUsername()); + } + + @Test + void newUnmappedTargetColumnIsSilentlyNullInDozer() { + UserEntityV2 v2 = DozerSetup.standard().map(Samples.flat(), UserEntityV2.class); + assertNull(v2.getPhone()); + } + + @Test + void fieldNameTypoFailsAtMapperBuildTimeNotAtCompileTime() { + BeanMappingBuilder typo = new BeanMappingBuilder() { + @Override + protected void configure() { + mapping(UserDTO.class, UserEntity.class).fields("emial", "emailAddress"); + } + }; + MappingException e = assertThrows(MappingException.class, + () -> DozerBeanMapperBuilder.create().withMappingBuilder(typo).build()); + assertTrue(e.getMessage().contains("emial")); + } + + @Test + void chainedMapNullFromTheOldPostDoesNotExistInDozer7() { + assertTrue(java.util.Arrays.stream(com.github.dozermapper.core.loader.api.TypeMappingBuilder.class.getMethods()) + .noneMatch(m -> m.getName().equals("mapNull"))); + assertNotNull(TypeMappingOptions.mapNull(false)); + } + + // ---- records ---- + + @Test + void dozerCannotMapIntoARecordButMapStructCan() { + assertThrows(MappingException.class, () -> DozerSetup.standard().map(Samples.flat(), UserRecord.class)); + assertEquals(new UserRecord(1L, "ankurm"), RecordMapper.INSTANCE.toRecord(Samples.flat())); + } + + @Test + void dozerFromARecordCopiesNothingWithoutAnError() { + UserDTO dto = DozerSetup.standard().map(new UserRecord(7L, "rec"), UserDTO.class); + assertEquals(0L, dto.getId()); + assertNull(dto.getUsername()); + assertEquals(7L, RecordMapper.INSTANCE.toDto(new UserRecord(7L, "rec")).getId()); + } + + // ---- lazy getters ---- + + @Test + void dozerCallsLazyGetterUnlessExcluded() { + LazyUser lazy = new LazyUser(); + assertThrows(MappingException.class, () -> DozerSetup.conventionOnly().map(lazy, LazyUserView.class)); + assertEquals(1, lazy.orderReads()); + + BeanMappingBuilder exclude = new BeanMappingBuilder() { + @Override + protected void configure() { + mapping(LazyUser.class, LazyUserView.class).exclude("orders"); + } + }; + LazyUser lazy2 = new LazyUser(); + assertDoesNotThrow(() -> DozerBeanMapperBuilder.create().withMappingBuilder(exclude).build().map(lazy2, LazyUserView.class)); + assertEquals(0, lazy2.orderReads()); + } + + @Test + void mapStructAlsoCallsLazyGetterUnlessIgnored() { + LazyUser lazy = new LazyUser(); + assertThrows(IllegalStateException.class, () -> LazyMapper.INSTANCE.toViewWithOrders(lazy)); + assertEquals(1, lazy.orderReads()); + + LazyUser lazy2 = new LazyUser(); + LazyMapper.INSTANCE.toView(lazy2); + assertEquals(0, lazy2.orderReads()); + } +} diff --git a/pom.xml b/pom.xml index 07025a2..b560839 100644 --- a/pom.xml +++ b/pom.xml @@ -48,6 +48,7 @@ classloaders optional process-api + bean-mapping