Performance tuning EF, ASP.NET and loadtest

Web-Services-IconWe are developing webservices for accessing data in SQL Server. The performance requirement was not met until we started to tune our solution.

TLDR: use table in stead of view

EF

First we added the AsNoTracking() extension from EntityFramework to the DbContext virtual properties. This informs EF to just read the data and not track any changes. Since we’re only using the data read-only we have no need for change tracking. Speed improvement quick-win.

async-await

Next is the loadtest in Visual Studio. The Constant Load Pattern is set to 1. This means 1 thread doing all the requests. But we’ve created an ‘async’ webservice that ‘awaits’ the data from the database. Setting the number to 4 improved the requests/second by just a little.

Then we noticed the huge speed increase when requesting the same id for the second time. This happend when the number of requests exceeded the list of unique id’s and started over with already requested id’s. Is this caching of information in SQL Server?

Table in stead of view

Last change is in the database. Even with an indexed View the data is read slowly until it is requested the second time (and probably cached). We moved to a table with the same data as the view. This made the performance on par. (200+ page/s)

References

Performance Considerations for EF 4, 5 and 6 on msn
Tips to improve Entity Framework Performanc on dotnet-tricks

Posted in Development | Tagged , , , , , , | Leave a comment

Week roundup

Last week recap and links:
Image courtesy of kanate / FreeDigitalPhotos.net

Image courtesy of kanate / FreeDigitalPhotos.net

What are your best reads this week? Leave them in the comments below.

Posted in Uncategorized | Tagged , , , , | Leave a comment

Multiple service calls as data for AngularJs

angularjsWe need a composition layer for the GUI. It should combine the data from multiple services into one interactive web page. AngularJs comes to mind.

My next step in AngularJs (first step here) is to get data from multiple REST calls. The trigger is a button and the data is presented side-by-side.

First the HTML code. The button fires the getData function on data-ng-click. By using the selectedId in the function call the input is passed to the function.

<div ng-app="multiple" ng-controller="leftrightctrl">
  <div class="row">
    <input type="number" ng-model="selectedId" />
    <button data-ng-click="getData(selectedId)">Click me!</button>
  </div>
  <div class="row">
    <div class="col-md-6">
      <pre>November: {{left.name}}</pre>
    </div>
    <div class="col-md-6">
      <pre>December: {{right.name}}</pre>
    </div>
  </div>
</div>

In javascript I defined a function to get the left and the right. With getData I call both. The data is from a REST service. It is called with the id and a date. Left is november (2015-11-01) and right is december (2015-12-01).

angular
  .module('multiple', [])
  .controller('leftrightctrl', function ($scope, $http) {
    $scope.left = null;
    $scope.right = null;
    $scope.getLeft = function (id) {
     return $http.get("http://localhost/service/api/multiple/" + id + "/2015-11-01")
                 .then(function (response) {
                   $scope.left = response.data;
                 });
    };
    $scope.getRight = function (id) {
     return $http.get("http://localhost/service/api/multiple/" + id + "/2015-12-01")
                 .then(function (response) {
                   $scope.right = response.data;
                 });
    };
    $scope.getData = function (id) {
     $scope.getLeft(id);
     $scope.getRight(id);
    };
});

Reading code from others is the first step in shu-ha-ri. I’m learning.
plnkr with a live version of the code.

Posted in Development | Tagged , , | Leave a comment

My first steps with AngularJS

angularjsWe need a composition layer for the GUI. It should combine the data from multiple services into one interactive web page. AngularJs comes to mind.

As a proof of concept I decided to create a simple web page. The page requests data from a REST service and displays this in a type-ahead search box. Below is the code I created.

<div class="container body-content" ng-app="searchdemo">
   <div class="container-fluid" ng-controller="TypeaheadCtrl">
       <h4>Search</h4>
       <input type="text"
              ng-model="selectedVariable"
              placeholder="search for variable"
              uib-typeahead="var as var.Name for var in variables($viewValue)" 
              typeahead-loading="loading" 
              typeahead-no-results="noResults" 
              typeahead-min-length="3"
              class="form-control"/>
       <i ng-show="loading" class="glyphicon glyphicon-refresh"></i>
       <div ng-show="noResults">
          <i class="glyphicon glyphicon-remove"></i> No Results Found
       </div>
   </div>
</div>

The angular controller seemed to work, but no type-ahead.

Robbert looked at my code, hummed, clicked with the mouse and pointed me in the right direction. We changed the handler for the $http.get from “success” to “then”, because of the “promise” pattern (thanks Robbert)

angular.module('searchdemo', ['ui.bootstrap'])
       .controller('TypeaheadCtrl', function ($scope, $http) {
          $scope.result = new Array ({ Name : 'test' });
          $scope.variables = function (filter) {
             return $http.get("http://localhost:2305/api/relatie/" + filter)
                .then(function (response) {
                   console.debug("response received ", response);
                   return response.data;
                });
    };
});

Looks like I’ll be diving deeper into AngularJs the next days. Good times ahead 😉

Posted in Development | Tagged | 2 Comments

Week roundup

Last week recap and links:
Image courtesy of kanate / FreeDigitalPhotos.net

Image courtesy of kanate / FreeDigitalPhotos.net

What are your best reads this week? Leave them in the comments below.

https://msdn.microsoft.com/en-us/data/hh949853.aspx

Posted in Uncategorized | Tagged | Leave a comment