Sunday, July 27, 2008

7z Zip and Transfer Speed Comparison

I regularly backup my databases and transfer them from the server to a local machine. My strategy is to use the 7-Zip compression utility to compress the database first and then transfer it. A smaller file will obviously transfer faster and locally I'm going to zip it anyway to save space if I'm not using it.

Today I had a situation where I was going to use the DB once I'd transfered it so it was a matter of zipping, transferring, and then unzipping. While I watched the file zip I was surprised at how long it was taking to zip and wondered if I was gaining or losing by doing the zip-transfer-unzip steps rather than just do the transfer. So I measured what I was doing.

The file size unzipped is 47Mb and zipped is 6Mb. The transfer rate is around 12 MBS (Cox Cable).

Method 1: zip - transfer - unzip:
zip: 95 seconds
transfer: 27 seconds
unzip: 3 seconds
Total: 125 seconds

Method 2: transfer
transfer: 192 seconds

Contrary to what I thought, the zip-transfer-unzip method is about 35% faster. This would obviously scale to better performance if your connection speed is slower.

Something else that you should consider is being a good net-citizen and use less bandwidth than you need to which fits in with the results of this experiment. In fact, I think if we were charged (even partially) on the bandwidth we use instead of paying a fixed amount for an unlimited amount it would have a great impact on the web's availability.

Friday, July 25, 2008

Are there enough credit card numbers?

I was wondering if the 16 digits that most credit cards have (VISA and Mastercard et al) if you don't include American Express' 13 digit cards were enough for the world or if we'd run out of digits at some point.

If I've done my calculations correctly and based on my assumption that there are 7 billion people in the world then there are enough numbers for each person on earth to have just under 1.5 million credit cards.

I then pulled out my wallet and counted that I have 16 credit card sized items in there (some of which are credit cards) and held together they are about 1cm thick. So my next calculation was to see how fat my wallet would have to be to hold 1.5 million credit cards: 937.5 meters wide, that's almost a Km.

Casting Out Nines

I'm working on a project at the moment that involves the verification of credit card numbers. This has led to running checksums on the numbers to validate them including Luhn's algorithm.

Simply stated, Luhn's algorithm doubles every second digit (starting from the right) and then adds together all of those digits which it then mods against 10 to check for success. So if you had the sequence 2568 it would be transformed into 4-5-12-8. You would then add each digit together, the 12 would become 3 (1+2) and not be a 12. So your total is 4+5+1+2+8 = 20. Mod this against ten: 20 % 10 = 0 and if 0 then it passed the test.

In code, when computing the algorithm, you may end up with a 2-digit number after doubling it. Although you could convert it to a string and then convert each digit back to a number to add them together it's easier to subtract 9 from the doubled number which will give you the same result. 12 - 9 = 3 as does 1 + 2 = 3. This, I have just learned is called "Casting Out Nines." Cool term, I like it.

Here is the C# code that I came up with:

                int total = 0;
                bool even = false;
                for (int i = digits.Length - 1; i >= 0; i--)
                {
                    int current = digits[i];
                    if (even)
                    {
                        current *= 2;
                        if (current > 9)
                        {
                            current -= 9; // cast out nines
                        }
                    }
                    total += current;
                    even = !even;
                }
 

Wednesday, July 23, 2008

Firefox style search in VisualStudio

A co-worker recently tipped me off to a way faster and easier way to search through a file in Visual Studio. I've tried this in VS2008 and not sure if it works in VS2005.

You know how you can hit Ctrl+F in Firefox and then as you type characters it will scan down the page and find the text? Well in VS you can hit Ctrl+I and it will operate in the same way. After you've hit Ctrl+I the subsequent characters that you type will search and move down the page. Once you've found your target then just hit F3 to continue the search for the next occurrence.

Quoting my co-worker "I feel cheated not having know about that hot key before." All those wasted Ctrl+F dialogs that I've had to use.

 

Tuesday, July 22, 2008

WebHost4Life

I hosted a few web sites at WebHost4Life and had a bad experience with them. My biggest complaint was the fact that their advertising was intentionally misleading and a lot of what they claimed that you got you didn't get and had to pony up extra for it. My second biggest complaint was that their support was useless and outsourced (Philippines I think) to people who couldn't understand or speak English.

I was amused to read this comment by the creator of chat-o-licious:

www.webhost4life.com has shut down my account due to the tremendous turnout from digg.com users. They demanded I upgrade my account, and told me the 'unlimited bandwidth' clause did not account for this kind of traffic.

Thanks to all the digg'ers who stopped by. Remember this the next time you're looking for a host.

This ties in directly with my experience.

My advice: stay away from webhost4life.

Thursday, July 3, 2008

Network card Speed and Duplex

We've been having a problem that's been plaguing us for weeks and today we finally solved it. We have 4 Windows Server 2003 machines running a handful of web sites in a cluster being accessed through a non-sticky VIP (Virtual IP). Some of the web sites allow uploading of files to the server which saves them to a NAS (Network Attached Storage) file share.

The problem was that two of the machines uploaded files like dogs and the other two were lightening fast. The speed difference was about 100 to 1.

The first test we ran was to make sure that it was the file share (NAS) that was the problem. We did this by setting up a test that uploaded files to each of the servers' local disks. They all ran fast. The same test was then modified to upload to the NAS file share via the servers and that isolated and confirmed the problem to be the NAS part of the equation.

It turned out that the problem was that the Speed & Duplex setting on the dog slow machines was marked as Auto. Changing this to 100 Mb Full solved this problem.

To get to this setting, on Windows Server 2003 click on Start -> right click My Computer and select Properties to open up the System Properties dialog. Select the Hardware tab and click on Device Manager to open the Device Manager window. Select the active network card and right click and bring up the properties dialog for that network adapter. On the properties dialog box for that adapter select the Advanced tab and in the Property list select Speed & Duplex and from the Value drop down select 100 Mb Full.

I hope this post helps at least just 1 person save several hours.

Wednesday, July 2, 2008

Get link list from HTML page with regular expressions

Here's a little bit of code that I wrote to strip out the anchor links from an HTML page using regular expressions. The final line of the example code will only work if you're testing this in Joseph Albahari's excellent LINQPad. i.e. Dump() is an extension method that he's added to IEnumerable.

String text = @"<html>
                <head><title>Development Projects</title></head>
                <body>
                <ul>
                    <li><a href=""http://linttrap.domain.com"">linttrap</a></li>
                    <!--
                    <li><a href=""http://help.domain.com"">help</a></li>
                    -->
                    <li><a href=""http://help2.domain.com"">help2</a></li>
                    <li><a href=""http://help3.domain.com"">help3</a></li>
                    <li><a href=""http://gdhelp.domain.com"">gdhelp</a></li>
                    <li><a href=""http://helpadmin.domain.com"">help admin</a></li>
                </ul>
                </body>
            </html>";
Regex linkRegex = new Regex(" href=\"([^\"]*)\"");
List<String> links = new List<String>();
MatchCollection matches = linkRegex.Matches(text);
foreach (Match m in matches) {
    links.Add(m.Groups[1].Value);
}
links.Dump("Links");

Here's the output from the Dump() function:

▪ Links

5List<String>

http://linttrap.domain.com

http://help.domain.com
http://help2.domain.com
http://help3.domain.com
http://gdhelp.domain.com
http://helpadmin.domain.com

 

Tuesday, July 1, 2008

Identity Impersonate

This is a reminder to myself to set

<system.web>
    <identity impersonate="true"/>
</system.web>

in the web.config after setting up a web site otherwise it won't be able to access external resources such as file shares that require the login/password that you've set for the site.