【发布时间】: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