Пример #1
0
        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");
        }
Пример #2
0
        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");
        }
Пример #3
0
        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");
        }
Пример #4
0
        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");
        }
Пример #5
0
        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");
        }
Пример #6
0
        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");
        }
Пример #7
0
        public void ZeroAveragingPeriod ()
        {
            var monitor = new SpeedMonitor (0);
            monitor.AddDelta (1000);
            monitor.Tick (1000);

            Assert.AreEqual (1000, monitor.Rate, "#1");
        }
Пример #8
0
        public void Tick ()
        {
            var monitor = new SpeedMonitor ();
            monitor.AddDelta (1000);
            monitor.Tick (1000);

            Assert.AreEqual (1000, monitor.Rate, "#1");
        }
Пример #9
0
        public void ZeroAveragingPeriod()
        {
            var monitor = new SpeedMonitor(0);

            monitor.AddDelta(1000);
            monitor.Tick(1000);

            Assert.AreEqual(1000, monitor.Rate, "#1");
        }
Пример #10
0
        public void Tick()
        {
            var monitor = new SpeedMonitor();

            monitor.AddDelta(1000);
            monitor.Tick(1000);

            Assert.AreEqual(1000, monitor.Rate, "#1");
        }
Пример #11
0
        public Download(string savePath, TorrentManager manager)
        {
            this.manager = manager;
            this.swarmSpeed = new SpeedMonitor (30);
            SavePath = savePath;

            GLib.Timeout.Add (1000, delegate {
                swarmSpeed.Tick ();
                return true;
            });

            manager.PieceHashed += delegate (object sender, PieceHashedEventArgs e) {
                hashProgress = (double) e.PieceIndex / manager.Torrent.Pieces.Count;
            };

            manager.TorrentStateChanged += delegate(object sender, TorrentStateChangedEventArgs e) {
                hashProgress = 0;

                if (Active)
                Gtk.Application.Invoke (delegate {
                    switch (e.NewState) {
                    case TorrentState.Downloading:
                        State = State.Downloading;
                        break;
                    case TorrentState.Hashing:
                        State = State.Hashing;
                        break;
                    case TorrentState.Paused:
                        State = State.Paused;
                        break;
                    case TorrentState.Seeding:
                        State = State.Seeding;
                        break;
                    case TorrentState.Stopped:
                        State = State.Stopped;
                        Active = false;
                        break;
                    }
                });
            };
        }
Пример #12
0
        public void VeryFastRate()
        {
            // We're reporting 500MB every 750ms period
            var speed = 500 * 1024 * 1024;

            // The actual speed averaged over a 1 second period would be this
            var estimatedActualSpeed = speed * 4 / 3;

            var monitor = new SpeedMonitor();

            for (int i = 0; i < 37; i++)
            {
                monitor.AddDelta(speed);
                monitor.Tick(750);

                Assert.Greater(monitor.Rate, 0, "#1." + i);
                Assert.Less(monitor.Rate, estimatedActualSpeed + (1 * 1024 * 1024), "#2." + i);
            }

            // Should be somewhere between 499 and 501 MB/sec
            Assert.IsTrue((monitor.Rate - estimatedActualSpeed) < (1 * 1024 * 1024), "#3");
        }
Пример #13
0
        internal DiskManager(ClientEngine engine, PieceWriter writer)
        {
            this.bufferedReads = new Queue<BufferedIO>();
            this.bufferedWrites = new Queue<BufferedIO>();
            this.cache = new Cache<BufferedIO>(true).Synchronize ();
            this.engine = engine;
            this.readLimiter = new RateLimiter();
            this.readMonitor = new SpeedMonitor();
            this.writeMonitor = new SpeedMonitor();
            this.writeLimiter = new RateLimiter();
            this.writer = writer;

            LoopTask = delegate {
                if (disposed)
                    return;

                while (this.bufferedWrites.Count > 0 && writeLimiter.TryProcess(bufferedWrites.Peek ().buffer.Length / 2048))
                {
                    BufferedIO write;
                    lock (bufferLock)
                        write = this.bufferedWrites.Dequeue();
                    try
                    {
                        PerformWrite(write);
                        cache.Enqueue (write);
                    }
                    catch (Exception ex)
                    {
                        if (write.Manager != null)
                            SetError(write.Manager, Reason.WriteFailure, ex);
                    }
                }

                while (this.bufferedReads.Count > 0 && readLimiter.TryProcess(bufferedReads.Peek().Count / 2048))
                {
                    BufferedIO read;
                    lock(bufferLock)
                        read = this.bufferedReads.Dequeue();

                    try
                    {
                        PerformRead(read);
                        cache.Enqueue (read);
                    }
                    catch (Exception ex)
                    {
                        if(read.Manager != null)
                            SetError(read.Manager, Reason.ReadFailure, ex);
                    }
                }
            };

            IOLoop.QueueTimeout(TimeSpan.FromSeconds(1), delegate {
                if (disposed)
                    return false;

                readMonitor.Tick();
                writeMonitor.Tick();
                LoopTask();
                return true;
            });
        }