Skip to content

Instantly share code, notes, and snippets.

@rmueller
Last active September 9, 2015 18:51
Show Gist options
  • Save rmueller/3e794488b963d0fa997e to your computer and use it in GitHub Desktop.
Save rmueller/3e794488b963d0fa997e to your computer and use it in GitHub Desktop.
class Program
{
private static HttpSelfHostServer server;
public static int Timeout = 3000;
static void Main(string[] args)
{
if (args.Length > 0)
Timeout = Int32.Parse(args[0]) * 1000;
Console.WriteLine(System.Runtime.InteropServices.RuntimeEnvironment.SystemConfigurationFile);
System.Net.ServicePointManager.DefaultConnectionLimit = Int32.MaxValue;
var selfHostConfiguraiton = new HttpSelfHostConfiguration(string.Format("http://localhost:{0}", 3333));
int workerThreads, ioThreads;
ThreadPool.GetMaxThreads(out workerThreads, out ioThreads);
Console.WriteLine("Max threads {0} ioThreads {1}", workerThreads, ioThreads);
ShowNumberOfThreads();
selfHostConfiguraiton.Routes.MapHttpRoute(
name: "DefaultApiRoute",
routeTemplate: "api/{controller}/{action}/{id}",
defaults: new { id = RouteParameter.Optional });
selfHostConfiguraiton.MaxBufferSize = int.MaxValue;
selfHostConfiguraiton.MaxConcurrentRequests = int.MaxValue;
selfHostConfiguraiton.MaxReceivedMessageSize = int.MaxValue;
server = new HttpSelfHostServer(selfHostConfiguraiton);
server.OpenAsync().Wait();
Console.ReadLine();
}
public static void ShowNumberOfThreads()
{
int workerThreads;
int ioThreads;
ThreadPool.GetAvailableThreads(out workerThreads, out ioThreads);
Console.WriteLine("Available threads {0} ioThreads {1}", workerThreads, ioThreads);
}
}
public class TesteController : ApiController
{
private static int asyncCounter = 0;
private static int counter = 0;
[HttpPost]
public Task<HttpResponseMessage> ReceiveAsync()
{
Interlocked.Increment(ref asyncCounter);
Console.WriteLine("Requests received: {0}", asyncCounter);
Program.ShowNumberOfThreads();
return Request.Content.ReadAsStringAsync()
.ContinueWith(x =>
{
Sleep();
return new HttpResponseMessage(HttpStatusCode.OK)
{
Content = new StringContent(x.Result, Encoding.UTF8, "text/plain")
};
});
}
private static void Sleep()
{
Thread.Sleep(Program.Timeout);
}
[HttpPost]
public HttpResponseMessage Receive()
{
Interlocked.Increment(ref counter);
Console.WriteLine("Requests received: {0}", counter);
var c = Request.Content.ReadAsStringAsync().Result;
Program.ShowNumberOfThreads();
Sleep();
return new HttpResponseMessage(HttpStatusCode.OK) { Content = new StringContent(c, Encoding.UTF8, "text/plain") };
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment