Skip to content

Commit

Permalink
* MonoTorrent.Tests/MonoTorrent.Tests.csproj:
Browse files Browse the repository at this point in the history
* MonoTorrent.Tests/Common/SpeedMonitorTest.cs: Add tests to ensure
  the correctness of the transfer rate estimation code.

* MonoTorrent/MonoTorrent.Common/SpeedMonitor.cs: Add missing license
  header and update the rate estimation algorithm so that it no longer
  gives insane answers when the number of ticks is less than the
  averaging period. Patch by 'nop'.

svn path=/trunk/bitsharp/; revision=156979
  • Loading branch information
alanmcgovern committed May 9, 2010
1 parent 0ed1f9f commit 8799aa8
Show file tree
Hide file tree
Showing 3 changed files with 146 additions and 37 deletions.
69 changes: 69 additions & 0 deletions src/MonoTorrent.Tests/Common/SpeedMonitorTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
using System;
using NUnit.Framework;

namespace MonoTorrent.Common
{
[TestFixture]
public class SpeedMonitorTest
{
[Test]
public void ZeroAveragingPeriod ()
{
var monitor = new SpeedMonitor (0);
monitor.AddDelta (1000);
monitor.Tick (1000);

Assert.AreEqual (1000, monitor.Rate, "#1");
}

[Test]
public void Tick ()
{
var monitor = new SpeedMonitor ();
monitor.AddDelta (1000);
monitor.Tick (1000);

Assert.AreEqual (1000, monitor.Rate, "#1");
}

[Test]
public void TickTwice ()
{
// Send 1000 bytes in 2000 milliseconds. Check
// transfer rate is then 500bytes/second
var monitor = new SpeedMonitor ();
monitor.AddDelta (1000);
monitor.Tick (1000);
monitor.Tick (1000);

Assert.AreEqual (500, monitor.Rate, "#1");
}

[Test]
public void TickFull ()
{
// Averaging period is 3, tick 3 times.
var monitor = new SpeedMonitor (3);
monitor.AddDelta (1200);
monitor.Tick (1000);
monitor.Tick (1000);
monitor.Tick (1000);

Assert.AreEqual (400, monitor.Rate, "#1");
}

[Test]
public void Tick_AveragingTwo_TickThree ()
{
// Send data only in the first tick. When we tick a third time
// we should have a transfer rate of zero as the data should vanish.
var monitor = new SpeedMonitor (2);
monitor.AddDelta (1200);
monitor.Tick (1000);
monitor.Tick (1000);
monitor.Tick (1000);

Assert.AreEqual (0, monitor.Rate, "#1");
}
}
}
1 change: 1 addition & 0 deletions src/MonoTorrent.Tests/MonoTorrent.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@
<Compile Include="Common\CustomFileSource.cs" />
<Compile Include="Client\NetworkIOTests.cs" />
<Compile Include="Common\MagnetLinkTest.cs" />
<Compile Include="Common\SpeedMonitorTest.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\MonoTorrent.Dht\MonoTorrent.Dht.csproj">
Expand Down
113 changes: 76 additions & 37 deletions src/MonoTorrent/MonoTorrent.Common/SpeedMonitor.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,31 @@
//
// SpeedMonitor.cs
//
// Authors:
// Alan McGovern alan.mcgovern@gmail.com
//
// Copyright (C) 2010 Alan McGovern
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//

using System;
using System.Collections.Generic;
using System.Text;
Expand All @@ -8,10 +36,10 @@ public class SpeedMonitor
{
private const int DefaultAveragePeriod = 12;

private int averagingPeriod;
private long total;
private int speed;
private List<int> speeds;
private int[] speeds;
private int speedsIndex;
private int lastUpdated;
private long tempRecvCount;

Expand All @@ -35,9 +63,12 @@ public SpeedMonitor()

public SpeedMonitor(int averagingPeriod)
{
this.averagingPeriod = averagingPeriod;
if (averagingPeriod < 0)
throw new ArgumentOutOfRangeException ("averagingPeriod");

this.lastUpdated = Environment.TickCount;
this.speeds = new List<int>(averagingPeriod);
this.speeds = new int [Math.Max (1, averagingPeriod)];
this.speedsIndex = -speeds.Length;
}


Expand All @@ -47,62 +78,70 @@ public void AddDelta(int speed)
this.tempRecvCount += speed;
}

public void AddDelta(long speed)
{
this.total += speed;
this.tempRecvCount += speed;
}

public void AddDelta(long speed)
{
this.total += speed;
this.tempRecvCount += speed;
}

public void Reset()
{
this.total = 0;
this.speed = 0;
this.tempRecvCount = 0;
this.lastUpdated = Environment.TickCount;
this.speeds.Clear();
this.speedsIndex = -speeds.Length;
}


private void TimePeriodPassed()
private void TimePeriodPassed(int difference)
{
int count = 0;
long total = 0;
lastUpdated = Environment.TickCount;

// Find how many milliseconds have passed since the last update and the current tick count
int difference = Environment.TickCount - this.lastUpdated;
if (difference < 0)
difference = 1000;

// Take the amount of bytes sent since the last tick and divide it by the number of seconds
// since the last tick. This gives the calculated bytes/second transfer rate.
// difference is in miliseconds, so divide by 1000 to get it in seconds
if (speeds.Count == this.averagingPeriod)
speeds.RemoveAt (0);
speeds.Add ((int)(tempRecvCount / (difference / 1000.0)));

// What we do here is add up all the bytes/second readings held in each array
// and divide that by the number of non-zero entries. The number of non-zero entries
// is given by ArraySize - count. This is to avoid the problem where a connection which
// is just starting would have a lot of zero entries making the speed estimate inaccurate.
for (int i = 0; i < this.speeds.Count; i++)
int currSpeed = (int)(tempRecvCount * 1000 / difference);
tempRecvCount = 0;

int speedsCount;
if( speedsIndex < 0 )
{
if (speeds.Count != averagingPeriod)
count++;
//speeds array hasn't been filled yet

int idx = speeds.Length + speedsIndex;

total += this.speeds[i];
speeds[idx] = currSpeed;
speedsCount = idx + 1;

speedsIndex++;
}
else
{
//speeds array is full, keep wrapping around overwriting the oldest value
speeds[speedsIndex] = currSpeed;
speedsCount = speeds.Length;

speedsIndex = (speedsIndex + 1) % speeds.Length;
}

int total = speeds[0];
for( int i = 1; i < speedsCount; i++ )
total += speeds[i];

this.speed = (int)(total / Math.Max (1, speeds.Count - count));
this.tempRecvCount = 0;
this.lastUpdated = Environment.TickCount;
speed = total / speedsCount;
}


public void Tick()
{
long difference = (long)Environment.TickCount - lastUpdated;
int difference = Environment.TickCount - lastUpdated;
if (difference >= 800 || difference < 0)
TimePeriodPassed();
TimePeriodPassed(difference);
}

internal void Tick (int difference)
{
TimePeriodPassed (difference);
}
}
}

0 comments on commit 8799aa8

Please sign in to comment.