Created
September 5, 2012 20:43
-
-
Save HenrikFrystykNielsen/3644432 to your computer and use it in GitHub Desktop.
Tweaked perf run
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
using System; | |
using System.Net; | |
using System.Net.Http; | |
using System.Threading; | |
using System.Threading.Tasks; | |
using System.Web.Http.SelfHost; | |
namespace leveldb_sharp_sample | |
{ | |
public class WebHost | |
{ | |
private static readonly string _baseAddress = "http://henrikn-10:50231"; | |
private HttpSelfHostServer host; | |
public void Open() | |
{ | |
var config = new HttpSelfHostConfiguration(_baseAddress); | |
config.MessageHandlers.Add(new ShortcutMessageHandler()); | |
host = new HttpSelfHostServer(config); | |
host.OpenAsync().Wait(); | |
Console.WriteLine("Listening on {0}", _baseAddress); | |
} | |
public void Close() | |
{ | |
host.CloseAsync().Wait(); | |
} | |
public class ShortcutMessageHandler : DelegatingHandler | |
{ | |
public static Int64 count; | |
protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) | |
{ | |
HttpResponseMessage response = new HttpResponseMessage() | |
{ | |
StatusCode = HttpStatusCode.OK, | |
Content = new StringContent("HELLO WORLD"), | |
}; | |
return Task.FromResult(response); | |
} | |
} | |
} | |
internal class Program2 | |
{ | |
//private static Timer timer = new Timer(TimerCallback, null, 1000, 1000); | |
private static DateTime lastUpdated; | |
private static Int64 clientCount; | |
private static void Main(string[] args) | |
{ | |
var host = new WebHost(); | |
host.Open(); | |
#if false | |
ServicePointManager.DefaultConnectionLimit = Int32.MaxValue; | |
for (int i = 0; i < 4; i++) | |
{ | |
Task.Factory.StartNew(() => | |
{ | |
var httpClient = new HttpClient(); | |
while (true) | |
{ | |
Interlocked.Increment(ref clientCount); | |
var response = httpClient.GetAsync("http://localhost:50231").Result; | |
} | |
}); | |
} | |
#endif | |
Console.WriteLine("Hit ENTER to exit..."); | |
Console.ReadLine(); | |
} | |
private static void TimerCallback(object state) | |
{ | |
var count = WebHost.ShortcutMessageHandler.count; | |
var clientCount = Program2.clientCount; | |
var elapsed = DateTime.Now - lastUpdated; | |
Console.WriteLine("Client: {0}\t\tServer: {1}", clientCount / elapsed.TotalSeconds, count / elapsed.TotalSeconds); | |
Interlocked.Exchange(ref WebHost.ShortcutMessageHandler.count, 0); | |
Interlocked.Exchange(ref Program2.clientCount, 0); | |
lastUpdated = DateTime.Now; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment