Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
slowness example.
using System;
using System.Net;
using System.Text;
using System.Linq;
using System.Diagnostics;
using System.Collections.Generic;
using Newtonsoft.Json;
using EventStore.ClientAPI;
namespace Foo {
public interface StockEvent{
Guid MessageId {get ;}
string Sku {get;}
}
class PutAwayEvent : StockEvent {
private string _skuIdentifier;
private Guid _messageId;
private int _count;
public string Sku {get {return _skuIdentifier;}}
public Guid MessageId {get {return _messageId;}}
public int Count {get {return _count;}}
public PutAwayEvent(Guid messageId, string skuIdentifier, int count) {
_messageId = messageId;
_skuIdentifier = skuIdentifier;
_count = count;
}
}
class DespatchEvent : StockEvent {
private string _skuIdentifier;
private Guid _messageId;
private int _count;
public string Sku {get {return _skuIdentifier;}}
public Guid MessageId {get {return _messageId;}}
public int Count {get {return _count;}}
public DespatchEvent(Guid messageId, string skuIdentifier, int count) {
_messageId = messageId;
_skuIdentifier = skuIdentifier;
_count = count;
}
}
public class shitbird {
public static void Main(params string [] args) {
Console.WriteLine("foo");
SimpleTest();
}
public static 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 static 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();
Console.WriteLine("Reading events.");
var nextSliceStart = StreamPosition.Start;
do
{
currentSlice = connection.ReadStreamEventsForward(streamId, nextSliceStart, sliceSize, false);
nextSliceStart = currentSlice.NextEventNumber;
streamEvents.AddRange(currentSlice.Events);
} while (!currentSlice.IsEndOfStream);
sw.Stop();
Console.WriteLine("done. " + sw.Elapsed);
}
}
public static void AppendEvents(string sku, IList<StockEvent> events)
{
if (events.Any(x => x.Sku != sku))
{
throw new InvalidOperationException("Event found with non matching SKU identifier.");
}
var sw = new Stopwatch();
sw.Start();
Console.WriteLine("Writing events.");
using (var connection = EventStoreConnection.Create(new IPEndPoint(IPAddress.Loopback, 1113)))
{
connection.Connect();
connection.AppendToStream(sku, ExpectedVersion.Any, events.Select(x => ToEventData(x.MessageId, x)));
}
Console.WriteLine("done. " + sw.Elapsed);
}
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
You can’t perform that action at this time.