Event-Store perf test
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[Test] | |
public void SimpleTest() | |
{ | |
string skuIdentifier = Guid.NewGuid().ToString(); | |
var stockEvents1 = new List<StockEvent> | |
{ | |
new PutAwayEvent(Guid.NewGuid(), skuIdentifier, 4), | |
new DespatchEvent(Guid.NewGuid(), skuIdentifier, 1) | |
}; | |
AppendEvents(skuIdentifier, stockEvents1); | |
// Uncomment this and the next time it will run 4 times slower | |
//ReadEvents(skuIdentifier); | |
var stockEvents = new List<StockEvent>(); | |
for (int i = 0; i < 10000; i++) | |
{ | |
stockEvents.Add(new PutAwayEvent(Guid.NewGuid(), skuIdentifier, 1)); | |
} | |
for (int i = 0; i < 5000; i++) | |
{ | |
stockEvents.Add(new DespatchEvent(Guid.NewGuid(), skuIdentifier, 1)); | |
} | |
AppendEvents(skuIdentifier, stockEvents); | |
ReadEvents(skuIdentifier); | |
} | |
public void ReadEvents(string streamId) | |
{ | |
const int sliceSize = 2000; | |
using (var connection = EventStoreConnection.Create(new IPEndPoint(IPAddress.Loopback, 1113))) | |
{ | |
connection.Connect(); | |
StreamEventsSlice currentSlice; | |
var streamEvents = new List<ResolvedEvent>(); | |
var sw = new Stopwatch(); | |
sw.Start(); | |
var nextSliceStart = StreamPosition.Start; | |
do | |
{ | |
currentSlice = connection.ReadStreamEventsForward(streamId, nextSliceStart, sliceSize, false); | |
nextSliceStart = currentSlice.NextEventNumber; | |
streamEvents.AddRange(currentSlice.Events); | |
} while (!currentSlice.IsEndOfStream); | |
sw.Stop(); | |
Debug.WriteLine("1:{0}", sw.ElapsedMilliseconds); | |
} | |
} | |
public void AppendEvents(string sku, IList<StockEvent> events) | |
{ | |
if (events.Any(x => x.Sku != sku)) | |
{ | |
throw new InvalidOperationException("Event found with non matching SKU identifier."); | |
} | |
using (var connection = EventStoreConnection.Create(new IPEndPoint(IPAddress.Loopback, 1113))) | |
{ | |
connection.Connect(); | |
connection.AppendToStream(sku, ExpectedVersion.Any, events.Select(x => ToEventData(x.MessageId, x))); | |
} | |
} | |
private static EventData ToEventData(Guid eventId, object @event, IDictionary<string, object> headers = null) | |
{ | |
if (headers == null) headers = new Dictionary<string, object>(); | |
var serializerSettings = new JsonSerializerSettings { TypeNameHandling = TypeNameHandling.None }; | |
var data = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(@event, serializerSettings)); | |
var eventHeaders = new Dictionary<string, object>(headers) | |
{ | |
{ | |
"EventClrTypeName", @event.GetType().AssemblyQualifiedName | |
} | |
}; | |
var metadata = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(eventHeaders, serializerSettings)); | |
var typeName = @event.GetType().Name; | |
return new EventData(eventId, typeName, true, data, metadata); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment