0

I have a strange problem with my script in powershell, I want to examine the average time of downloading page. I write script which fires frequently. But sometimes my script returns result 0, which means it downloads site in 0 ms. If i modified my script to save whole site to the file when the download time is about 0ms it doesn't saves anything. And I'm interesting if I do something wrong, or powershell function isn't too accurate to count such "small" times.

ps. other "good" results are about 4-9 ms.

Here is a part of my script which responds to count the download time:

$StartTime = Get-Date
$PageDownload = $Request.DownloadString("mypage.com")
$TimeTaken = ((Get-Date) - $StartTime).TotalMilliseconds 
Cœur
  • 37,241
  • 25
  • 195
  • 267
MNie
  • 1,347
  • 1
  • 15
  • 35

1 Answers1

3

Get-Date should be as precise as the system clock is.

There could be web caching going on. Unfortunately, disabling caching for WebClient is not possible, from what I see elsewhere. The "do it right" method is to construct your own Http request with the TcpClient class, but that's also pretty complex.

One easy way to make sure you're not being cached is to put an arbitrary value as a GET request. It's a hack, but it is often enough to fool a cache. So, instead of:

"http://mypage.com"

You use:

"http://mypage.com?someUnusedValueName=$([System.Environment]::TickCount)"
Community
  • 1
  • 1
Bacon Bits
  • 30,782
  • 5
  • 59
  • 66