.NET Core MVC Thread Pool: Sequential vs Async Performance

I’ve been working with .NET Core lately and am really enjoying C#. I was interested in finding out more about how to increase RPS in a backend web environment. Some online research shows that .NET threads are heavy, and that .NET uses M:M threading, so the OS will do the context switching between threads. This is especially concerning in a web application because I/O intensive operations such as Database and API calls will cause threads to block and use up system resources.

Luckily, .NET provides Async / Await calls that convert supported Async calls to callback-based calls and free-up the thread to perform other work while waiting on I/O. I couldn’t find much documentation on how exactly .NET Core MVC’s request handler worked. My testing revealed that it uses a dynamically-sized thread pool to respond to incoming requests, and reuses threads when async controllers are used.

Sequential Behavior

  // GET api/values/sleep
  [HttpGet("sleep")]
  public string GetSleep()
  {
      Thread.Sleep(1000);
      return Process.GetCurrentProcess().Threads.Count.ToString();
  }

This MVC API controller uses Thread.Sleep to sleep the currently executing thread. Performance is quite poor; even though the request only sleeps for 1 second, the backlog of requests causes the response time to backup to around 57 seconds at load.

This chart does verify one important thing though - .NET Core MVC is using a dynamic thread pool to serve requests. As the request volume increases, .NET adds more system threads to process requests.

Async to the Rescue

  // GET api/values/delay
  [HttpGet("delay")]
  async public Task GetDelay()
  {
      await Task.Delay(1000);
      return Process.GetCurrentProcess().Threads.Count.ToString();
  }

What a relief! Task.Delay use async/await, and mean response time is right where it should - 1s. .NET’s thread pool stays steady, between 21-22 threads the entire time. It looks like in both case, .NET starts out with around 21 threads on the first request. Most of these are probably framework threads such as Kestrel, the HTTP Server the Garbage Collector, etc. We’re really just interested in anything over the initial 21 threads.

If there’s one takeaway from this, it is to find a database driver that implements async methods properly, and make all of the API Controllers that you can use these async methods. .NET will efficiently reuse the threads in it’s dynamic thread pool and you’ll be able to squeeze way more performance out of each web server.


Docker Performance Benchmark: Bare Metal, VirtualBox, and Hyper-V

The development world is quickly making the shift to Docker and microservices, and every day I feel like I’m running more Docker containers. My daily laptop is an Macbook Pro (Early 2015), but I’ve recently had the chance to test the Dell XPS 15 (9550) as well. In doing so, I got to wondering what the performance penalty for running containers through a Virtual Machine (such as Boot2Docker or Kitematic) as opposed to running on Bare Metal Linux.


Yii2 PHP 7 Performance

PHP 7.0.0 was just tagged yesterday, and I was interested to see if it could live up to the performance claims that Zend made. Yii2 is my PHP framework of choice, so I built PHP 7.0.0 from source, stress tested one of my application’s pages, and compared performance to PHP 5.6.4.

Benchmark Results

The maximum acceptable levels here for web performance are PHP 5.6.4 serving between 75-100 RPS at between a 31ms-176ms response time and PHP 7.0.0 serving 175 RPS at a 47ms response time.

PHP 7.0.0 does a very nice job here- about 2x the performance for serving Yii2! It seems that it is definitely worth upgrading.


Buzzfeed What State do you Actually Belong In Revealed

Buzzfeed recently released a quiz that has been getting a lot of attention on my Facebook news feed, titled “What State Do You Actually Belong In?“. The quiz consists of 12 sets of 12 questions and then produce an answer of a state that you supposedly closely identify with. According to Buzzfeeed, I belong in Minnesota, which is far too cold and obviously incorrect, so I decided to do some digging to figure out how their quiz system works.

buzzfeed-state-quiz

To view the states that Buzzfeed assigned to each quiz answer, view my What State Do You Actually Belong In Revealed (PDF). To see how I created this file, continue reading…


Yii Framework Active Record INSERT … ON DUPLICATE KEY UPDATE

Recently I was programming in the Yii Framework and ran into a situation where I wanted to use INSERT … ON DUPLICATE KEY UPDATE in an Active Record. In my research, I discovered that this is specific to MySQL. In my case, this was still be a valuable requirement for my Web Application and was worth extending Yii to get working. My approach was as follows:


Alpine UTE-42BT Review and Teardown

In the past few years, an interesting new option has arisen in the Music Industry – the option to purchase music “As A Service” is now extremely popular through services such as Spotify, Google Play Music, etc. I instantly fell in love with Google Play Music All Access and needed a way to play it in my car through my HTC One (M7) Android cell phone. I decided to upgrade my car stereo to the Alpine UTE-42BT Digital Media Receiver. Here’s my review after spending 2 months with the Alpine UTE-42BT.

Alpine UTE-42BT


JVC KD-X250BT Review

In my quest to bring my vehicle’s audio system up to 2013 standards, I started out with a search at crutchfield.com with the criteria of Bluetooth Playback and no CD Player. My specific use-case is playing Google Play Music All Access over Bluetooth through my HTC One (M7) to the Car Stereo. The search returned a few “Digital Media Receivers” (DMRs) and the first unit that I ordered was the JVC KD-X250BT. Here’s my review after spending a month with the JVC KD-X250BT.

JVC KD-X250BT


Aluminum Unibody MacBook Optical Drive Noisy, Buzzing, and Vibrating Fix

I have had my Late 2008 Aluminum Unibody MacBook for over 3.5 years now. I had not used my optical drive in quite some time, and went to watch a DVD today. The optical drive made a loud buzzing sound and seemed to vibrate more than usual. I opened the MacBook up and discovered that downward pressure on the optical drive made the problem worse, while wedging a screwdriver under the optical drive and applying pressure upwards made the noise disappear.

After the Aluminum Unibody MacBook’s introduction in Late 2008, Apple quickly changed the MacBook Pro lineup to the Aluminum Unibody design and all MacBook Pros from Mid 2009 through Mid 2012 have used a similar unibody design. This fix may also work for other MacBooks, MacBook Pros, or other Computers with slot-load Optical Drives. The fix does require disassembling the optical drive and will most likely void your warranty.

The fix is to disassemble the Optical Drive, bend the bottom aluminum cover of it outwards to relieve the pressure on the disc that causes the buzzing sound and vibration, and reassemble the Optical Drive. Here are pictures and instructions on how to do this:

Step 0. Open up your MacBook so that you can see all of the internals.

Step 1. Remove Connectors and Screws

Disconnect the Optical Drive connector and the Connector that covers the Optical Drive L-Bracket screw. Unscrew the speaker and the T-Shaped mount that covers the optical drive.


Dojo / Dojox Mobile mblDomButton Icon List for Android and iPhone Themes

The Dojo Mobile Android and iPhone themes contain default icons that can be used in widgets, such as dojox.mobile.ListItem and dojox.mobile.ToolBarButton widgets. These icons all start with the prefix “mblDomButton”. I could not find a detailed list of these icons, so I searched through the Android and iPhone CSS files to find everything with the mblDomButton prefix. I have an Android and iPhone page listing these icons and displaying them, both shown below.


Automatically Take EBS Snapshots and Delete Old Ones with PHP Script

I am a big fan of Amazon’s RDS product, which takes automated nightly snapshots of your RDS Storage and deletes old snapshots after a specified amount of time. After reading that Amazon’s EBS drives sustain a 0.1% – 0.5% Annual Failure Rate, I also wanted to take automated nightly snapshots of my EBS drives. Deleting snapshots after a certain number of days was also of interest, because this way I am not over-paying for snapshot storage at Amazon or removing snapshots manually. I also wanted to email the results to myself after backups were taken.

I did some searching on Google and found a nice script by Chris at Applied Trust Engineering, Inc. that runs PHP from the command line to create an automatic snapshot. I used this script as an example to setup my script, so thanks, Chris. My script has support for:

  • Backup Multiple EBS Volumes
  • Protect against script running again and creating another snapshot too soon
  • Delete Snapshots after a Specified Period of Time
  • Script outputs Detailed Snapshot Information for these 5 categories:
    • Snapshots that succeeded
    • Snapshots that failed and had errors
    • Old Snapshots that were removed
    • Old Snapshots that had errors while trying to remove
    • Snapshots that were preserved
  • Includes PHPMailer code to email results of script to yourself

Script Setup

You will need: (all links open in new window)

  • My ebs_backup.php Code (download is a zip) (or look below)
  • Stores snapshot information in “./snapshot_information.json” – Make sure PHP can write this file
  • Configure the lines of code within the configuration comment blocks
  • Run script periodically to your needs with CRON or whatever
  • Requires AWS PHP SDK be configured for your AWS Account: http://aws.amazon.com/sdkforphp/
  • Optional PHPMailer Support to email results to yourself: http://phpmailer.worxware.com/ (configure PHPMailer at the very bottom of the script)

Screenshot of Output

Automatic EBS Snapshots