ServiceStack SelfHosted Performance Boost

#c# #servicestack #selfhosted

ServiceStack is doing a great work in the REST API world with the plus given by mono support.

The basic way of Self Hosting uses the .NET HttpListener to power up a single thread serving requests, this quickly show up as the performance blottle neck (Basic Self Hosted).

Digging a little I found the extended (concurrent model on self hosted) that uses a thread pool to increase the overall performance.
(Thread Pool Self Hosted).

From my little experience with async in C#, I had the idea that maybe the awesome “smartthreadpool” library can be used to handle the async part of the self hosted application, so I give it a try.

The test bed, the ultra simple Hello service, with a little artificial work. (hard work I think).

[Route("/sayHello/{Name}")]
public class HelloRequest : IReturn<HelloResponse> {
    public string Name { get; set; }
}

public class HelloResponse {
    public string Message { get; set; }
    public string Status { get; set; }
}

public class HelloService : Service, IGet<HelloRequest> {
    private static Random r = new Random();

    public object Get(HelloRequest request) {
        var timer = new Stopwatch();

        timer.Start();

        while(timer.ElapsedMilliseconds < 100)
            Thread.SpinWait(1000000);

        timer.Stop();

        return new HelloResponse() {
                                       Message = "hello " + request.Name,
                                       Status = "OK -> " + timer.ElapsedMilliseconds
                                   };
    }
}

The use of the “SpinWait” aim to simulate real scenarios where work is actually performed, and the thread is not available until finished (unlike the Thread.Sleep() that release the thread to be used).

The test machine has the following specs.

Core i5 3570K
8GB DDR3 1866MHZ @ 1666MHZ
ASUS P8Z77 VLK
OCZ VERTEX 4 128GB
WIN 8 x64

the load test is performed using JMeter, all the applications tested were build on release mode with visual studio 2012, targeting .NET 3.5 and monitored by sysinternal process explorer.

First Scenario: AppHostHttpListenerBase

JMeter Results for AppHostHttpListenerBase

Imagen 2 de ServiceStack SelfHosted Performance Boost

Second Scenario: AppHostHttpListenerLongRunningBase

Imagen 3 de ServiceStack SelfHosted Performance Boost

Imagen 4 de ServiceStack SelfHosted Performance Boost

Third Scenario: SmartThreadPool AppHostHttpListener (better response time average)

Imagen 5 de ServiceStack SelfHosted Performance Boost

Imagen 6 de ServiceStack SelfHosted Performance Boost

Fourth Scenario: SmarthThreadPool Build targeting Framework 4 (better response time and better throughput)

Imagen 7 de ServiceStack SelfHosted Performance Boost

Imagen 8 de ServiceStack SelfHosted Performance Boost

The Others, not really impressive but can be used. Task Library and ThreadPool.

Task Library

Imagen 9 de ServiceStack SelfHosted Performance Boost

ThreadPool

Imagen 10 de ServiceStack SelfHosted Performance Boost

the full code of this test CODE

Comentarios GitHub Discussions