Solved

VB.net Webclient - trying not to cache webpage

Posted on 2015-02-12
4
204 Views
Last Modified: 2015-03-06
The main thing I am trying to do is get page load time of a website.  But it seems to work the first time I put a new website domain in, but run it again it appears to be a lot faster (cache). I dont want that, I want it make sure it does not load cache version.  Any ideas on what I did wrong below?  I'm also open to a better way to get total page load time.

Dim sw = New Stopwatch()
sw.Start()
Dim policy As New RequestCachePolicy(RequestCacheLevel.NoCacheNoStore)
Dim sourceString As String = New System.Net.WebClient().DownloadString("http://www.google.com")

 sw.Stop()
 Dim ts As TimeSpan = sw.Elapsed
        txtWebSource.Text = sourceString
        ' Format and display the TimeSpan value. 
        Dim elapsedTime As String = String.Format("{0:00}:{1:00}:{2:00}.{3:00}", ts.Hours, ts.Minutes, ts.Seconds, ts.Milliseconds / 1)
        lblPingTime.Text = elapsedTime

Open in new window

0
Comment
Question by:keith1001
  • 2
  • 2
4 Comments
 
LVL 35

Expert Comment

by:Kimputer
Comment Utility
You didn't actually use the policy. Make sure WebClient.CachePolicy is used in your code.

Please note that the numbers AFTER the first run are pretty accurate, even if you DIDN'T use the policy (I only mentioned you didn't use it so in your further .net programming, setting a variable and not using it later is senseless). Cache isn't used (you can capture your network traffic, you will see it is indeed reloaded from the server every time).
Since you used a quick website, you would think it's not accurate. If you tested on a foreign slow website, you probably wouldn't notice it.
Why the first run is always a bit higher has probably something to do with the .Net framework.
0
 

Author Comment

by:keith1001
Comment Utility
I have tested it on bigger sites, and the 1st run is always longer by a couple seconds.

So what do I need to add the set the policy to NoCache or Reload?
0
 
LVL 35

Accepted Solution

by:
Kimputer earned 500 total points
Comment Utility
Read my message again. I said even if you didn't use the policy, the results will largely STILL be the same (first run longer). You should discard the first result, it's a .NET thing. All subsequent timings are correct.
So Policy to NoCache, Reload or NOT SETTING it, it doesn't matter. Your application WILL get the files from the server, NOT from disk or memory.
0
 

Author Closing Comment

by:keith1001
Comment Utility
Sorry for long time for accepting, fighting some health issues unfortunately.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now