/// <summary>
        /// Adds new time of a block to the list of times of recently downloaded blocks.
        /// </summary>
        /// <param name="peer">Peer that downloaded the block.</param>
        /// <param name="blockDownloadTimeMs">Time in milliseconds it took to download the block from the peer.</param>
        /// <param name="blockSize">Size of the downloaded block in bytes.</param>
        public void AddSample(IBlockPullerBehavior peer, long blockDownloadTimeMs, int blockSize)
        {
            this.logger.LogTrace($"({nameof(peer)}:{peer.GetHashCode():x},{nameof(blockDownloadTimeMs)}:{blockDownloadTimeMs},{nameof(blockSize)}:{blockSize})");

            double timePerKb = 1024.0 * (double)blockDownloadTimeMs / (double)blockSize;

            if (timePerKb < 0.00001)
            {
                timePerKb = 0.00001;
            }

            lock (this.lockObject)
            {
                // If we reached the maximum number of samples, we need to remove oldest sample.
                if (this.samplesCount == this.samples.Length)
                {
                    PeerSample oldSample = this.samples[this.samplesIndex];
                    this.samplesSum -= oldSample.timePerKb;
                    this.peerReferenceCounter[oldSample.peer]--;

                    if (this.peerReferenceCounter[oldSample.peer] == 0)
                    {
                        this.peerReferenceCounter.Remove(oldSample.peer);
                    }
                }
                else
                {
                    this.samplesCount++;
                }

                // Add new sample to the mix.
                this.samples[this.samplesIndex].timePerKb = timePerKb;
                this.samples[this.samplesIndex].peer      = peer;
                this.samplesIndex = (this.samplesIndex + 1) % this.samples.Length;

                if (this.peerReferenceCounter.ContainsKey(peer))
                {
                    this.peerReferenceCounter[peer]++;
                }
                else
                {
                    this.peerReferenceCounter.Add(peer, 1);
                }

                // Update the sum and the average with the latest data.
                this.samplesSum           += timePerKb;
                this.AverageBlockTimePerKb = this.samplesSum / this.samplesCount;
            }

            this.logger.LogTrace($"(-):{nameof(this.AverageBlockTimePerKb)}={this.AverageBlockTimePerKb}");
        }
Beispiel #2
0
        /// <summary>
        /// Adds new time of a block to the list of times of recently downloaded blocks.
        /// </summary>
        /// <param name="peer">Peer that downloaded the block.</param>
        /// <param name="blockDownloadTimeMs">Time in milliseconds it took to download the block from the peer.</param>
        /// <param name="blockSize">Size of the downloaded block in bytes.</param>
        public void AddSample(IBlockPullerBehavior peer, long blockDownloadTimeMs, int blockSize)
        {
            this.logger.LogTrace("({0}:{1:x},{2}:{3},{4}:{5})", nameof(peer), peer.GetHashCode(), nameof(blockDownloadTimeMs), blockDownloadTimeMs, nameof(blockSize), blockSize);

            double timePerKb = 1024.0 * (double)blockDownloadTimeMs / (double)blockSize;

            if (timePerKb < 0.00001)
            {
                timePerKb = 0.00001;
            }

            lock (this.lockObject)
            {
                // Add new sample to the mix.
                PeerSample newSample = new PeerSample();
                newSample.TimePerKb = timePerKb;
                newSample.Peer      = peer;

                if (this.peerReferenceCounter.ContainsKey(peer))
                {
                    this.peerReferenceCounter[peer]++;
                }
                else
                {
                    this.peerReferenceCounter.Add(peer, 1);
                }

                PeerSample oldSample;
                if (this.samples.Add(newSample, out oldSample))
                {
                    // If we reached the maximum number of samples, we need to remove oldest sample.
                    this.samplesSum -= oldSample.TimePerKb;
                    this.peerReferenceCounter[oldSample.Peer]--;

                    if (this.peerReferenceCounter[oldSample.Peer] == 0)
                    {
                        this.peerReferenceCounter.Remove(oldSample.Peer);
                    }
                }

                // Update the sum and the average with the latest data.
                this.samplesSum           += timePerKb;
                this.AverageBlockTimePerKb = this.samplesSum / this.samples.Count;
            }

            this.logger.LogTrace("(-):{0}={1}", nameof(this.AverageBlockTimePerKb), this.AverageBlockTimePerKb);
        }