public void IndexSpeedTest(bool useIndexes, int numberOfQueries, int numberOfEvents) { var sequenceNumbers = new Dictionary<string, long>(); var serializer = new JsonDomainEventSerializer(); try { var database = MongoHelper.InitializeTestDatabase(); database.Drop(); var eventStore = new MongoDbEventStore(database, "events", automaticallyCreateIndexes: useIndexes); var random = new Random(DateTime.Now.GetHashCode()); var aggregateRootIds = Enumerable.Range(0, 1000).Select(i => i.ToString()).ToArray(); Func<string, long> getNextSequenceNumber = id => !sequenceNumbers.ContainsKey(id) ? (sequenceNumbers[id] = 0) : ++sequenceNumbers[id]; Func<string> randomAggregateRootId = () => aggregateRootIds[random.Next(aggregateRootIds.Length)]; var events = Enumerable.Range(1, numberOfEvents) .Select(i => Event(getNextSequenceNumber, randomAggregateRootId())) .ToList(); TakeTime("Insert " + events.Count + " events", () => { foreach (var e in events) { eventStore.Save(Guid.NewGuid(), new[] { serializer.Serialize(e) }); } }); TakeTime("Execute " + numberOfQueries + " queries", () => numberOfQueries.Times(() => eventStore.Load(randomAggregateRootId()).ToList())); } finally { Console.WriteLine(@"This is how far we got: {0}", string.Join(Environment.NewLine, sequenceNumbers.Select(kvp => string.Format(" {0}: {1}", kvp.Key, kvp.Value)))); } }
void SaveEvents(int numberOfCommandsToProcess, string aggregateRootId) { var eventStore = new MongoDbEventStore(_database, "Events"); var serializer = new JsonDomainEventSerializer(); var typeNameMapper = new DefaultDomainTypeNameMapper(); Enumerable.Range(0, numberOfCommandsToProcess) .ToList() .ForEach(number => { var domainEvents = new[] { new RootGotNewNumber(number) { Meta = { {DomainEvent.MetadataKeys.AggregateRootId, aggregateRootId}, {DomainEvent.MetadataKeys.SequenceNumber, number.ToString()}, {DomainEvent.MetadataKeys.TimeUtc, Time.UtcNow().ToString("u")}, {DomainEvent.MetadataKeys.Type, typeNameMapper.GetName(typeof(RootGotNewNumber))}, {DomainEvent.MetadataKeys.Owner, typeNameMapper.GetName(typeof(Root))}, } } }; eventStore.Save(Guid.NewGuid(), domainEvents.Select(e => serializer.Serialize(e))); }); }