Skip to content

Commit

Permalink
Added link to page to enable forcing GC.
Browse files Browse the repository at this point in the history
Counting bytes sent/recv more efficiently.
Added Peak sent/recv counters.
  • Loading branch information
DamianEdwards committed Dec 9, 2011
1 parent def7b3d commit faaa07a
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 40 deletions.
20 changes: 19 additions & 1 deletion flywheel/Default.aspx
Expand Up @@ -48,6 +48,7 @@

<div>
<a id="resetAvg" href="#">Reset average</a>
<a id="forceGC" href="#">Force GC</a>
</div>
</fieldset>

Expand Down Expand Up @@ -98,9 +99,26 @@
$payloadSize.change(function () {
hub.setBroadcastSize($payloadSize.val());
});
$("#resetAvg").click(function () {
$("#resetAvg").click(function (e) {
e.preventDefault();
hub.resetAverage();
});
$("#forceGC").click(function (e) {
/// <param name="e" type="jQuery.Event">Description</param>
var link = $("#forceGC"),
text = link.text(),
href = link.prop("href");
e.preventDefault();
link.text("Collecting...")
.prop("href", "");
hub.forceGC().done(function () {
link.text(text)
.prop("href", href);
});
});
}
$.connection.hub.start(init);
Expand Down
98 changes: 60 additions & 38 deletions flywheel/Stats.cs
Expand Up @@ -35,35 +35,53 @@ public static void Init()
}
_measuringRate = true;
var now = DateTime.UtcNow;
var timeDiffSecs = (now - _lastRateRead).TotalSeconds;
var sent = Interlocked.Read(ref Sent);
var sendDiff = sent - _lastSendCount;
var sendsPerSec = sendDiff / timeDiffSecs;
SentPerSecond = sendsPerSec;
var recv = Interlocked.Read(ref Received);
var recvDiff = recv - _lastReceivedCount;
var recvPerSec = recvDiff / timeDiffSecs;
ReceivedPerSecond = recvPerSec;
_lastSendCount = sent;
_lastReceivedCount = recv;
_lastRateRead = now;
// Update average
AvgSentPerSecond = _avgLastSentCount / (now - _avgCalcStart).TotalSeconds;
AvgReceivedPerSecond = _avgLastReceivedCount / (now - _avgCalcStart).TotalSeconds;
// Update tracked connected clients
ConnectedClientsTracking = _connectedClients.Count;
MessageStoreSize = _messageStore.Value.CurrentMessageCount();
_measuringRate = false;
try
{
var now = DateTime.UtcNow;
var timeDiffSecs = (now - _lastRateRead).TotalSeconds;
if (timeDiffSecs <= 0)
return;
var sent = Interlocked.Read(ref Sent);
var sendDiff = sent - _lastSendCount;
var sendsPerSec = sendDiff / timeDiffSecs;
SendsPerSecond = sendsPerSec;
var recv = Interlocked.Read(ref Received);
var recvDiff = recv - _lastReceivedCount;
var recvPerSec = recvDiff / timeDiffSecs;
ReceivesPerSecond = recvPerSec;
_lastSendCount = sent;
_lastReceivedCount = recv;
_lastRateRead = now;
// Update the peak
if (sendsPerSec < long.MaxValue && sendsPerSec > PeakSendsPerSecond)
{
Interlocked.Exchange(ref PeakSendsPerSecond, sendsPerSec);
}
if (recvPerSec < long.MaxValue && recvPerSec > PeakReceivesPerSecond)
{
Interlocked.Exchange(ref PeakReceivesPerSecond, recvPerSec);
}
// Update average
AvgSendsPerSecond = _avgLastSentCount / (now - _avgCalcStart).TotalSeconds;
AvgReceivesPerSecond = _avgLastReceivedCount / (now - _avgCalcStart).TotalSeconds;
// Update tracked connected clients
ConnectedClientsTracking = _connectedClients.Count;
MessageStoreSize = _messageStore.Value.CurrentMessageCount();
}
finally
{
_measuringRate = false;
}
}, null, 1000, 1000);

//PersistentConnection.ClientConnected += (s) =>
//{
// //Task.Factory.StartNew(() =>
Expand Down Expand Up @@ -121,7 +139,7 @@ public static void Init()

var onSending = new Action<string>(payload =>
{
var payloadSize = Encoding.UTF8.GetBytes(payload).Length;
var payloadSize = Encoding.UTF8.GetByteCount(payload);
Interlocked.Add(ref BytesSent, payloadSize);
Interlocked.Add(ref BytesTotal, payloadSize);
Interlocked.Increment(ref Sent);
Expand All @@ -130,7 +148,7 @@ public static void Init()

var onReceving = new Action<string>(payload =>
{
var payloadSize = Encoding.UTF8.GetBytes(payload).Length;
var payloadSize = Encoding.UTF8.GetByteCount(payload);
Interlocked.Add(ref BytesReceived, payloadSize);
Interlocked.Add(ref BytesTotal, payloadSize);
Interlocked.Increment(ref Received);
Expand Down Expand Up @@ -163,11 +181,13 @@ public static void ResetAverage()

public static long Sent;
public static long Received;
public static double SentPerSecond;
public static double ReceivedPerSecond;
public static double SendsPerSecond;
public static double ReceivesPerSecond;
public static double TotalPerSecond;
public static double AvgSentPerSecond;
public static double AvgReceivedPerSecond;
public static double AvgSendsPerSecond;
public static double AvgReceivesPerSecond;
public static double PeakSendsPerSecond;
public static double PeakReceivesPerSecond;
public static long ConnectionsReturnedImmediately;
public static long ConnectionsSubscribedToSignal;
public static long BytesSent;
Expand All @@ -185,11 +205,13 @@ public static object GetStats()
{
Sent,
Received,
SentPerSecond,
ReceivedPerSecond,
SendsPerSecond,
ReceivesPerSecond,
TotalPerSecond,
AvgSentPerSecond,
AvgReceivedPerSecond,
AvgSentPerSecond = AvgSendsPerSecond,
AvgReceivedPerSecond = AvgReceivesPerSecond,
PeakSentPerSecond = PeakSendsPerSecond,
PeakReceivedPerSecond = PeakReceivesPerSecond,
BytesSent,
BytesReceived,
BytesTotal,
Expand Down
6 changes: 6 additions & 0 deletions flywheel/StatsHub.cs
Expand Up @@ -84,6 +84,12 @@ public void ResetAverage()
Stats.ResetAverage();
}

public void ForceGC()
{
GC.Collect();
GC.WaitForPendingFinalizers();
}

private static void SetBroadcastPayload()
{
_broadcastPayload = String.Join("", Enumerable.Range(0, _broadcastSize - 1).Select(i => "a"));
Expand Down
2 changes: 1 addition & 1 deletion flywheel/Web.config
Expand Up @@ -5,7 +5,7 @@
-->
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.0" />
<compilation debug="false" targetFramework="4.0" />
<authentication mode="None"></authentication>
<membership>
<providers>
Expand Down

0 comments on commit faaa07a

Please sign in to comment.