ServiceStack SelfHosted Performance Boost

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

 

 

Second Scenario: AppHostHttpListenerLongRunningBase

 

 

Third Scenario: SmartThreadPool AppHostHttpListener (better response time average)

 

 

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

 

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

Task Library

 

ThreadPool

 

the full code of this test  CODE

 

Calculator in Assembly with NASM on Windows

Calculator in Assembly

this is a basic calculator written in assembly, it has some nice features, it was developed for the laboratory of  the Computer Architecture course of the Computer Science Engineering on the USAC (so it is an useful tool for learning purposes)

  1. Written fully in assembly, no external library for input/output, just interruptions.
  2. Internally use a 128 bit representation for holding operators and result. The requirement was operate (+,-) with numbers up to 20 digits, and (*,/) with numbers up to 10 digits.
  3. Multiplication using repeated addition operations instead of the native “mult”.
  4. Divide by repeated subtraction and counting instead of the native “div”.
  5. Convert an ASCII String to Binary Number and vice versa. this is required to handle user input, and to display results.
  6. Basic Input/Output functions, like GetCh, PutCh, NewLine, WriteLine
  7. Custom Input/Output functions to read and write 128bit numbers, like Read128BitNum, Write128BitNum
  8. Basic Menu in Text Mode.
  9. Easy to modify, build. The package include the assembler and linker so the executable can be created anytime. (using DOSBox if you are running on a modern operating system)

For Example, in this image sequence, we can see a simple addition.

assembler calculator main menu

Calculator Main Menu

assembler calculator addition result

Calculator after execution of a addition operation.

And for large numbers (123456789 + 987654321)

assembler calculator with large numbers

Calculator after execution of an addition of two large numbers.

Continue reading

PowerShell, Load Configuration From XML File

When you need create a script file with a complex parameter set, sometimes a better option can be load a file with configuration instead of using a large set of arguments on script invoke.

I found this solution when I faced a very small problem, I need create a script that can replace a XML Element in a target document using another XML Document. basically it’s a template file, and its associated segments.

By Example I had a template document with  this content.

<?xml version="1.0"?>
<sections>
    <section_a>
    </section_a>
    <section_b>
    </section_b>
</sections>

and two files with the actual content of each section.

<?xml version="1.0"?>
<section_a>
    <content_a/>
</section_a>
<?xml version="1.0"?>
<section_b>
    <content_b/>
</section_b>

And, finally a configuration file, that define the map, SectionName -> FileName, this map allow to define a variable number of sections to be replaced, using files, this configuration file was intentionally defined with the same format of the App.config or Web.Config  AppSettings section.

<?xml version="1.0"?>
<configuration>  
  <appSettings>
    <add key="section_a" value="sectiona.xml" />    
    <add key="section_b" value="sectionb.xml" />            
  </appSettings>
</configuration>

So the first step was load section map file, the configuration, that file defines which segment file is used to replace a section).

function LoadSectionConfigMap( $configurationFilePath )
{
    $result = @{}
    $config = [xml](get-content $configurationFilePath)
    
    $configDir = Split-Path $configurationFilePath -parent
    
    foreach ($addNode in $config.configuration.appsettings.add) {
     if ($addNode.Value.Contains(‘,’)) {
      # Array case
      $value = $addNode.Value.Split(‘,’)
      for ($i = 0; $i -lt $value.length; $i++) { 
        $value[$i] = $value[$i].Trim() 
      }
     }
     else {
      # Scalar case
      $value = $addNode.Value
     }
     $key = $addNode.Key     
     $result[$key] = Join-Path $configDir $value     
    }
    return $result
}

So, if we had a folder called “C:temp” with the above files on it.

we can call the function with this parameter “C:tempsectionMap.xml” and we get a the name of the section related to the filename with the actual content of that section.

The function to load the configuration from the XML File is pretty specific to this case, but can be generalized to be used on any kind of configuration value.

function LoadSectionConfigMap( $configurationFilePath )
{
    $result = @{}
    $config = [xml](get-content $configurationFilePath)
             
    foreach ($addNode in $config.configuration.appsettings.add) {
     if ($addNode.Value.Contains(‘,’)) {
      $value = $addNode.Value.Split(‘,’)
      for ($i = 0; $i -lt $value.length; $i++) { 
        $value[$i] = $value[$i].Trim() 
      }
     }
     else {
      $value = $addNode.Value
     }
     $key = $addNode.Key     
     $result[$key] = $value    
    }
    return $result
}

so we can use it to load any value.

this will be the first post of a serie, the next one will be focused how to replace the sections with the content in the segment files.