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"); }
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"); }
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"); }
public void ZeroAveragingPeriod() { var monitor = new SpeedMonitor(0); monitor.AddDelta(1000); monitor.Tick(1000); Assert.AreEqual(1000, monitor.Rate, "#1"); }
public void Tick() { var monitor = new SpeedMonitor(); monitor.AddDelta(1000); monitor.Tick(1000); Assert.AreEqual(1000, monitor.Rate, "#1"); }
internal void Tick() { lock (locker) { dataDown.Tick(); dataUp.Tick(); protocolDown.Tick(); protocolUp.Tick(); } }
internal void Tick() { lock (_locker) { _dataDown.Tick(); _dataUp.Tick(); _protocolDown.Tick(); _protocolUp.Tick(); } }
public void Start(string trackerAddress) { for (var i = 0; i < _threads.Length; i++) { _threads[i] = new Thread(() => { var sb = new StringBuilder(); var torrent = 0; while (true) { sb.Remove(0, sb.Length); sb.Append(trackerAddress); sb.Append("?info_hash="); sb.Append(_hashes[torrent++]); sb.Append("&peer_id="); sb.Append("12345123451234512345"); sb.Append("&port="); sb.Append("5000"); sb.Append("&uploaded="); sb.Append("5000"); sb.Append("&downloaded="); sb.Append("5000"); sb.Append("&left="); sb.Append("5000"); sb.Append("&compact="); sb.Append("1"); var request = WebRequest.Create(sb.ToString()); request.BeginGetResponse(r => { try { request.EndGetResponse(r).Close(); _requests.AddDelta(1); } catch { } finally { _requests.Tick(); } }, null); Thread.Sleep(_threadSleepTime); } }); _threads[i].Start(); } }
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; } }); } }; }
public void Start(string trackerAddress) { for (int i = 0; i < threads.Length; i++) { threads[i] = new Thread((ThreadStart) delegate { StringBuilder sb = new StringBuilder(); int torrent = 0; while (true) { sb.Remove(0, sb.Length); int ipaddress = random.Next(0, hashes.Count); sb.Append(trackerAddress); sb.Append("?info_hash="); sb.Append(hashes[torrent++]); sb.Append("&peer_id="); sb.Append("12345123451234512345"); sb.Append("&port="); sb.Append("5000"); sb.Append("&uploaded="); sb.Append("5000"); sb.Append("&downloaded="); sb.Append("5000"); sb.Append("&left="); sb.Append("5000"); sb.Append("&compact="); sb.Append("1"); WebRequest req = HttpWebRequest.Create(sb.ToString()); req.BeginGetResponse(delegate(IAsyncResult r) { try { req.EndGetResponse(r).Close(); requests.AddDelta(1); } catch { } finally { requests.Tick(); } }, null); System.Threading.Thread.Sleep(threadSleepTime); } }); threads[i].Start(); } }
public void Start(string trackerAddress) { var client = new HttpClient(); for (int i = 0; i < threads.Length; i++) { threads[i] = new Thread((ThreadStart)async delegate { StringBuilder sb = new StringBuilder(); int torrent = 0; while (true) { sb.Remove(0, sb.Length); int ipaddress = random.Next(0, hashes.Count); sb.Append(trackerAddress); sb.Append("?info_hash="); sb.Append(hashes[torrent++]); sb.Append("&peer_id="); sb.Append("12345123451234512345"); sb.Append("&port="); sb.Append("5000"); sb.Append("&uploaded="); sb.Append("5000"); sb.Append("&downloaded="); sb.Append("5000"); sb.Append("&left="); sb.Append("5000"); sb.Append("&compact="); sb.Append("1"); await client.GetByteArrayAsync(sb.ToString()); requests.AddDelta(1); requests.Tick(); await Task.Delay(threadSleepTime); } }); threads[i].Start(); } }
internal DiskManager(ClientEngine engine, PieceWriter writer) { this.bufferedReads = new Queue <BufferedIO>(); this.bufferedWrites = new Queue <BufferedIO>(); this.engine = engine; this.ReadLimiter = new RateLimiter(); this.readMonitor = new SpeedMonitor(); this.writeMonitor = new SpeedMonitor(); this.WriteLimiter = new RateLimiter(); this.Writer = writer; IOLoop.QueueTimeout(TimeSpan.FromSeconds(1), () => { readMonitor.Tick(); writeMonitor.Tick(); WriteLimiter.UpdateChunks(engine.Settings.MaxWriteRate, WriteRate); ReadLimiter.UpdateChunks(engine.Settings.MaxReadRate, ReadRate); ProcessBufferedIO(); return(!Disposed); }); }
internal DiskManager(ClientEngine engine, PieceWriter writer) { _bufferedReads = new Queue <BufferedIO>(); _bufferedWrites = new Queue <BufferedIO>(); _cache = new Cache <BufferedIO>(true).Synchronize(); _engine = engine; ReadLimiter = new RateLimiter(); _readMonitor = new SpeedMonitor(); _writeMonitor = new SpeedMonitor(); WriteLimiter = new RateLimiter(); _writer = writer; _loopTask = delegate { if (_disposed) { return; } while (_bufferedWrites.Count > 0 && WriteLimiter.TryProcess(_bufferedWrites.Peek().buffer.Length / 2048)) { BufferedIO write; lock (_bufferLock) write = _bufferedWrites.Dequeue(); try { PerformWrite(write); _cache.Enqueue(write); } catch (Exception ex) { if (write.Manager != null) { SetError(write.Manager, Reason.WriteFailure, ex); } } } while (_bufferedReads.Count > 0 && ReadLimiter.TryProcess(_bufferedReads.Peek().Count / 2048)) { BufferedIO read; lock (_bufferLock) read = _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), () => { if (_disposed) { return(false); } _readMonitor.Tick(); _writeMonitor.Tick(); _loopTask(); return(true); }); }
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) { Logger.Log(null, "Write error: {0}", ex.Message); 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) { Logger.Log(null, "Read error: {0}", ex.Message); 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); }); }
internal DiskManager(ClientEngine engine, PieceWriter writer) { _bufferedReads = new Queue<BufferedIO>(); _bufferedWrites = new Queue<BufferedIO>(); _cache = new Cache<BufferedIO>(true).Synchronize(); _engine = engine; ReadLimiter = new RateLimiter(); _readMonitor = new SpeedMonitor(); _writeMonitor = new SpeedMonitor(); WriteLimiter = new RateLimiter(); _writer = writer; _loopTask = delegate { if (_disposed) return; while (_bufferedWrites.Count > 0 && WriteLimiter.TryProcess(_bufferedWrites.Peek ().buffer.Length / 2048)) { BufferedIO write; lock (_bufferLock) write = _bufferedWrites.Dequeue(); try { PerformWrite(write); _cache.Enqueue (write); } catch (Exception ex) { if (write.Manager != null) SetError(write.Manager, Reason.WriteFailure, ex); } } while (_bufferedReads.Count > 0 && ReadLimiter.TryProcess(_bufferedReads.Peek().Count / 2048)) { BufferedIO read; lock (_bufferLock) read = _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), () => { if (_disposed) return false; _readMonitor.Tick(); _writeMonitor.Tick(); _loopTask(); return true; }); }