Condividi tramite


PingUrl

Sometimes when testing a performance issue with SharePoint, you really just want a very simple tool that will do nothing but submit a request to the SharePoint server and tell you EXACTLY how long that request took. And sometimes you want it to automatically KEEP trying to submit that request and continue to report out exactly how long each subsequent request took. And, sometimes you want it to log out to a file exactly how long each request took so that you might look for patterns.

Well, I had exactly this need recently while working with a customer on a performance concern they had in their environment, so I quickly whipped up a utility to do exactly that. It is absolutely simple by design. It does one thing, does it very basically, and accomplishes the goal. It is called quite simply PingUrl.

PingUrl is compiled as a windows Console app (yes, PowerShell is there, but you don't always have PS at your disposal, so this is a bit simpler). Running it is simple: PingUrl followed by the URL to ping. For example:

pingurl https://www.bing.com bing.log

This will cause pingurl to submit a request to the default page at https://www.bing.com and report to the screen exactly how long that request took. This information will also be written to a log file called bing.log which will be created/appended automatically at the current directory. The ability to name the log file allows you to run multiple instances of PingUrl in different command windows but in the same directory. My customer had multiple URLs which forced requests to go to specific servers behind the load balancer, allowing us to identify performance differences between the servers. I found it to be useful and interesting... and just randomly common and esoteric to be worth sharing. So, code listed below.

If you find it useful, let me know. :)

Update 1/25/2013: At Thomas's (good) suggestion, compiled version attached. Thanks for reading!

 

Due to a quirk in the layout choices made by the MSDN Blog site, the following code may appear cut off on the right side. If you select it, copy, and paste it into another tool such as notepad, it should copy completely and correctly. Good luck!

 

 using System;
using System.Web;

namespace PingUrl
{
    class Program
    {
        static void Main(string[] args)
        {
            if (args.Length == 0)
            {
                Console.WriteLine("Please provide a URL to request. You must have appropriate permissions to request this URL and logfile name.");
                return; // Exit Application
            }

            // Prepare the log file to write
            System.IO.StreamWriter logStream = null; // null assigment resolves an "unassigned variable" error in compiler
            if (args[1] != null)
            {
                Console.WriteLine("Logging to " + args[1]);
                logStream = System.IO.File.CreateText(args[1]);
            }
            

            while (true) // Do Forever -- Control-C to exit application.
            {
                // Response captured in case writing out the response is valuable
                string response;

                // Start datetime for this request.
                DateTime started = DateTime.Now;

                try
                {
                    System.Net.WebClient client = new System.Net.WebClient();
                    client.UseDefaultCredentials = true;
                    
                    started = DateTime.Now;

                    System.IO.StreamReader reader = new System.IO.StreamReader(client.OpenRead(args[0]));

                    response = reader.ReadToEnd();

                    Console.WriteLine(started.ToString("T") + " Duration: " + DateTime.Now.Subtract(started).TotalSeconds.ToString());
                    if (args[1] != null)
                    {
                        logStream.WriteLine(started.ToString("T") + " Duration: " + DateTime.Now.Subtract(started).TotalSeconds.ToString());
                        logStream.Flush();
                    }
                }
                catch (System.Net.WebException ex)
                {
                    // Catches all HTTP errors, such as 404, 500, or access denied.
                    Console.WriteLine("Error accessing the specified URL: " + args[0]);
                    Console.WriteLine("Error Message:");
                    Console.WriteLine(ex.Message);
                }
                catch (Exception ex)
                {
                    // Catches everything else, which is likely a local error.
                    Console.WriteLine("General Execution error: " + args[0]);
                    Console.WriteLine("Error Message:");
                    Console.WriteLine(ex.Message);
                }

                // continue while loop - remember... this continues until control-C is pressed to break it.
            }

        }
    }
}

PingUrl.zip

Comments

  • Anonymous
    January 24, 2013
    Sounds great, but do you have the compiled version available for download? Not everyone has Visual Studio at at or know how to handle csc.exe :)

  • Anonymous
    January 25, 2013
    Thanks Thomas! Compiled version attached. Thanks for reading!

  • Anonymous
    January 25, 2013
    Cool trick! thanks for share!

  • Anonymous
    May 07, 2013
    Great! Thanks for Sharing it Chris. This would help me a lot :)