使用Embed Mongo DB

时间:2018-12-13 11:42:02

标签: mongodb spring-boot startup spring-boot-test

即使运行一项测试也需要超过1分钟的时间:

  • 嵌入Mongo的启动时间约为1分钟
  • 测试执行〜3-8秒

我不明白这种行为的原因是什么?
从控制台中摘录了以下内容:

2018-12-13 13:14:43.214  INFO 12277 --- [           main] c.j.s.embedmongo.EmbeddedMongoBuilder    : Initializing embedded MongoDB instance
2018-12-13 13:14:43.293  INFO 12277 --- [           main] d.f.embed.process.store.Downloader       : Extract /Users/nazar/.embedmongo/osx/mongodb-osx-ssl-x86_64-3.6.5.tgz : starting...
2018-12-13 13:14:44.287  INFO 12277 --- [           main] d.f.embed.process.store.Downloader       : Extract /Users/nazar/.embedmongo/osx/mongodb-osx-ssl-x86_64-3.6.5.tgz : extract mongodb-osx-x86_64-3.6.5/bin/mongod
2018-12-13 13:14:44.288  INFO 12277 --- [           main] d.f.embed.process.store.Downloader       : Extract /Users/nazar/.embedmongo/osx/mongodb-osx-ssl-x86_64-3.6.5.tgz : nothing left
2018-12-13 13:14:44.288  INFO 12277 --- [           main] d.f.embed.process.store.Downloader       : Extract /Users/nazar/.embedmongo/osx/mongodb-osx-ssl-x86_64-3.6.5.tgz : finished
2018-12-13 13:14:44.289  INFO 12277 --- [           main] c.j.s.embedmongo.EmbeddedMongoBuilder    : Starting embedded MongoDB instance
2018-12-13 13:14:45.467  INFO 12277 --- [           main] d.f.embed.mongo.MongodExecutable         : start de.flapdoodle.embed.mongo.config.MongodConfigBuilder$ImmutableMongodConfig@55fee662
2018-12-13 13:14:45.871  INFO 12277 --- [           main] org.mongodb.driver.cluster               : Cluster created with settings {hosts=[localhost:50770], mode=SINGLE, requiredClusterType=UNKNOWN, serverSelectionTimeout='30000 ms', maxWaitQueueSize=500}
2018-12-13 13:14:45.985  INFO 12277 --- [localhost:50770] org.mongodb.driver.connection            : Opened connection [connectionId{localValue:1, serverValue:1}] to localhost:50770
2018-12-13 13:14:45.990  INFO 12277 --- [localhost:50770] org.mongodb.driver.cluster               : Monitor thread successfully connected to server with description ServerDescription{address=localhost:50770, type=STANDALONE, state=CONNECTED, ok=true, version=ServerVersion{versionList=[3, 6, 5]}, minWireVersion=0, maxWireVersion=6, maxDocumentSize=16777216, logicalSessionTimeoutMinutes=30, roundTripTimeNanos=4065087}
2018-12-13 13:14:46.627  WARN 12277 --- [           main] c.t.p.p.configuration.BugsnagConfig      : Bugsnag apikey not set, not using it
2018-12-13 13:14:46.702  INFO 12277 --- [           main] c.t.p.p.c.AsyncConfigurationTest         : Creating sync task executor for tests
2018-12-13 13:15:38.019  INFO 12277 --- [       Thread-7] o.s.b.a.mongo.embedded.EmbeddedMongo     : note: noprealloc may hurt performance in many applications
2018-12-13 13:15:38.057  INFO 12277 --- [       Thread-7] o.s.b.a.mongo.embedded.EmbeddedMongo     : 2018-12-13T13:15:38.056+0200 I CONTROL  [initandlisten] MongoDB starting : pid=12460 port=50789 dbpath=/var/folders/95/6cdccp1s629gtfwm5tjb63gr0000gp/T/embedmongo-db-8535bf9e-fdc6-44f3-88c8-4bc77dd60bb3 64-bit host=sdos-MacBook-Pro.local
2018-12-13 13:15:38.057  INFO 12277 --- [       Thread-7] o.s.b.a.mongo.embedded.EmbeddedMongo     : 2018-12-13T13:15:38.057+0200 I CONTROL  [initandlisten] db version v3.5.5
2018-12-13 13:15:38.058  INFO 12277 --- [       Thread-7] o.s.b.a.mongo.embedded.EmbeddedMongo     : 2018-12-13T13:15:38.057+0200 I CONTROL  [initandlisten] git version: 98515c812b6fa893613f063dae568ff8319cbfbd
2018-12-13 13:15:38.058  INFO 12277 --- [       Thread-7] o.s.b.a.mongo.embedded.EmbeddedMongo     : 2018-12-13T13:15:38.057+0200 I CONTROL  [initandlisten] allocator: system
2018-12-13 13:15:38.058  INFO 12277 --- [       Thread-7] o.s.b.a.mongo.embedded.EmbeddedMongo     : 2018-12-13T13:15:38.057+0200 I CONTROL  [initandlisten] modules: none
2018-12-13 13:15:38.058  INFO 12277 --- [       Thread-7] o.s.b.a.mongo.embedded.EmbeddedMongo     : 2018-12-13T13:15:38.057+0200 I CONTROL  [initandlisten] build environment:
2018-12-13 13:15:38.058  INFO 12277 --- [       Thread-7] o.s.b.a.mongo.embedded.EmbeddedMongo     : 2018-12-13T13:15:38.057+0200 I CONTROL  [initandlisten]     distarch: x86_64
2018-12-13 13:15:38.058  INFO 12277 --- [       Thread-7] o.s.b.a.mongo.embedded.EmbeddedMongo     : 2018-12-13T13:15:38.057+0200 I CONTROL  [initandlisten]     target_arch: x86_64
2018-12-13 13:15:38.058  INFO 12277 --- [       Thread-7] o.s.b.a.mongo.embedded.EmbeddedMongo     : 2018-12-13T13:15:38.057+0200 I CONTROL  [initandlisten] options: { net: { bindIp: "127.0.0.1", http: { enabled: false }, port: 50789 }, security: { authorization: "disabled" }, storage: { dbPath: "/var/folders/95/6cdccp1s629gtfwm5tjb63gr0000gp/T/embedmongo-db-8535bf9e-fdc6-44f3-88c8-4bc77dd60bb3", journal: { enabled: false }, mmapv1: { preallocDataFiles: false, smallFiles: true }, syncPeriodSecs: 0.0 } }

这里是:full console log

行后最粘:

  

c.t.p.p.c.AsyncConfigurationTest:创建用于测试的同步任务执行器

这是pom文件的摘录:

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.1.0.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>
<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-mongodb</artifactId>
    </dependency>
    <dependency>
        <groupId>de.flapdoodle.embed</groupId>
        <artifactId>de.flapdoodle.embed.mongo</artifactId>
        <version>2.1.1</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>cz.jirutka.spring</groupId>
        <artifactId>embedmongo-spring</artifactId>
        <version>1.3.1</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>

没有其他插件,只有spring-boot-maven-plugin
另外,application.yml上没有用于测试的其他配置。

接下来是Mongo配置:

@Configuration
public class MongoConfig {

    private static final String MONGO_DB_URL = "localhost";
    private static final String MONGO_DB_NAME = "embeded_db";

    @Bean
    public MongoTemplate mongoTemplate() throws IOException {
        EmbeddedMongoFactoryBean mongo = new EmbeddedMongoFactoryBean();
        mongo.setBindIp(MONGO_DB_URL);
        MongoClient mongoClient = mongo.getObject();
        return new MongoTemplate(mongoClient, MONGO_DB_NAME);
    }
}

任务执行器配置:

@Slf4j
@Configuration
@Profile("test")
public class AsyncConfigurationTest {

    @Bean
    @Primary
    public TaskExecutor taskExecutor() {
        log.info("Creating sync task executor for tests");
        return new SyncTaskExecutor();
    }
}

下一步测试:

@Slf4j
@SpringBootTest
@ActiveProfiles("test")
@RunWith(SpringRunner.class)
public class ServiceTest extends BaseDataServiceTest {

@Value("classpath:enter/enter_file.pdf")
private Resource enterPdf;
@Value("classpath:enter/expected.json")
private Resource enterExpectedJson;

@Test
public void testEnterDemoDocument() {
    testHelper(enterPdf, enterExpectedJson);
}

一切正常。但是,目前速度非常慢。
如何调查瓶颈是什么?

启动缓慢的原因是什么?

2 个答案:

答案 0 :(得分:2)

Spring Boot对embedded MongoDB具有开箱即用的支持。此外,embedmongo-spring似乎已经过时,因为三年来没有碰过它。

对嵌入式MongoDB使用开箱即用的支持,而不是尝试在它周围包装其他东西。

注意::您还可以从flappoodle依赖项中删除<version>,因为Spring Boot也具有依赖项管理功能。

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>
<dependency>
    <groupId>de.flapdoodle.embed</groupId>
    <artifactId>de.flapdoodle.embed.mongo</artifactId>
    <scope>test</scope>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-test</artifactId>
    <scope>test</scope>
</dependency>

这些依赖项应该是您所需要的。

答案 1 :(得分:0)

我遇到了完全相同的问题,但是我没有使用Spring Boot。我发现我的问题是此行(嵌入式mongo使用该行):

        InetAddress ret = InetAddress.getLocalHost();

这就是解决方法 InetAddress.getLocalHost() slow to run (30+ seconds)