Created
September 5, 2012 01:00
-
-
Save darrelmiller/3628687 to your computer and use it in GitHub Desktop.
Perf testing Web API
This file contains hidden or 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
// Server | |
using System; | |
using System.Collections.Generic; | |
using System.Diagnostics; | |
using System.Linq; | |
using System.Net.Http; | |
using System.Net.Http.Headers; | |
using System.Text; | |
using System.Threading; | |
using System.Threading.Tasks; | |
using System.Web.Http; | |
using System.Web.Http.SelfHost; | |
namespace PerfWebApiServer | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
var config = new HttpSelfHostConfiguration("http://localhost:9102"); | |
config.Routes.MapHttpRoute("default", "", new {Controller = "Root"}); | |
config.MessageHandlers.Add(new ThroughputMessageHandler(new ConsoleLogger())); | |
var host = new HttpSelfHostServer(config); | |
host.OpenAsync().Wait(); | |
Console.Read(); | |
host.CloseAsync().Wait(); | |
} | |
} | |
public interface ILogger | |
{ | |
void LogException(string category, TraceLevel level, Exception exception); | |
void Log(string category, TraceLevel level, Func<string> messageCallback); | |
} | |
public class ConsoleLogger : ILogger | |
{ | |
public void LogException(string category, TraceLevel level, Exception exception) | |
{ | |
Console.WriteLine(exception.Message); | |
} | |
public void Log(string category, TraceLevel level, Func<string> messageCallback) | |
{ | |
Console.WriteLine(messageCallback()); | |
} | |
} | |
public class RootController : ApiController | |
{ | |
public HttpResponseMessage Get() | |
{ | |
var httpResponseMessage = new HttpResponseMessage() | |
{ | |
Content = new StringContent("Hello World") | |
}; | |
httpResponseMessage.Headers.CacheControl = new CacheControlHeaderValue() { MaxAge = new TimeSpan(0, 0, 0, 20) }; | |
return httpResponseMessage; | |
} | |
} | |
public class ThroughputMessageHandler : DelegatingHandler | |
{ | |
private readonly ILogger _logger; | |
private Timer _timer; | |
private int _count; | |
public ThroughputMessageHandler(ILogger logger) | |
{ | |
_logger = logger; | |
_count = 0; | |
_timer = new Timer(new TimerCallback(timerCallback),null,1000,1000); | |
} | |
private void timerCallback(object state) | |
{ | |
_logger.Log("perf",TraceLevel.Verbose,() => _count + " req/sec"); | |
_count = 0; | |
} | |
protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) | |
{ | |
return base.SendAsync(request, cancellationToken) | |
.ContinueWith((t) => { | |
lock (this) | |
{ | |
_count++; | |
} | |
return t.Result; | |
}); | |
} | |
} | |
} | |
// Client | |
using System; | |
using System.Net.Http; | |
namespace PerfWebConsole2 | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
var httpClient = new HttpClient(); | |
while (true) | |
{ | |
var response = httpClient.GetAsync("http://pearl:9102").Result; | |
} | |
} | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment