Thursday, 9 February 2012

Web Browser Process Statistics Using Windows PowerShell

I use a number of web browsers on my Windows PC. One of them is Google Chrome, which I have been using from not long after its initial release. From Wikipedia: “A multi-process architecture is implemented in Chrome where, by default, a separate process is allocated to each site instance and plugin.” This makes it awkward to work out its memory consumption. It is in fact possible to obtain this information from Chrome itself, though I only discovered that quite recently. Chrome has its own task manager with which you can report such statistics. Tools –> Task Manager –> Stats for nerds displays the results in a tab called About Memory. It also reports stats for other running browsers. Here are some stats from the top of the About Memory tab:

image

Notice that here it only reports the usage for the Chrome processes minus plugins and extensions. To get the total figure you need to view the figure at the bottom.

image

Windows PowerShell is also able to calculate the total memory consumption by summing up all the processes named Chrome:

$p = (Get-Process Chrome | Measure-Object -Sum WorkingSet).Sum / 1024

Write-Host "Total = "$p" K"

This produces a similar result (consumption fluctuates from moment to moment):

Total =  621508 K

Chrome’s About Memory also produces stats for Firefox. However, once again this excludes plugins and extensions. So Chrome doesn’t help us out here. We can write similar code for Firefox but this time we also need to include another process called plugin-container of which there may be zero or more depending on whether the current Firefox instance has had to start one up or not (i.e., whether user has happened to run Flash or a PDF reader). The code for this is slightly more involved:

$f = (Get-Process Firefox | Measure-Object -Sum WorkingSet).Sum / 1024

Write-Host "Firefox Total = "$f" K"

$p = (Get-Process "plugin-container" -ErrorAction SilentlyContinue | Measure-Object -Sum WorkingSet).Sum / 1024

Write-Host "Plugin Container Total = "$p" K"

$c = $f + $p

Write-Host "Combined Total = "$c" K"

The first part is the same except for substituting Firefox for Chrome. Then we define another variable for summing up the plugin-container processes. Adding the two variables together gives us the total consumption.

Firefox Total =  556116 K
Plugin Container Total =  45536 K
Combined Total =  601652 K

But notice there’s some extra code we had to use:

-ErrorAction SilentlyContinue

This is required because if there are no active plugin-container processes PowerShell will report an error. The SilentlyContinue argument does what it says.

The current release of PowerShell is v2.0. It is included by default in Windows 7 and Windows Server 2008 R2. It is also available as a free download for Windows XP SP3, Vista and Servers 2003 and 2008.

Microsoft’s package manager, NuGet, for Visual Studio 2010 also makes use of PowerShell in its console window. PowerShell comes with a basic script editor supplied by Microsoft but there are more powerful IDEs out there. A good one is PowerGUI, which also has excellent IntelliSense amongst other capabilities. It also has an add-in for Visual Studio 2010 if desired.

Monday, 23 January 2012

New Year, New Language

Functional programming languages are all the rage at the moment. They’re well-suited to parallel programming  and the multi-core world. On the Microsoft .NET platform we have F#. I’ve made one or two attempts at learning F# before but lost heart once the going got tough. This time around I’ve decided to make more of an effort. I’ve found that it helps to try more than one learning source as they differ in the degree of explanation they give for each concept.

Thus far I am consulting primarily F# Programming, Real World Functional Programming (online partial version of the book) and MSDN’s F# Language Reference.

I’ve been thinking about whether the learning-curve from procedural to object-oriented programming is greater than that from OO (or procedural) to functional.

I think the harder part about going from procedural to OO was not the mechanics but OO design perhaps. Whereas with functional I think even the mechanics are quite difficult.

However, it could be that I’ve just forgotten how difficult the procedural to OO transition was!

One initial difficulty with F#, especially for those coming from a C-syntax background, is F#’s syntax. It does look quite alien. Syntax itself should not be that big a deal but when combined with new concepts it does add to the mental load, especially once examples start to get elaborate.

A similar language on the Java JVM is Scala. Its syntax is a cross between C-syntax and Ruby/Python’s. I looked briefly at Scala some time ago and it does seem more accessible initially. Though once you get beyond the basics it becomes as scary as F#! A colleague of mine who’s been using Scala commercially for many months tells me it’s a matter of practice. Blogger Labels: Functional,.NET,Microsoft,F#,Java,Scala,Ruby,Python

Thursday, 7 July 2011

Reactive Extensions 1.0 Stable is Released

Some months ago Microsoft made Reactive Extensions (Rx) an officially supported product and moved it out of Dev Labs to its new site. On June 29th it was officially released as version 1.0.  It now also has some very accessible starter documentation in MSDN. Until now documentation has been scattered between videos, blogs, hands on labs and the MSDN Rx forum.

Rx is also consumable from LINQPad. LINQPad subscribes to the observables that you dump. For example the example below writes “Hello World” every second but stops after the first five. If we removed the call to Take(5) it would run for ever. In that case you can stop it by hitting the Stop button in LINQPad.

image

Thursday, 30 June 2011

Leveraging LINQPad’s Object Visualisation in Visual Studio

When you run a query in LINQPad it produces formatted output on the results tab that you can optionally export to Word, Excel or HTML. Here I show two techniques for visualising any arbitrary .NET object.

The first way is non-invasive, i.e., requires no change to your Visual Studio solution or compiled assemblies.

The second way is invasive but allows you to export any arbitrary .NET object to HTML by leveraging LINQPad’s Dump() extension method.

Technique 1: Add a Visual Studio LINQPad Debugger Visualizer

Download the LINQPad Visualizer from Google Code and follow the instructions from the point where it says “If using the download do this:.”

You should also remember to unblock the DLLs after downloading. Consider this simplified code with fields made public for brevity.

class User
{
public int UserId;
public List<Course> Courses;
public List<Student> Students;
}

class Course
{
public string CourseCode;
}

class Student
{
public int StudentId;
}


Set up a collection of Users.



var users = new List<User> {
new User {
UserId = 1,
Courses = new List<Course> {
new Course {
CourseCode = "ECU120"
},
new Course {
CourseCode = "ECU121"
},
new Course {
CourseCode = "ECU122"
},
},
Students = new List<Student> {
new Student {
StudentId = 1
},
new Student {
StudentId = 2
},
}
},
new User {
UserId = 2,
Courses = new List<Course> {
new Course {
CourseCode = "ECU124"
},
new Course {
CourseCode = "ECU125"
},
new Course {
CourseCode = "ECU126"
},
},
Students = new List<Student> {
new Student {
StudentId = 1
},
new Student {
StudentId = 3
},
}
},
new User {
UserId = 3,
Courses = new List<Course> {
new Course {
CourseCode = "BESU022"
},
new Course {
CourseCode = "BESU023"
},
new Course {
CourseCode = "ECT034"
},
},
Students = new List<Student> {
new Student {
StudentId = 1
},
new Student {
StudentId = 2
},
}
},
};

users.Dump();



Then we can debug into it.



image



To do this set a watch for your object of interest. Here we are inspecting the users collection. In order to view its contents we need to enter



new System.WeakReference(users)



in the Name column as shown.



Clicking on the dropdown for Value shows two LINQPad debugger visualizers. The default visualizer is a JSON visualizer that allows us to see the contained values of objects that haven’t been marked as Serializable. Clicking on the visualizer pops up a Windows Form containing the object’s values.



image



In this case we lose the type information but we can still see all the values neatly laid out.



However, if we mark the User, Student and Course classes as Serializable



[Serializable]
class User
{
public int UserId;
public List<Course> Courses;
public List<Student> Students;
}

[Serializable]
class Course
{
public string CourseCode;
}

[Serializable]
class Student
{
public int StudentId;
}



Then select the lower LINQPad visualizer we get the type information as well and with neater layout.



image



Technique 2: Add a Reference to LINQPad.exe



For this technique we add a reference to LINQPad.exe that allows us to leverage LINQPad’s Dump() extension method for any object…



/// <summary>
///
LINQPad extension methods.
/// </summary>
public static class LinqPadExtensions
{
/// <summary>
///
Writes object properties to HTML
/// and displays them in default browser.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="o"></param>
/// <param name="heading"></param>
public static void Dump<T>(
this T o,
string heading = null
)
{
string localUrl =
Path.GetTempFileName() + ".html";
using (
var writer =
LINQPad.Util.CreateXhtmlWriter(true)
)
{
if (!String.IsNullOrWhiteSpace(heading))
writer.Write(heading);
writer.Write(o);
File.WriteAllText(localUrl, writer.ToString());
}
Process.Start(localUrl);
}
}



Then we can write users.Dump() as in the example above. On execution this writes the object’s values to HTML and launches the default web browser. This produces the same results as the typed version above.



image



This method has been adapted from an example on StackOverflow. One issue to be aware of is that your project should target the full .NET 4, not the Client Profile.

Thursday, 18 November 2010

ASP.NET MVC and LINQ to SQL – LINQPad to the Rescue

Recently I was rewriting  a broken algorithm in some ASP.NET MVC code.  For part of the fix I needed to write some LINQ to SQL. I needed to do a comparison against a nullable field but I was thinking in C# not in SQL. Here is a simplified version of the real problem. Suppose we have a table of customers where LastName is a nullable field. We want to return customers whose last name is NULL.

string lastName = null;

var customers =
from c in Customers
where c.LastName == lastName
select c;


This was unexpectedly returning no values. The correct solution is



var customers =
from c in Customers
where c.LastName == null
select
c;


The reason was due to the “IS NULL vs. = NULL”  issue in the underlying SQL. This was easy to check by launching the excellent LINQPad application that allows you to view the underlying SQL after running a LINQ statement. I’d just assumed that from the C#/LINQ side it would automatically generate the appropriate code.



Comparing directly against null from the C# generates



SELECT [t0].[CustomerID], [t0].[FirstName], [t0].[LastName]

FROM [Customers] AS [t0]

WHERE [t0].[LastName] IS NULL



Comparing against a variable that’s null generates



DECLARE @p0 VarChar(1000) = null

SELECT [t0].[CustomerID], [t0].[FirstName], [t0].[LastName]

FROM [Customers] AS [t0]

WHERE [t0].[LastName] = @p0



In my real problem I was comparing against a nullable ID column and I wanted to return both null and non-null matches in one clause of a compound where. So I wanted to do a more elaborate version of this.



string name = (lastName != null) ? "Fred" : null;

var customers =
from c in Customers
where c.LastName == name
select c;


But I was forced to use an if-else instead.



if (lastName != null)
{
var customers =
from c in Customers
where c.LastName == lastName
select c;
// ...more code
}
else
{
var customers =
from c in Customers
where c.LastName == null
select
c;
// ...more code
}


I couldn’t think of a more concise way around this. Anyway, at least I solved the bigger problem of which this was a part. :)

Wednesday, 25 August 2010

JavaScript Unit Testing with QUnit

Recently, I’ve been trying to learn more about the Reactive Extensions library (currently still in Microsoft DevLabs) which I’ve blogged about previously. Since then Microsoft have pushed out a JavaScript version of the library and published a couple of Hands-on Labs for both the .NET and JavaScript versions. I thought I’d start looking at the JavaScript version as well. However, since the lab makes use of the jQuery JavaScript library it helps to be familiar with it too. So I’ve spent some time with jQuery as well. This in turn led me to realise that I needed to learn more about JavaScript in general.

In following Matthew Podwysocki’s Rx for JavaScript tutorials (not got very far yet) I noticed that he makes use of QUnit, a JavaScript unit testing library that is an offshoot of jQuery. So I made a diversion into this. It turns out that there are a number of unit testing libraries for JavaScript, but I’d not bothered to look into them before. Although QUnit is an offshoot of jQuery, and is what jQuery itself uses to test jQuery, it can be used for generic JavaScript. I have a number of JavaScript utility methods so after an initial play with QUnit I thought I’d try creating a few tests. Lo and behold I discovered a bug in the first method I tried.  Ah, the benefits of unit testing.

There’s a one-page runnable example on the home page and a couple of tutorial links. Following the lead of the second tutorial I created a web site in Visual Studio, added the tester page and then separate “project under test” and unit tests JavaScript files.

<script type="text/javascript" src="Scripts/qunit.js"></script> 
<!-- Library -->
<script type="text/javascript" src="Scripts/utility.js"></script>
<!-- Tests -->
<script type="text/javascript" src="Scripts/tests.js"></script>


I have a method called isInteger(text) that checks whether a text entry is a [positive] integer. Its unit tests are:



/// <reference path="utility.js" />
module('isInteger Tests');

test('isInteger_positiveInteger_true', function () {
var text = '32';
ok(isInteger(text), text + ' is an integer');
})

test('isInteger_negativeInteger_false', function () {
var text = '-32';
ok(!isInteger(text), text + ' is not an integer');
})

test('isInteger_zero_true', function () {
var text = '0';
ok(isInteger(text), text + ' is an integer');
})

test('isInteger_decimal_false', function () {
var text = '32.3';
ok(!isInteger(text), text + ' is not an integer');
})

test('isInteger_alpha_false', function () {
var text = 'abc';
ok(!isInteger(text), text + ' is not an integer');
})

test('isInteger_alphanumeric_false', function () {
var text = 'abc123';
ok(!isInteger(text), text + ' is not an integer');
})

test('isInteger_whitespace_false', function () {
var text = ' ';
ok(!isInteger(text), 'Whitespace text is not an integer');
})

test('isInteger_empty_false', function () {
var text = '';
ok(!isInteger(text), text + 'Empty text is not an integer');
})



QUnit produces out put like this (showing tests 1 to 5 for isInteger with some tests collapsed):



image



This is just the simplest usage of QUnit. It has some more assertions and also an asynchronous testing capability. It is this that Matthew Podwysocki uses in one of his Rx for JavaScript blog posts.

Friday, 11 June 2010

A Useful New String Method in .NET 4.0

In .NET 2.0 the String class introduced a method, IsNullOrEmpty, that allows you to check whether a string is null or the Empty string. However, sometimes you also want to disallow a string that contains just white space. I created a static utility method called IsNullOrEmptyOrBlank that additionally checked for white space.

public static bool IsNullOrEmptyOrBlank(string s)
{
return String.IsNullOrEmpty(s) || s.Trim().Length == 0;
}



In .NET 3.5 I turned this into an extension method.



public static bool IsNullOrEmptyOrBlank(this string s)
{
return String.IsNullOrEmpty(s) || s.Trim().Length == 0;
}



Then to my pleasure I noticed that in .NET 4.0 Microsoft has fixed this oversight and added this functionality to the String class.  The method is IsNullOrWhiteSpace. The description is: “Indicates whether a specified string is null, empty, or consists only of white-space characters.”



Kudos to Microsoft.  :-)