I recently began my foray into ASP.net with a small project at work. Part of the project displays the ping response time. To accomplish this, I wanted to show the response time and use Bootstrap progress bars to indicate slow response times. In ColdFusion/Railo, this is simple enough with CFIF. However, in the code-behind world of ASP.net things are a bit more complicated. As a counterpoint ColdFusion doesn’t have a ping function baked in; even though there are several UDFs available on the internet.
ASPX Code
So, first, we have our ASP.net code. I use one asp:Label inside an asp:Repeater to hold the server name that will be pinged. The second asp:Label will actually display the response time and be styled with Bootstrap progress bars to indicate slow response times.
<ul class="list-group small"> <li class="list-group-item">Response Time<br /> <asp:Label ID="serverNameLabel" runat="server" Visible="false" Text='<%# Eval("Server_Name") %>'></asp:Label> <div class="progress"> <asp:Label ID="responseTime" runat="server"></asp:Label> </div> </li></ul>
Code Behind
Next, in the code behind file, we see the OnItemDataBound function that runs for each row returned in the query. Once we define the response time label, we can use the Attributes.Add() function to add class and style definitions to the page element. On a local network, response times are a generally much faster than response times across the internet. As such, we expect response times to be less than 4ms.
protected void serverRepeater_ItemDataBound(object sender, RepeaterItemEventArgs e) { Label tempServerName = (Label)e.Item.FindControl("serverNameLabel"); Label tempRespTime = (Label)e.Item.FindControl("ResponseTime"); string address = tempServerName.Text; System.Net.NetworkInformation.Ping ping = new System.Net.NetworkInformation.Ping(); try { System.Net.NetworkInformation.PingReply reply = ping.Send(address); tempRespTime.Text = reply.RoundtripTime.ToString() + "ms"; if (reply.RoundtripTime < 4) { tempRespTime.Attributes.Add("class", "progress-bar progress-bar-success"); tempRespTime.Attributes.Add("style", "width:15%;"); } else if (reply.RoundtripTime >= 4 && reply.RoundtripTime <= 8) { tempRespTime.Attributes.Add("class", "progress-bar progress-bar-warning"); tempRespTime.Attributes.Add("style", "width:60%;"); } else if (reply.RoundtripTime > 8) { tempRespTime.Attributes.Add("class", "progress-bar progress-bar-danger"); tempRespTime.Attributes.Add("style", "width:100%;"); } } catch (Exception ex) { throw ex; } }
Visual Effect
Finally, we can see the various states of response times.
Green for everything less than 4ms.
Orange indicates that the response time is between 4ms and 8ms. Locally, we generally regard this as cause for concern if the condition persists.
Red indicates that there is an unacceptable response time greater than 8ms.
Leave a Reply
You must be logged in to post a comment.