// TODO: look into warming up the server, this client for accuracy. void runPubSubLatency(string testName, long testCount, long testSize) { Object subcriberLock = new Object(); bool subscriberDone = false; List <long> measurements = new List <long>((int)testCount); byte[] payload = generatePayload(testSize); ConnectionFactory cf = new ConnectionFactory(); var opts = ConnectionFactory.GetDefaultOptions(); opts.Url = url; if (creds != null) { opts.SetUserCredentials(creds); } IConnection subConn = cf.CreateConnection(opts); IConnection pubConn = cf.CreateConnection(opts); Stopwatch sw = new Stopwatch(); IAsyncSubscription subs = subConn.SubscribeAsync(subject, (sender, args) => { sw.Stop(); measurements.Add(sw.ElapsedTicks); lock (subcriberLock) { Monitor.Pulse(subcriberLock); subscriberDone = true; } }); subConn.Flush(); for (int i = 0; i < testCount; i++) { lock (subcriberLock) { subscriberDone = false; } sw.Reset(); sw.Start(); pubConn.Publish(subject, payload); pubConn.Flush(); // block on the subscriber finishing - we do not want any // overlap in measurements. lock (subcriberLock) { if (!subscriberDone) { Monitor.Wait(subcriberLock); } } } double latencyAvg = measurements.Average(); double stddev = Math.Sqrt( measurements.Average( v => Math.Pow((double)v - latencyAvg, 2) ) ); // TODO: fix accuracy - trim out outliers, etc. Console.WriteLine( "{0} (us)\t{1} msgs, {2:F2} avg, {3:F2} min, {4:F2} max, {5:F2} stddev", testName, testCount, convertTicksToMicros(latencyAvg), convertTicksToMicros(measurements.Min()), convertTicksToMicros(measurements.Max()), convertTicksToMicros(stddev)); pubConn.Close(); subConn.Close(); }
void runPubSub(String testName, long testCount, long testSize) { Object pubSubLock = new Object(); bool finished = false; int subCount = 0; byte[] payload = generatePayload(testSize); ConnectionFactory cf = new ConnectionFactory(); Options o = ConnectionFactory.GetDefaultOptions(); o.Url = url; o.SubChannelLength = 10000000; if (creds != null) { o.SetUserCredentials(creds); } o.AsyncErrorEventHandler += (sender, obj) => { Console.WriteLine("Error: " + obj.Error); }; IConnection subConn = cf.CreateConnection(o); IConnection pubConn = cf.CreateConnection(o); IAsyncSubscription s = subConn.SubscribeAsync(subject, (sender, args) => { subCount++; if (subCount == testCount) { lock (pubSubLock) { finished = true; Monitor.Pulse(pubSubLock); } } }); s.SetPendingLimits(10000000, 1000000000); subConn.Flush(); Stopwatch sw = Stopwatch.StartNew(); for (int i = 0; i < testCount; i++) { pubConn.Publish(subject, payload); } pubConn.Flush(); lock (pubSubLock) { if (!finished) { Monitor.Wait(pubSubLock); } } sw.Stop(); PrintResults(testName, sw, testCount, testSize); pubConn.Close(); subConn.Close(); }