Finally Got Dual Monitors Working

I spent nearly 2 hours trying to get it working yesterday (or rather spent 15 minutes trying to get it working, and 1 hour 45 minutes trying to undo the damage and get my computer useable again). I decided to try again this evening and got it working in 5 minutes.

This picture is a (slightly chopped) image of my complete desktop. The original image is 3200×1024, my actual screen resolutions are 1920×1200 and 1280×1024 so the image cropped 176 pixels off the bottom of my big screen.

image

Mind blowing screen real estate! Awesome!

Setting Up a Local PHP Development Environment

I spent a good part of yesterday afternoon setting up a PHP development environment on my primary PC. I already had PHP setup to run locally under IIS, but I wanted to make some changes to a site which makes use of Apache’s mod_rewrite module for ‘routing’. I figured it would be easier to get Apache setup locally that try to find some way of rewriting URLs in IIS (version 6, not 7).

So I downloaded a Windows binary package of Apache and installed it. I had some issues with installing for current user only, so I ended up installing it for all users. Configuring Apache was fairly straightforward, but then I have a lot of experience with Apache on Linux and the configuration files on Windows are the same. I did have to tweak one of my RewriteRule directives for some reason. Frankly I can understand why I had to change it from what I had in production. What I don’t understand is why production works the way it is.

I already had both MySQL and PHP installed. I did go ahead and install the PEAR classes. I also had to grab a copy of the HTML/IT PEAR package from one of my Linux servers since I make extensive use of it and it no longer seems to be supported in the official PEAR repository. I’ll either have to switch to another PHP template solution (like Smarty) or start using a PHP framework (like CodeIgniter). For now I’ll continue with my customize HTML/IT implementation I guess.

Next I needed a PHP IDE. In the past I’ve used Dreamweaver, but I was looking for something new (and something free, LEGALLY free I mean). I’ve been trying out Aptana Studio, but I decided to try the latest release of NetBeans. So far I am pretty happy with NetBeans. It seems to integrate pretty well with my Subversion version control repository, seems stable and has pretty good intellisense. It’s also XDebug compatible, so I can debug my PHP code in the IDE like I would debug .Net code in Visual Studio. It’s already helped me fix a whole bunch of errors I wasn’t even aware of (typos, unmatched tags, etc).

Karl Seguin’s Foundations of Programming e-Book

I just finished reading Karl Seguin‘s excellent, and free, e-Book “Foundations of Programming“.

The book was brought to my attention via an invite to a lunch time session Karl is doing for the Ottawa .Net Community this Thursday. I find both the presentation and the e-book particularly timely as I am just recently getting into Test Driven Development (TDD), unit tests, mocking, Dependency Injection (DI) and Inversion of Control (IoC) frameworks.

Most of these things I’ve known about for some time, but I am just starting to actually use them all together, and I am finally starting to ‘grok’ them.

After reading the book I am looking forward to Karl’s presentation on Thursday all the more.

Investigating a .Net Web Application’s Performance Issues

Yesterday my boss and I meet with a group that has been having some significant performance issues with one of their web applications.

It’s a basic .Net web forms application (in C#). Their issue was that they had one page that was taking upwards of 75 seconds to load! Seriously. 75 seconds! They said they were using NHibernate and they had tracked the issue down to one method that was taking up the majority of those 75 seconds. They also said they had tried some direct database (Oracle) queries that returned MUCH faster than the NHibernate.

We suggested that they look at caching the results of the database queries and that they confirm exactly what queries NHibernate is making on their behalf (I think they assumed it would execute the same query they had tested directly, which may or may not be the case).

Now I am a fan of NHibernate, and I have done some things with it that resulted in less than stellar performance, but I’ve always found tweak things to near direct database performance levels without much effort by changing/correcting some basic assumptions I had about the data and model. So when I left the meeting I felt that NHibernate had unfairly been labelled as the source of the performance issues, so I decided to have a look at the code for myself.

After grabbing a local copy of the code from their source control repository, I ran the code in debug mode and quickly came to the conclusion that the method they had identified as taking a long time to execute was in fact really taking a long time to execute. So I had a closer look at that method, and here (essentially) is what I saw:

/* ... */
IQuery query = CreateQuery("SELECT ...");

for (i = 0; i < query.List().Count; i++)
{
    if ((!((object[])(new ArrayList(query.List())[i]))[0] == null)
       && (!((object[])(new ArrayList(query.List())[i]))[2] == null))
    {
        String value = (((object[])(new ArrayList(query.List())))[2]).ToString();
    }
}
/* ... */

I am “paraphrasing” a bit (the real code was worse).

The query.List() call returns an IList instance containing all the results of the SQL query. I don’t know if each call to query.List() actually executes the database query again (there may be some caching involved somewhere) but each call to query.List() did involve some database activity (according to my network sniffer). You notice that query.List is being called 3 times per loop (in the real code it was more like 6 times per loop)!

Changing this code to make one call to query.List, storing the results in a local IList variable and subsequently re-using that local variable shaved nearly 50 seconds of the response time.

On top of that there would also be improvements by removing the code that creates 3 ArrayLists per loop (again in the real code, 6 ArrayLists per loop), copies the IList of database results into each ArrayList, uses each of those ArrayLists to access only one single item and them discards the ArrayLists (only to recreate them again on the next loop).

NHibernate wasn’t the source of their problem…

A Day Spent Refactoring Code

I spent a good part of today refactoring some code I’ve inherited. It’s a VB.Net wrapper around some commercial address verification software. Two examples of the type of functionality the software provides are address correction and address formatting.

Here is a simplified version of three of the classes I was given to work with: Address, CorrectedAddress and FormattedAddress. An Address is what you would provide to the verification software and the other two classes would be returned from either the address correction or address formatting functionality.

Public Class Address
    Public AddressLine As String
    Public City As String
    Public Province As String
    Public PostalCode As String
    Public Country As String
End Class
Public Class CorrectedAddress : Inherits Address
    Public Status As CorrectedAddress.Status
    Public Message As String

    Public Enum Status
        Valid
        Invalid
        Corrected
        [Error]
    End Enum
End Class
Public Class FormattedAddress
    Public AddressLineOne As String
    Public AddressLineTwo As String
    Public AddressLineThree As String

    Public Status As FormattedAddress.Status
    Public Message As String

    Public Enum Status
        Valid
        Invalid
        [Error]
    End Enum
End Class

Note that CorrectedAddress inherits from Address, but FormattedAddress does not. Both CorrectedAddress and FormattedAddress have a Status and Message properties (implemented as public fields above only for brevity). The Status properties both return similar, but not identical enumerations. In the actual code there were six classes like these two, but I am focusing only on these two for simplicity.

The whole thing didn’t seem quite right to me, and I struggled a bit to put my finger on how this should have been implemented. But then it hit me. CorrectedAddress isn’t really an Address. It’s a result of an address correction (that happens to have an address). “Is A” versus “Has A”. Remember the oft quoted Object-Oriented design principle:

Favour Composition over Inheritance

So I pulled the Address class out of the inheritance hierarchy, renamed CorrectedAddress and FormattedAddress to CorrectedResult and FormattedResult respectively and even threw in some Generics stuff to deal with the similar but different Status enumerations.

The result (the Address class was left as-is):

Public MustInherit Class Result(Of T As Structure)
    Public Status As T
    Public Message As String
End Class
Public Class CorrectedResult : Inherits Result(Of CorrectedResultStatus)
    Public Address As Address

    Public Enum CorrectedResultStatus
        Valid
        Invalid
        Corrected
        [Error]
    End Enum
End Class
Public Class FormattedResult : Inherits Result(Of FormattedResultStatus
    Public AddressLineOne As String
    Public AddressLineTwo As String
    Public AddressLineThree As String

    Public Enum FormattedResultStatus
        Valid
        Invalid
        [Error]
    End Enum
End Class

Much better!

If Anyone is Wondering Why…

… I’ve written more blog posts for this month than I have for the previous, oh maybe, three years combined, it’s because I’ve been using Windows Live Writer to manage my blog posts (and pages). It’s a wicked tool that makes it super easy to manage blog content. In my experience so far it has flawlessly handled posting and updating my WordPress based site.

Upgrading a WordPress Site Using Subversion

image

A few weeks ago I finally made the upgrade jump from WordPress version 2.5.1 to version 2.6.2. I skipped the versions in between because while I haven’t got a lot of experience upgrading WordPress, from what experience I have had it has usually been a pain. Especially if I’ve done any kind of customization (which I have).

So when I did make the jump to 2.6.2 I resolved to do it in such a way that any future upgrades would be easy. So what’s the obvious solution to the problem of controlling versions? Version control, of course! I am a big Subversion user and luckily the creators of WordPress are Subversion users too!

Continue reading

AnkhSVN is AWESOME!

What is AnkhSVN? It’s a Subversion source control provider for Visual Studio. It allows you to use Subversion for version control in Visual Studio rather than something like Visual SourceSafe. It’s also open-source and free.

I can just hear some people saying “I’ve tried AnkhSVN. It sucks. VisualSVN is the closest you can get to working Subversion / Visual Studio integration”. I can even hear a few people saying “They both suck. Who needs IDE integration anyway. TortoiseSVN from Windows Explorer is just fine”.

I have shared both of these views at various points in the last few years. But today I discovered and tried AnkhSVN 2.0! Huge difference! The new version is a nearly complete rewrite. It’s no longer an add-in, it’s own a proper SCC provider.

Do yourself a favour and give it a try before you drop any money on VisualSVN.