One of the keys for performance testing in SharePoint is determining how long your
page takes to load for the average user. There are lots of tools out there to measure page rendering time, but when we using
SharePoint framework, we want to give our site administration the ability to
monitor in the browser and to indentify the slow pages in the site.
Implementation
First, let’s build our ping method to calculate the page render time and the content
size.
The following procedure describes the steps used to request a SharePoint page from
a server, for example, a List View or Homepage.
1. Create a WebRequest instance by calling Create with the URI of the resource.
WebRequest webRequest = WebRequest.Create(url);
2. Enable authentication, Use the user deafalt credentials.
webRequest.UseDefaultCredentials = true;
3. start measuring page rendering time.
DateTime startTime = DateTime.Now;
4. Send the request to the server, call GetResponse.
WebResponse webResponse = webRequest.GetResponse();
5. Page rendering time finished - Stop measuring page rendering time.
DateTime stopTime = DateTime.Now;
6. Calculate the total render time.
TimeSpan duration = stopTime - startTime;
7. Retrieve the number of bytes returned.
long responseContentLength = webResponse.ContentLength
Here is what the ping method looks like:
private void PingUrl(string url)
{
try
{
WebRequest webRequest = WebRequest.Create(url);
webRequest.UseDefaultCredentials = true;
DateTime startTime = DateTime.Now;
WebResponse webResponse = webRequest.GetResponse();
DateTime stopTime = DateTime.Now;
string status = (((HttpWebResponse)webResponse).StatusDescription);
TimeSpan duration = stopTime - startTime;
long responseContentLength = webResponse.ContentLength;
webResponse.Close();
}
catch (Exception ex)
{
//handel error
}
}
Scans all site pages
To be able to scans all site pages, like: lists views navigation items in the site
and aspx pages in your site, we will need to find them first.
Get all Lists and Views in your site
To gets all lists and views that are contained in the our site, we can use these
two collections:
• SPListCollection
• SPViewCollection
Syntax :
SPWeb web = SPContext.Current.Web;
SPListCollection webLists = web.Lists;
foreach (SPList list in webLists)
{
SPViewCollection listViews = list.Views;
foreach (SPView view in listViews)
{
//call ping method
}
}
Get site Homapage
We can simply use the SPWeb.Url property to retrieve the default home page.
Syntax:
SPWeb web = SPContext.Current.Web;
string url = web.Url;
//call ping method
Get all publishing pages
The PublishingWeb class provides publishing specific behavior for a site that supports
publishing, including access to pages lists.
Syntax:
if (PublishingWeb.IsPublishingWeb(web))
{
Guid pagesListId = PublishingWeb.GetPagesListId(web);
SPList pagesList = web.Lists[pagesListId];
SPListItemCollection pagesListItems = pagesList.Items;
foreach (SPListItem pageItem in pagesListItems)
{
if (PublishingPage.IsPublishingPage(pageItem))
{
//call pin method
}
}
}
Get all site navigation links
In SharePoint we have two main navigations, the Global Navigation and the Quick Launch.
For both we can use the SPNavigationNodeCollection to retrieve all navigation
links.
Syntax:
SPNavigationNodeCollection globalNodes = web.Navigation.GlobalNodes;
foreach (SPNavigationNode node in globalNodes)
{
//call ping method
}
SPNavigationNodeCollection quickLaunchNodes = web.Navigation.QuickLaunch;
foreach (SPNavigationNode node in quickLaunchNodes)
{
//call ping method
}
Create a WSP SharePoint solution
Now that we gather all relevant page load information, we need a nice user interface
to be use by site administrations. Here we will use an application page on the
Site Settings page (settings.aspx).
Add a Link on Site Settings
To add a link on the Site Administration section in the Site Settings page, we can
use this element manifests:
<?xml version="1.0" encoding="utf-8" ?>
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
<CustomAction
Id="UserInterfaceCustomActions.SiteSettings"
GroupId="SiteAdministration"
Location="Microsoft.SharePoint.SiteSettings"
Sequence="306"
Title="URL Ping Tool">
<UrlAction Url="_LAYOUTS/UrlPingTool.aspx"/>
</CustomAction>
</Elements>
Create the Application Page
After adding the link we need to create the actually application page (ASPX), in
my previous article I describe how to do this. SharePoint Application Page with Custom Controls
Deploy
To deploy the application page on all SharePoint sites, we can create a Farm feature
that installs this page on the site settings:
<Feature Id="{GUID}"
Title="Url Ping Tool"
Description="The Url Ping Tool is a farm feature in SharePoint that provide additional
performance and tracing information that can be used to troubleshoot issues with
page rendering time. "
Version="12.0.0.0"
Hidden="FALSE"
Scope="Farm"
DefaultResourceFile="core"
xmlns="http://schemas.microsoft.com/sharepoint/">
<ElementManifests>
<!-- reference to the element manifests -->
<ElementManifest Location="elements.xml"/> </ElementManifests>
</Feature>
Summary
Large lists, custom web parts or any custom solution can have performance impact
on your site. In order to capture page load information, we develop a custom
tool to troubleshoot rendering performance issues.
Download
The complete solution (WSP) can be downloaded on CodePlex: http://upt.codeplex.com/
You can also browse the source code here