【问题标题】:Tests using RavenDb EmbeddableDocumentStore very slow compared to real local Raven instance与真实的本地 Raven 实例相比,使用 RavenDb EmbeddableDocumentStore 的测试非常慢
【发布时间】:2014-03-21 19:24:11
【问题描述】:

总结:我想使用 EmbeddableDocumentStore 对使用 IDocumentSession 的类进行单元测试(我相信是the recommended approach)。与使用“真实的”本地托管 DocumentStore 创建的会话相比,使用静态索引的简单测试运行非常缓慢。运行了一些基本的分析 - 大约 7x 慢(见下文)。我不能使用RavenTestBase,因为我想使用this approach to test using SpecsFor

我期待使用内存存储的测试非常快 - 我在解释这种性能时做错了吗?

编辑Having read this我禁用了我的防病毒软件,这没有任何区别。

两种方式的区别:

我通过以下方式创建了一个 EmbeddableDocumentStore

    private static IDocumentStore CreateInMemoryEmbdeddableDocumentStore()
    {
        var embeddedStore = new EmbeddableDocumentStore();
        embeddedStore.Configuration.RunInMemory = true;
        embeddedStore.Configuration.RunInUnreliableYetFastModeThatIsNotSuitableForProduction = true;
        embeddedStore.RegisterListener(new NoStaleQueriesAllowedListener());
        return embeddedStore.Initialize();
    }

和“真正的”DocumentStore by:

    public IDocumentStore CreateLocalDocumentStore()
    {
        var store = new DocumentStore { Url = "http://localhost:8080", DefaultDatabase = "ColourTest" };
        store.RegisterListener(new NoStaleQueriesAllowedListener());
        return store.Initialize();
    }

在这两种情况下我都使用它来确保在获取查询结果之前已经进行了索引:

    public class NoStaleQueriesAllowedListener : IDocumentQueryListener
    {
        public void BeforeQueryExecuted(IDocumentQueryCustomization queryCustomization)
        {
            queryCustomization.WaitForNonStaleResults();
        }
    }

然后我通过以下方式获得会话:

    private static IDocumentSession ConfigureIndexesAndCreateSession(IDocumentStore store)
    {
        IndexCreation.CreateIndexes(typeof(Colours_ColourCountByMood).Assembly, store);
        return store.OpenSession();
    }

使用这个会话,我设置了测试数据(对于真实的商店,我也先删除现有数据)。然后像往常一样运行method being tested - 它使用static index - 并执行断言。

性能与分析比较:

我还添加了logging calls through the code (see complete code here) 来比较两种情况下的时间。测试运行的示例结果:

TestUsingInMemoryEmbeddable()
  191 ms : Start create EmbeddableDocumentStore
 1819 ms : Finish create EmbeddableDocumentStore
 1819 ms : Start embeddedStore.Initialize()
 3411 ms : Finish embeddedStore.Initialize()
 3411 ms : Start CreateIndexes
 5322 ms : Finish CreateIndexes
 5322 ms : Start OpenSession
 5330 ms : Finish OpenSession
 5331 ms : Start test data storing
 5852 ms : Finish test data storing
 5853 ms : Start test Act
 6985 ms : Finish test Act
 6985 ms : Start test Assert
 6998 ms : Finish test Assert

TestUsingLocallyHosted()
    1 ms : Start create DocumentStore
    1 ms : Finish create DocumentStore
    2 ms : Start documentStore.Initialize()
  608 ms : Finish documentStore.Initialize()
  608 ms : Start CreateIndexes
  717 ms : Finish CreateIndexes
  717 ms : Start OpenSession
  717 ms : Finish OpenSession
  718 ms : Start DeleteTestData
  730 ms : Finish DeleteTestData
  730 ms : Start test data storing
  823 ms : Finish test data storing
  823 ms : Start test Act
  957 ms : Finish test Act
  957 ms : Start test Assert
  957 ms : Finish test Assert

运行这些多次后,上述数字具有代表性:当运行嵌入式时,整个测试代码的相对缓慢是显而易见的/固有的。

【问题讨论】:

    标签: .net unit-testing ravendb


    【解决方案1】:

    您需要直接在 EmbeddableDocumentStore 上使用 RunInMemory 属性。

    另外,您可能需要运行几次,以查看时间。我们正在做大量前期工作以降低总体成本。

    【讨论】:

    • 我又运行了几次,但得到了相似的结果。 Taking a look at the source,看起来像调用“embeddedStore.Configuration.RunInMemory = true;”和“embeddedStore.RunInMemory = true;”做同样的事情 - RunInMemory 只是在其 RavenConfiguration 实例上设置相应的属性。
    • 如果我在我的测试夹具中多次复制(复制、粘贴然后重命名)my test method "TestUsingInMemoryEmbeddable()",运行的第一个副本执行如上,但所有后续副本运行得非常快(200 毫秒)。这表明即使在每个副本中我都在创建一个存储/会话 然后处理其中的每一个 - something 在内存中徘徊,以便创建内存存储并在同一运行中的后续测试调用中更快地初始化......
    • 如果是这样,那么我认为每次创建/初始化 EmbeddableDocumentStore(即我在需要 IDocumentSession 的地方测试的每个类)时都会成倍增加的性能问题就不是问题了。这符合你的理解吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-07-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-12
    • 2021-02-13
    • 1970-01-01
    相关资源
    最近更新 更多