Calculating Download Time
By Randal L. Schwartz
It's a simple problem, really. You click on a link to a site. The site starts loading, but you're on a slow modem dialup. So it keeps loading, and loading, and loading. And the little icon in the upper right of your browser continues its animation as if that will keep you from noticing that the page still hasn't finished loading. Finally, just when you're about to hit "stop," it's done.
Why are these pages so big? Why are most pages so unfriendly to slow connections? I suspect it's because most pages are being designed within corporations that have T1 lines, and nobody ever bothers to test the pages from their home ISP connection. And that's unfortunate.
It would be fairly simple to write a test program that downloads the entire page, with all of its links, and reports how many bytes and how much time it takes to satisfy the browser. Of course, the program would have to go through all of the HTML looking for embedded images, sounds, objects (like Flash), and frame links. Hmm, sounds like a lot of work unless you have the right tools, like Perl's wonderful LWP library. With this in mind, I wrote such a program, and it's shown in
Listing 1.
Line 1 turns on warnings a good thing to have while developing. Line 2 enables the common compiler restrictions, forcing us to declare our variables, and avoid the use of barewords and symbolic references. Line 3 enables autoflushing on STDOUT, so each line of output will be immediately visible as it's printed.