#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.
This commit is contained in:
Mark Paluch
2020-04-06 14:37:40 +02:00
parent 40e051f494
commit 327ead755a

View File

@@ -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<Document> collection = Mockito.mock(MongoCollection.class);
MongoClient client = mock(MongoClient.class);
MongoDatabase db = mock(MongoDatabase.class);
MongoCollection<Document> 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;
}
}