public void TestPerformance(int addIterations, int getIterations) { var stopwatch = Stopwatch.StartNew(); var messages = new List <object>(); addIterations.Times(() => { var message = new object(); messages.Add(message); c.AttachHeader(message, "whatever", "some value"); }); getIterations.Times(() => { foreach (var message in messages) { var customHeaders = c.GetHeadersFor(message); customHeaders.ShouldContainKeyAndValue("whatever", "some value"); } }); Console.WriteLine("{0} + {1} took {2:0.0} s", addIterations, getIterations, stopwatch.Elapsed.TotalSeconds); }
public void IsReliableAlsoWhenUsedByManyThreads(int howManyThreads, int numberOfIterations) { var reads = 0; var writes = 0; var headersAttached = 0; var headersActuallyFound = 0; Action printStats = () => Console.WriteLine("Reads: {0}. Writes: {1}. Buckets: {2}.", reads, writes, c.headers.Count); using (var printTimer = new Timer()) { printTimer.Interval = 2000; printTimer.Elapsed += (s, a) => printStats(); printTimer.Start(); var threads = Enumerable .Range(1, howManyThreads) .Select(no => string.Format("Thread#{0}", no)) .Select(name => new Thread(() => { var random = new Random(); var messages = new List <object>(); for (var count = 0; count < numberOfIterations; count++) { if (random.Next(2) == 0) { var message = new object(); messages.Add(message); c.AttachHeader(message, name + ".key", "some value"); Interlocked.Increment(ref headersAttached); Interlocked.Increment(ref writes); } else { var randomMessage = messages[random.Next(messages.Count)]; var headers = c.GetHeadersFor(randomMessage); string randomKey; do { randomKey = name + ".random." + random.Next(10000); } while (headers.ContainsKey(randomKey)); Interlocked.Increment(ref headersAttached); c.AttachHeader(randomMessage, randomKey, "RANDOOOOM!"); Interlocked.Increment(ref reads); } } foreach (var message in messages) { Interlocked.Add(ref headersActuallyFound, c.GetHeadersFor(message).Count); } })) .ToList(); threads.ForEach(t => t.Start()); threads.ForEach(t => t.Join()); } printStats(); Console.WriteLine("Expected total of {0} headers - found {1}", headersAttached, headersActuallyFound); }