public void roundrobin_test() { var sw = Stopwatch.StartNew(); var p = new RoundRobinHostPool(); p.AddHost("a", null); p.AddHost("b", null); p.AddHost("c", null); //initially hosts are not dead, for testing of course. foreach (var h in p.HostList) { h.Dead = false; } p.GetRoundRobin().Host.Should().Be("a"); p.GetRoundRobin().Host.Should().Be("b"); p.GetRoundRobin().Host.Should().Be("c"); var respA = p.GetRoundRobin(); respA.Host.Should().Be("a"); respA.MarkFailed(); var respB = p.GetRoundRobin(); respB.MarkFailed(); var respC = p.GetRoundRobin(); respC.Host.Should().Be("c"); // get again, and verify that it's still c p.GetRoundRobin().Host.Should().Be("c"); p.GetRoundRobin().Host.Should().Be("c"); p.GetRoundRobin().Host.Should().Be("c"); var resp = p.GetRoundRobin(); resp.Should().NotBeNull(); sw.Stop(); //TOTAL TIME: 17 milliseconds Console.WriteLine($"TOTAL TIME: {sw.Elapsed.Humanize()}"); }
public void roundrobin_test() { var sw = Stopwatch.StartNew(); var p = new RoundRobinHostPool(); p.AddHost("a", null); p.AddHost("b", null); p.AddHost("c", null); //initially hosts are not dead, for testing of course. foreach( var h in p.HostList ) { h.Dead = false; } p.GetRoundRobin().Host.Should().Be("a"); p.GetRoundRobin().Host.Should().Be("b"); p.GetRoundRobin().Host.Should().Be("c"); var respA = p.GetRoundRobin(); respA.Host.Should().Be("a"); respA.MarkFailed(); var respB = p.GetRoundRobin(); respB.MarkFailed(); var respC = p.GetRoundRobin(); respC.Host.Should().Be("c"); // get again, and verify that it's still c p.GetRoundRobin().Host.Should().Be("c"); p.GetRoundRobin().Host.Should().Be("c"); p.GetRoundRobin().Host.Should().Be("c"); var resp = p.GetRoundRobin(); resp.Should().NotBeNull(); sw.Stop(); //TOTAL TIME: 17 milliseconds Console.WriteLine($"TOTAL TIME: {sw.Elapsed.Humanize()}"); }
public void bechmark_round_robin() { var p = new RoundRobinHostPool(); p.AddHost("a", null); p.AddHost("b", null); p.AddHost("c", null); //initially hosts are not dead, for testing of course. foreach (var h in p.HostList) { h.Dead = false; } var hitA = 0; var hitB = 0; var hitC = 0; var iterations = 120000; var threads = 5; var sw = Stopwatch.StartNew(); var tasks = Enumerable.Range(1, threads).Select((i) => { return(Task.Run(() => { for (var x = 0; x < iterations; x++) { var h = p.GetRoundRobin(); if (h.Host == "a") { Interlocked.Increment(ref hitA); } else if (h.Host == "b") { Interlocked.Increment(ref hitB); } else { Interlocked.Increment(ref hitC); } } })); }); Task.WaitAll(tasks.ToArray()); sw.Stop(); var hitCounts = new Dictionary <string, long>() { { "a", hitA }, { "b", hitB }, { "c", hitC } }; foreach (var kvp in hitCounts) { Console.WriteLine($"Host {kvp.Key} hit {kvp.Value} times {((double)kvp.Value / (iterations * threads)):P}"); } //TOTAL TIME: 60 milliseconds Console.WriteLine($"TOTAL TIME: {sw.Elapsed.Humanize()}"); }
public void bechmark_round_robin() { var p = new RoundRobinHostPool(); p.AddHost("a", null); p.AddHost("b", null); p.AddHost("c", null); //initially hosts are not dead, for testing of course. foreach (var h in p.HostList) { h.Dead = false; } var hitA = 0; var hitB = 0; var hitC = 0; var iterations = 120000; var threads = 5; var sw = Stopwatch.StartNew(); var tasks = Enumerable.Range(1, threads).Select((i) => { return Task.Run(() => { for( var x = 0; x < iterations; x++ ) { var h = p.GetRoundRobin(); if( h.Host == "a" ) Interlocked.Increment(ref hitA); else if( h.Host == "b" ) Interlocked.Increment(ref hitB); else Interlocked.Increment(ref hitC); } }); }); Task.WaitAll(tasks.ToArray()); sw.Stop(); var hitCounts = new Dictionary<string, long>() { {"a", hitA}, {"b", hitB}, {"c", hitC} }; foreach( var kvp in hitCounts ) { Console.WriteLine($"Host {kvp.Key} hit {kvp.Value} times {((double)kvp.Value / (iterations * threads)):P}"); } //TOTAL TIME: 60 milliseconds Console.WriteLine($"TOTAL TIME: {sw.Elapsed.Humanize()}"); }