Chart Refactor (#1019)

* Rename variable 'traffic' to 'trafficPerSecondQueue' and 'trafficInfoQueue'

* Remove class QueueLast<T> by using linq Queue.Last()

* Slight changes, such as rename, move several variables from function domain to class domain to improve performance.
Logic remains the same.

* Update the logic of copying queue from Controller to View.

* Move the TrafficInfo declaration into LogForm class

Overwise, the VS UI designer does not work.
This commit is contained in:
Allen Zhu
2017-03-31 22:21:01 +08:00
committed by Syrone Wong
parent 805edb9809
commit 84b69d3dff
4 changed files with 84 additions and 69 deletions
@@ -13,6 +13,7 @@ using Shadowsocks.Controller.Strategy;
using Shadowsocks.Model;
using Shadowsocks.Properties;
using Shadowsocks.Util;
using System.Linq;
namespace Shadowsocks.Controller
{
@@ -39,7 +40,7 @@ namespace Shadowsocks.Controller
private long _outboundCounter = 0;
public long InboundCounter => Interlocked.Read(ref _inboundCounter);
public long OutboundCounter => Interlocked.Read(ref _outboundCounter);
public QueueLast<TrafficPerSecond> traffic;
public Queue<TrafficPerSecond> trafficPerSecondQueue;
private bool stopped = false;
@@ -50,16 +51,6 @@ namespace Shadowsocks.Controller
public string Path;
}
public class QueueLast<T> : Queue<T>
{
public T Last { get; private set; }
public new void Enqueue(T item)
{
Last = item;
base.Enqueue(item);
}
}
public class TrafficPerSecond
{
public long inboundCounter;
@@ -620,10 +611,10 @@ namespace Shadowsocks.Controller
private void StartTrafficStatistics(int queueMaxSize)
{
traffic = new QueueLast<TrafficPerSecond>();
trafficPerSecondQueue = new Queue<TrafficPerSecond>();
for (int i = 0; i < queueMaxSize; i++)
{
traffic.Enqueue(new TrafficPerSecond());
trafficPerSecondQueue.Enqueue(new TrafficPerSecond());
}
_trafficThread = new Thread(new ThreadStart(() => TrafficStatistics(queueMaxSize)));
_trafficThread.IsBackground = true;
@@ -632,19 +623,20 @@ namespace Shadowsocks.Controller
private void TrafficStatistics(int queueMaxSize)
{
TrafficPerSecond previous, current;
while (true)
{
TrafficPerSecond previous = traffic.Last;
TrafficPerSecond current = new TrafficPerSecond();
previous = trafficPerSecondQueue.Last();
current = new TrafficPerSecond();
var inbound = current.inboundCounter = InboundCounter;
var outbound = current.outboundCounter = OutboundCounter;
current.inboundIncreasement = inbound - previous.inboundCounter;
current.outboundIncreasement = outbound - previous.outboundCounter;
current.inboundCounter = InboundCounter;
current.outboundCounter = OutboundCounter;
current.inboundIncreasement = current.inboundCounter - previous.inboundCounter;
current.outboundIncreasement = current.outboundCounter - previous.outboundCounter;
traffic.Enqueue(current);
if (traffic.Count > queueMaxSize)
traffic.Dequeue();
trafficPerSecondQueue.Enqueue(current);
if (trafficPerSecondQueue.Count > queueMaxSize)
trafficPerSecondQueue.Dequeue();
TrafficChanged?.Invoke(this, new EventArgs());