From 327ead755a21bf0aec9a020b01f9e8f105f14bbb Mon Sep 17 00:00:00 2001 From: Mark Paluch Date: Mon, 6 Apr 2020 14:37:40 +0200 Subject: [PATCH] #140 - Polishing. Use StaticApplicationContext instead of AnnotationConfigApplicationContext for an empty application context. Use dedicated Person class instead of reusing it from an other benchmark class. --- .../AfterConvertCallbacksBenchmark.java | 42 ++++++++++++------- 1 file changed, 28 insertions(+), 14 deletions(-) diff --git a/benchmark/mongodb/src/main/java/org/springframework/data/microbenchmark/mongodb/AfterConvertCallbacksBenchmark.java b/benchmark/mongodb/src/main/java/org/springframework/data/microbenchmark/mongodb/AfterConvertCallbacksBenchmark.java index 5a432a5..60a31c1 100644 --- a/benchmark/mongodb/src/main/java/org/springframework/data/microbenchmark/mongodb/AfterConvertCallbacksBenchmark.java +++ b/benchmark/mongodb/src/main/java/org/springframework/data/microbenchmark/mongodb/AfterConvertCallbacksBenchmark.java @@ -15,16 +15,18 @@ */ package org.springframework.data.microbenchmark.mongodb; +import static org.mockito.Mockito.*; + import org.bson.Document; -import org.mockito.Mockito; import org.openjdk.jmh.annotations.Benchmark; import org.openjdk.jmh.annotations.Setup; + import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; +import org.springframework.context.support.StaticApplicationContext; +import org.springframework.data.annotation.Id; import org.springframework.data.microbenchmark.common.AbstractMicrobenchmark; -import org.springframework.data.microbenchmark.mongodb.ProjectionsBenchmark.Address; -import org.springframework.data.microbenchmark.mongodb.ProjectionsBenchmark.Person; import org.springframework.data.mongodb.MongoDatabaseFactory; import org.springframework.data.mongodb.core.MongoTemplate; import org.springframework.data.mongodb.core.SimpleMongoClientDatabaseFactory; @@ -48,19 +50,21 @@ public class AfterConvertCallbacksBenchmark extends AbstractMicrobenchmark { @Setup public void setUp() { - MongoClient client = Mockito.mock(MongoClient.class); - MongoDatabase db = Mockito.mock(MongoDatabase.class); - MongoCollection collection = Mockito.mock(MongoCollection.class); + MongoClient client = mock(MongoClient.class); + MongoDatabase db = mock(MongoDatabase.class); + MongoCollection collection = mock(MongoCollection.class); - Mockito.when(client.getDatabase(Mockito.anyString())).thenReturn(db); - Mockito.when(db.getCollection(Mockito.anyString(), Mockito.eq(Document.class))).thenReturn(collection); + when(client.getDatabase(anyString())).thenReturn(db); + when(db.getCollection(anyString(), eq(Document.class))).thenReturn(collection); MongoDatabaseFactory factory = new SimpleMongoClientDatabaseFactory(client, "mock-database"); templateWithoutContext = new MongoTemplate(factory); templateWithEmptyContext = new MongoTemplate(factory); - templateWithEmptyContext.setApplicationContext(new AnnotationConfigApplicationContext(EmptyConfig.class)); + StaticApplicationContext empty = new StaticApplicationContext(); + empty.refresh(); + templateWithEmptyContext.setApplicationContext(empty); templateWithContext = new MongoTemplate(factory); templateWithContext.setApplicationContext(new AnnotationConfigApplicationContext(EntityCallbackConfig.class)); @@ -90,11 +94,6 @@ public class AfterConvertCallbacksBenchmark extends AbstractMicrobenchmark { return templateWithContext.save(source); } - @Configuration - static class EmptyConfig { - - } - @Configuration static class EntityCallbackConfig { @@ -112,4 +111,19 @@ public class AfterConvertCallbacksBenchmark extends AbstractMicrobenchmark { }; } } + + static class Person { + + @Id String id; + String firstname; + String lastname; + Address address; + } + + static class Address { + + String city; + String street; + } + }