Created
October 18, 2013 08:17
-
-
Save yallie/7038238 to your computer and use it in GitHub Desktop.
Shared proxy with lock / proxy per thread / proxy per request benchmark.
Proxy per thread shows the best performance.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Compile: csc test.cs /r:Zyan.Communication.dll | |
// Run server: test.exe | |
// Run client: test.exe /client | |
// Results: | |
// Shared proxy with lock benchmark. Time elapsed: 13.22 | |
// Per thread proxies benchmark. Time elapsed: 5.05 | |
// Per request proxies benchmark. Time elapsed: 5.33 | |
using System; | |
using System.Collections; | |
using System.Diagnostics; | |
using System.Linq; | |
using System.Security.Principal; | |
using System.Threading; | |
using System.Threading.Tasks; | |
using Zyan.Communication; | |
using Zyan.Communication.Protocols.Tcp; | |
using Zyan.Communication.Security; | |
struct Program | |
{ | |
const int Port = 1234; | |
const string ServerName = "SampleServer"; | |
const int Iterations = 10000; | |
static void Main(string[] args) | |
{ | |
if (args.Any()) | |
RunClient(args); | |
else | |
RunServer(); | |
} | |
static void RunServer() | |
{ | |
var auth = new NullAuthenticationProvider(); | |
var protocol = new TcpDuplexServerProtocolSetup(Port, auth, false); | |
using (var host = new ZyanComponentHost(ServerName, protocol)) | |
{ | |
host.RegisterComponent<IRequestCounter, RequestCounter>(); | |
Console.WriteLine("Server started. Press ENTER to quit."); | |
Console.ReadLine(); | |
} | |
} | |
static void RunClient(string[] args) | |
{ | |
var clientProtocol = new TcpDuplexClientProtocolSetup(false); | |
var serverUrl = clientProtocol.FormatUrl("localhost", Port, ServerName); | |
using (var conn = new ZyanConnection(serverUrl, clientProtocol)) | |
{ | |
Console.WriteLine("Connected to server. Running benchmarks..."); | |
BenchmarkLocks(conn); | |
BenchmarkPerThreadProxies(conn); | |
BenchmarkPerRequestProxies(conn); | |
} | |
} | |
static void BenchmarkLocks(ZyanConnection conn) | |
{ | |
var proxy = conn.CreateProxy<IRequestCounter>(); | |
var initialValue = proxy.Increment(); | |
var sw = Stopwatch.StartNew(); | |
Parallel.For(1, 100, x => | |
{ | |
var loopValue = 0; | |
while (loopValue - initialValue < Iterations) | |
{ | |
lock (proxy) | |
loopValue = proxy.Increment(); | |
} | |
}); | |
sw.Stop(); | |
Console.WriteLine("Shared proxy with lock benchmark. Time elapsed: {0:s\\.ff}", sw.Elapsed); | |
} | |
static void BenchmarkPerThreadProxies(ZyanConnection conn) | |
{ | |
var initialProxy = conn.CreateProxy<IRequestCounter>(); | |
var initialValue = initialProxy.Increment(); | |
var sw = Stopwatch.StartNew(); | |
Parallel.For(1, 100, x => | |
{ | |
var proxy = conn.CreateProxy<IRequestCounter>(); | |
while (proxy.Increment() - initialValue < Iterations) | |
{ | |
} | |
}); | |
sw.Stop(); | |
Console.WriteLine("Per thread proxies benchmark. Time elapsed: {0:s\\.ff}", sw.Elapsed); | |
} | |
static void BenchmarkPerRequestProxies(ZyanConnection conn) | |
{ | |
var proxy = conn.CreateProxy<IRequestCounter>(); | |
var initialValue = proxy.Increment(); | |
var sw = Stopwatch.StartNew(); | |
Parallel.For(1, 100, x => | |
{ | |
while (conn.CreateProxy<IRequestCounter>().Increment() - initialValue < Iterations) | |
{ | |
} | |
}); | |
sw.Stop(); | |
Console.WriteLine("Per request proxies benchmark. Time elapsed: {0:s\\.ff}", sw.Elapsed); | |
} | |
} | |
public interface IRequestCounter | |
{ | |
int Increment(); | |
} | |
public class RequestCounter : IRequestCounter | |
{ | |
private static int requestCount = 0; | |
private static long lastRequestTime = DateTime.Now.Ticks; | |
public int Increment() | |
{ | |
var result = Interlocked.Increment(ref requestCount); | |
if (DateTime.Now.Subtract(new DateTime(lastRequestTime)).Seconds >= 2) | |
{ | |
lastRequestTime = DateTime.Now.Ticks; | |
Console.WriteLine("Request count: {0}", requestCount); | |
} | |
return result; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment