Skip to content

Commit

Permalink
Tidy and comment, remove unnecessary calculations
Browse files Browse the repository at this point in the history
  • Loading branch information
tomwardill committed May 31, 2012
1 parent 08da899 commit 41b27df
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 24 deletions.
33 changes: 12 additions & 21 deletions BoxedIce.ServerDensity.Agent/Checks/NetworkTrafficCheck.cs
Expand Up @@ -55,8 +55,8 @@ public object DoCheck()
else
{

//if (!results.ContainsKey(key))
// {
if (!results.ContainsKey(key))
{
results.Add(key, new Dictionary<string, long>());

// we need to check if these have overflowed
Expand All @@ -73,7 +73,7 @@ public object DoCheck()
// Store now for calculation next time.
_networkTrafficStore[key]["recv_bytes"] = recv_overflow[1];
_networkTrafficStore[key]["trans_bytes"] = trans_overflow[1];
// }
}

}

Expand All @@ -87,42 +87,33 @@ public object DoCheck()
/// Check if the value has overflowed and reset to 0
/// http://connect.microsoft.com/VisualStudio/feedback/details/734915/getipv4statistics-bytesreceived-and-bytessent
/// AGENT-199
/// Factored into a separate method for testing
/// </summary>
/// <param name="toCheck">string of parameter to look up</param>
/// <param name="toCheck">string of parameter to look up (recv / trans)</param>
/// <param name="store">Past results</param>
/// <param name="currentValue">current value from the results</param>
/// <returns>Value with overflow taken into account</returns>
public List<long> CheckForOverflow(string toCheck, Dictionary<string, long> store, long currentValue)
{
// make up the strings we need to check in the dictionaries
var bytesString = toCheck + "_bytes";
var overflowString = toCheck + "_overflow";

// if the last was higher than our current, we've overflowed
// overflow occurs at UInt32.MaxValue
// so if we subtract that in this situation, we'll get the actual delta
if (currentValue < store[bytesString])
{
if (!store.ContainsKey(overflowString))
{
store[overflowString] = 1;
store[bytesString] -= UInt32.MaxValue;
}
else
{
store[overflowString] += 1;
store[bytesString] -= UInt32.MaxValue;
}
}

var overflowValue = store.ContainsKey(overflowString) ? store[overflowString] : 0;
store[bytesString] -= UInt32.MaxValue;
}

var values = new List<long>();

// calculate the delta
//values.Add((currentValue + (UInt32.MaxValue * overflowValue)) - store[bytesString]);
values.Add(currentValue - store[bytesString]);

// we need the 'raw' value for the next time round
//values.Add(currentValue + (UInt32.MaxValue * overflowValue));
values.Add(currentValue);
// pass back the overflow we used, it's useful for debugging
values.Add(UInt32.MaxValue * overflowValue);

return values;

Expand Down
3 changes: 0 additions & 3 deletions BoxedIce.ServerDensity.AgentTests/NetworkCalculations.cs
Expand Up @@ -23,7 +23,6 @@ public void StandardNetworkTraffic()
var result = check.CheckForOverflow("recv", store, 100);
Assert.AreEqual(result[0], 90);
Assert.AreEqual(result[1], 100);
Assert.AreEqual(result[2], 0);
}

[TestMethod]
Expand All @@ -37,7 +36,6 @@ public void HigherStandardNetworkTraffic()
var result = check.CheckForOverflow("recv", store, 30001000);
Assert.AreEqual(result[0], 1000);
Assert.AreEqual(result[1], 30001000);
Assert.AreEqual(result[2], 0);
}

[TestMethod]
Expand All @@ -51,7 +49,6 @@ public void StandardNetworkTrafficWithOverFlow()
var result = check.CheckForOverflow("recv", store, 100);
Assert.AreEqual(200, result[0]);
Assert.AreEqual(100, result[1]);
Assert.AreEqual(UInt32.MaxValue, result[2]);
}

[TestMethod]
Expand Down

0 comments on commit 41b27df

Please sign in to comment.