Blog moved to http://www.andrevdm.com/

Sunday 11 August 2013

Upgrading to ANTLR 4 with C#

Upgrading from ANTLR 3.x to ANTLR 4 was pretty painless. Here are the changes I needed to make to get it all working

  1. Get ANTLR 4 from nuget (you will need to allow pre-release versions for now)
  2. Change your ANTLR build script to reference the new ANTLR JAR. The JAR is included in the nuget. This what my build script looks like
        set classpath=C:\xxx\packages\Antlr4\tools\antlr4-csharp-4.1-SNAPSHOT-complete.jar
        java org.antlr.v4.Tool xxx.g4 -Dlanguage=CSharp_v4_0
  3. Rename grammars from .g to .g4
  4. Remove the options block or change it to match your C# version selection
       options
       {
          language=CSharp_v4_0;
       }
  5. Tokens should be comma delimited not semicolon delimited
  6. Always use a $ when referring to parameters, return variables, tokens etc. ANTLR 3.4 did not always enforce this so if you forgot it in a few places you will need to fix them
  7. You can still check if a optional token exists just be sure to use the .ctx. E.g. use this
       if( $i.ctx != null )
  8. You may get errors when using properties on matched tokens. Parentheses will fix this. E.g.
       ($i).Text
  9. Use “-> skip” rather than “{$channel=HIDDEN}”
  10. Use “.*?” rather than “options {greedy=false;}”

Hope this helps someone

Tuesday 28 May 2013

Building for mono and Microsoft .NET

Recently I wanted to build my project on mono and Microsoft .NET. What I wanted to do was
  1. Build on windows using Microsoft .NET as per usual
  2. Build using mono on windows
  3. Build using mono on linux
  4. Use nuget package restore on all three
Getting this all working was not terribly difficult but I did have to do a fair amount of googling. Here is what I needed to do to hopefully this will save someone some time.

Installing the latest version of mono
Windows:
  1. Download the 3.x from http://www.go-mono.com/mono-downloads/download.html
  2. Create dmcs.bat in C:\Program Files (x86)\Mono-3.0.10\bin as this is missing in the latest download
    1. See https://bugzilla.xamarin.com/show_bug.cgi?id=8813
    2. REM dmcs.bat compatibility shim for mcs
      @echo off
      call mcs -sdk:4 %*
Linux:
  1. See http://www.meebey.net/posts/mono_3.0_preview_debian_ubuntu_packages/

  2. Add this line to your /etc/apt/sources.list file:
       deb http://debian.meebey.net/experimental/mono /

  3. apt-get update
  4. apt-get install mono-complete




Creating a mono solution and projects

Mono’s xbuild is not yet 100% compatible with msbuild. MonoDevelop is also not 100% compatible with the VS 2012 solution/project format. So to make my life easier I’ve written a simple C# script that generates mono solutions and projects from the VS2012 ones.

This is pretty simple and works really well. It also means I can easily have separate output folders for the windows and mono binaries.

Get the source from my gist at https://gist.github.com/andrevdm/5655285#file-updateprojectfileversion-cs




Getting NuGet & package restore working on linux
  1. Import the required certificates
    1. See http://stackoverflow.com/questions/15181888/nuget-on-linux-error-getting-response-stream/16589218#comment24184791_16589218
    2. sudo mozroots --import --machine --sync
    3. sudo certmgr -ssl -m https://go.microsoft.com
    4. sudo certmgr -ssl -m https://nugetgallery.blob.core.windows.net
    5. sudo certmgr -ssl -m https://nuget.org

  2. Create a mono specific NuGet.target

      1. See http://nuget.codeplex.com/SourceControl/changeset/view/0b1e224884a3#src/Build/NuGet.targets

      2. Or use my version (minor modifications) that works with the project generator above
        https://gist.github.com/andrevdm/5660800#file-nuget-mono-targets

There is also some great info on NuGet here

  1. http://www.lextm.com/2013/01/how-to-use-nuget-on-mono-part-i.html
  2. http://www.lextm.com/2013/01/how-to-use-nuget-on-mono-part-ii.html
  3. http://www.lextm.com/2013/02/debugging-on-mono-xbuild-issue.html





Build scripts

Finally to wrap it all up here are build scripts for linux and windows


Linux
#!/bin/bash
export EnableNuGetPackageRestore=true
mono ./makeMonoProjectsAndSln.exe MySolution.sln
xbuild /p:TargetFrameworkProfile="" MySolution.mono.sln




Windows
@echo off

makeMonoProjectsAndSln.exe MySolution.sln

"C:\Program Files (x86)\Mono-3.0.10\bin\xbuild.bat" /p:TargetFrameworkProfile="" MySolution.mono.sln

Friday 29 March 2013

Efficiently Tracking Response Time Percentiles (in C#)

 

When looking for a better way to track response times than a simple min/max/average statistic recently I found a great article that had a clever solution. This article shows how to efficiently track the n-th percentile performance while storing only a small amount of data. See the full original article here http://techblog.molindo.at/2009/11/efficiently-tracking-response-time-percentiles.html

The original code is in Java but I needed it in .NET so I’ve created a .net version on github (https://github.com/andrevdm/PercentilePerformance). I chose to do a complete rewrite rather than porting the Java code so the class names etc will be different. The idea however is the same.

My .net version can generate output in three formats

PNG

clip_image002

HTML

image

 

Text

image

 

I hope this proves useful to someone.

Monday 25 February 2013

Learning AngularJs

 

I recently had to build a simple HTML application and decided to use AngularJS. I’ve used KnockOut in the past and found it easy to use. AngularJS is a little more opinionated than KO so there is a bit more that needs to be done to get a basic app working. However it is still simple and easy to follow and the end result definitely justifies the tiny bit of extra work.

Where I did have a problem was in understanding how the change tracking works. In KnockOut it is very clear, observables do all of the work and observables are easy to understand. In AngularJS there are no observables and everything works like “magic”. This so far has been my biggest issue with AngularJS. I find it hard to simply follow a set of rules without any understanding of the reasoning behind them. When I started building a slightly more complicated application it refused to work correctly and I could not work out why with the “magic” explanation.

After a little bit of searching I was able to come up with an explanation of how it worked and based on that some guidelines for structuring my app. The end result was very impressive. The code was simple, I had separated concerns and everything just worked. Overall I’ll definitely be using AngularJS more.

 

Pushing back the magic

Firstly AngularJS uses dirty tracking rather than observables. It will periodically scan you scope variables to see if their values have changed from the previous scan and if they have it will then updated the UI and fire the change events. That is it, pretty simple actually.

The other thing that greatly simplified my application was to use broadcast messages rather than trying to share things across the $rootScope. This allowed me to have controllers that are completely separate. Interaction between the controllers is always event-based. This also avoids complexities that arise when you are going across scopes and having to check what phase angular is in

 

Example

As a simple example I’ll show how I structured a simple tabbed interface. As this is not a post about angular binding but rather about the JS structure I’ve just bound directly to JSON text

This is what the UI looks like. There are two buttons that represent the tab pages and an area for the tab content.

image

 

Here is overall structure. Red arrows show the controllers responsible for each section of the view. Green arrows show the message broadcasting.

 

image

 

The code for the HTML page is straightforward

<!doctype html>
<html ng-app="PerformanceApp">
<head>
<script src="angular.min.js"></script>
<script src="PerformanceApp.js"></script>
<script src="NodePerformanceCtrl.js"></script>
<script src="NodesCtrl.js"></script>

<link href="cluster.css" rel="stylesheet" type="text/css" />

<title>Cluster overview</title>
</head>
<body>
<div class="tabs">
<button ng-click="CurrentTab='Nodes'" ng-class="{'selTab': CurrentTab=='Nodes', 'unSelTab': CurrentTab!='Nodes'}">Nodes</button>
<button ng-click="CurrentTab='NodePerformance'" ng-class="{'selTab': CurrentTab=='NodePerformance', 'unSelTab': CurrentTab!='NodePerformance'}">Performance</button>
</div>

<div class="tab" ng-controller="NodesCtrl" ng-show="CurrentTab=='Nodes'">
<div class="tabHeader">Nodes</div>
<span class="tabBody">
<pre>{{Nodes|json}}</pre>
</span>
</div>

<div class="tab" ng-controller="NodePerformanceCtrl" ng-show="CurrentTab=='NodePerformance'">
<div class="tabHeader">Performance</div>
<span class="tabBody">
<pre>{{Performance|json}}</pre>
</span>
</div>

</body>
</html>


 



The “tab” buttons show or hide sections by setting the value of the CurrentTab scope variable. The “tabs” are just divs each with its own controller.



This is the application initialisation code



var app = angular.module('PerformanceApp', []);

app.run( function( $timeout, $http, $rootScope ){
$rootScope.CurrentTab = "Nodes";

var machines = [
{"Name": "m1", "IP": "127.0.0.1"},
{"Name": "m2", "IP": "127.0.0.2"}
];

$timeout(
function(){
$rootScope.$broadcast( 'machinesUpdated', machines );
},
500 );

} );


Again nice and simple. On app.run




  1. the default tab is set


  2. A dummy list of machines is created. In the real app this is fetched using ajax


  3. A broadcast message is schedule in 500ms



 



The controllers then respond to the broadcast and update their local scope variables



function NodePerformanceCtrl($scope,$http,$timeout) {
$scope.Performance = {};

$scope.$on( "machinesUpdated", function( event, args ){
for( var m in args ){
var machine = args[m];

$scope.Performance[machine.Name] = machine.IP;
}

} )
}


 



Though this is a trivial example it does show how AngularJS helps you layout your application. It should also illustrate how using $broadcast messages help keep your controllers separated.