Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rogersillito/9692046 to your computer and use it in GitHub Desktop.
Save rogersillito/9692046 to your computer and use it in GitHub Desktop.
Comparison (inc. log4net profiling) of unit testing a class using a RavenDb IDocumentSession: in-memory embeddable vs locally-hosted RavenDb instance
using log4net;
using NUnit.Framework;
using Raven.Abstractions.Data;
using Raven.Client;
using Raven.Client.Document;
using Raven.Client.Embedded;
using Raven.Client.Indexes;
using Raven.Client.Listeners;
namespace RavenTesting
{
[TestFixture]
public class RavenTestEmbeddedVsServerWithProfiling
{
private static readonly ILog Log = LogManager.GetLogger(typeof(RavenTestEmbeddedVsServerWithProfiling));
[TestFixtureSetUp]
public void TestFixtureSetup()
{
log4net.Config.XmlConfigurator.Configure();
}
[SetUp]
public void Setup()
{
DebugTimer.Reset();
}
[TearDown]
public void TearDown()
{
Log.Debug("");
}
[Test]
public void TestUsingInMemoryEmbeddable()
{
Log.Debug("TestUsingInMemoryEmbeddable()");
using (var store = CreateInMemoryEmbdeddableDocumentStore())
using (var session = ConfigureIndexesAndCreateSession(store))
{
CreateTestData(session);
ActAssert(session);
}
}
[Test]
public void TestUsingLocallyHosted()
{
Log.Debug("TestUsingLocallyHosted()");
using (var store = CreateLocalDocumentStore())
using (var session = ConfigureIndexesAndCreateSession(store))
{
DeleteTestData(session); // Extra step to clean up data
CreateTestData(session);
ActAssert(session);
}
}
private static IDocumentStore CreateInMemoryEmbdeddableDocumentStore()
{
Log.DebugAppendTime("Start create EmbeddableDocumentStore");
var embeddedStore = new EmbeddableDocumentStore();
embeddedStore.Configuration.RunInMemory = true;
embeddedStore.Configuration.RunInUnreliableYetFastModeThatIsNotSuitableForProduction
= true;
embeddedStore.RegisterListener(new NoStaleQueriesAllowedListener());
Log.DebugAppendTime("Finish create EmbeddableDocumentStore");
Log.DebugAppendTime("Start embeddedStore.Initialize()");
var store = embeddedStore.Initialize();
Log.DebugAppendTime("Finish embeddedStore.Initialize()");
return store;
}
public IDocumentStore CreateLocalDocumentStore()
{
Log.DebugAppendTime("Start create DocumentStore");
var store = new DocumentStore { Url = "http://localhost:8080", DefaultDatabase = "ColourTest" };
store.RegisterListener(new NoStaleQueriesAllowedListener());
Log.DebugAppendTime("Finish create DocumentStore");
Log.DebugAppendTime("Start documentStore.Initialize()");
store.Initialize();
Log.DebugAppendTime("Finish documentStore.Initialize()");
return store;
}
private static IDocumentSession ConfigureIndexesAndCreateSession(IDocumentStore store)
{
Log.DebugAppendTime("Start CreateIndexes");
IndexCreation.CreateIndexes(typeof(Colours_ColourCountByMood).Assembly, store);
Log.DebugAppendTime("Finish CreateIndexes");
Log.DebugAppendTime("Start OpenSession");
var session = store.OpenSession();
Log.DebugAppendTime("Finish OpenSession");
return session;
}
private void DeleteTestData(IDocumentSession ravenSession)
{
Log.DebugAppendTime("Start DeleteTestData");
ravenSession.Advanced.DocumentStore.DatabaseCommands.DeleteByIndex("Raven/DocumentsByEntityName",
new IndexQuery { Query = "Tag:Colours" },
allowStale: true
);
Log.DebugAppendTime("Finish DeleteTestData");
}
protected void CreateTestData(IDocumentSession ravenSession)
{
Log.DebugAppendTime("Start test data storing");
ravenSession.Store(new Colour { Name = "Red", Mood = Mood.Happy });
ravenSession.Store(new Colour { Name = "Yellow", Mood = Mood.Happy });
ravenSession.Store(new Colour { Name = "Blue", Mood = Mood.Happy });
ravenSession.Store(new Colour { Name = "Grey", Mood = Mood.Unhappy });
ravenSession.Store(new Colour { Name = "Black", Mood = Mood.Unhappy });
ravenSession.SaveChanges();
Log.DebugAppendTime("Finish test data storing");
}
protected void ActAssert(IDocumentSession ravenSession)
{
Log.DebugAppendTime("Start test Act");
var result = new ColourCounter(ravenSession).GetHappyCount();
Log.DebugAppendTime("Finish test Act");
Log.DebugAppendTime("Start test Assert");
Assert.That(result, Is.EqualTo(3));
Log.DebugAppendTime("Finish test Assert");
}
public class NoStaleQueriesAllowedListener : IDocumentQueryListener
{
public void BeforeQueryExecuted(IDocumentQueryCustomization queryCustomization)
{
queryCustomization.WaitForNonStaleResults();
}
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" requirePermission="false" />
</configSections>
<log4net>
<root>
<level value="DEBUG" />
<appender-ref ref="TestFileLog" />
</root>
<logger name="Raven" additivity="false">
</logger>
<appender name="TestFileLog" type="log4net.Appender.FileAppender">
<file value="..\..\Logs\RavenTesting.log" />
<appendToFile value="false" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%m%n" />
</layout>
</appender>
</log4net>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="nunit.framework" publicKeyToken="96d09a1eb7f44a77" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-2.6.3.13283" newVersion="2.6.3.13283" />
</dependentAssembly>
</assemblyBinding>
</runtime>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
</configuration>
using System.Collections.Generic;
namespace RavenTesting
{
public class Colour
{
public string Id { get; set; }
public string Name { get; set; }
public Mood Mood { get; set; }
}
public enum Mood
{
Happy,
Unhappy
}
}
using System.Linq;
using Raven.Client;
namespace RavenTesting
{
public interface IColourCounter
{
int GetHappyCount();
}
public class ColourCounter : IColourCounter
{
private readonly IDocumentSession _documentSession;
public ColourCounter(IDocumentSession documentSession)
{
_documentSession = documentSession;
}
public int GetHappyCount()
{
var reduceResult = _documentSession
.Query<Colours_ColourCountByMood.ReduceResult, Colours_ColourCountByMood>()
.Single(x => x.Mood == Mood.Happy);
return reduceResult.Count;
}
}
}
using System.Linq;
using Raven.Client.Indexes;
namespace RavenTesting
{
public class Colours_ColourCountByMood : AbstractIndexCreationTask<Colour, Colours_ColourCountByMood.ReduceResult>
{
public Colours_ColourCountByMood()
{
Map = colours => colours.Select(c => new { c.Mood, Count = 1 });
Reduce = results => results.GroupBy(r => r.Mood)
.Select(g => new { Mood = g.Key, Count = g.Sum(x => x.Count) });
}
public class ReduceResult
{
public Mood Mood { get; set; }
public int Count { get; set; }
}
}
}
using System;
using System.Diagnostics;
using System.Globalization;
using log4net;
namespace RavenTesting
{
public class DebugTimer
{
private static DateTime? _start;
private static Stopwatch _stopwatch;
public static void Reset()
{
_stopwatch = Stopwatch.StartNew();
}
public static string Time()
{
return _stopwatch.ElapsedMilliseconds.ToString(CultureInfo.InvariantCulture);
}
}
public static class LogExtensions
{
public static void DebugAppendTime(this ILog logger, string message)
{
logger.DebugFormat(string.Concat("{0} ms : ", message), DebugTimer.Time().ToString(CultureInfo.InvariantCulture).PadLeft(5));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment