public void Should_correct_calculate_weights_by_latency(double replicaStdDev, double replicaAvg, double expectedWeight)
        {
            var timeStamp        = DateTime.UtcNow;
            var clusterStatistic = new AggregatedStatistic(1000, 0.2, 70, 300, timeStamp);
            var replicaStatistic = new AggregatedStatistic(1000, 0.2, replicaStdDev, replicaAvg, timeStamp);

            var calculatedWeight = relativeWeightCalculator
                                   .Calculate(clusterStatistic, replicaStatistic, new Weight(settings.InitialWeight, timeStamp - settings.WeightUpdatePeriod), settings);

            calculatedWeight.Value.Should().BeApproximately(expectedWeight, 0.001);
            calculatedWeight.Timestamp.Should().Be(timeStamp);
        }
        public void Should_correct_calculate_weights_by_statuses(double replicaTotalCount, double replicaErrorFraction, double expectedWeight)
        {
            settings.WeightUpdatePeriod           = 10.Seconds();
            settings.WeightByStatusesRpsThreshold = 10;
            var timeStamp        = DateTime.UtcNow;
            var clusterStatistic = new AggregatedStatistic(100, 0.2, 70, 300, timeStamp);
            var replicaStatistic = new AggregatedStatistic(replicaTotalCount, replicaErrorFraction, 10000, 1000, timeStamp);

            var calculatedWeight = relativeWeightCalculator
                                   .Calculate(clusterStatistic, replicaStatistic, new Weight(settings.InitialWeight, timeStamp - settings.WeightUpdatePeriod), settings);

            calculatedWeight.Value.Should().BeApproximately(expectedWeight, 0.001);
            calculatedWeight.Timestamp.Should().Be(timeStamp);
        }
예제 #3
0
        public void Should_smooth_statistic(int[] responseTimes, double expectedMean, double expectedStdDev)
        {
            var timestamp         = DateTime.UtcNow;
            var previousStatistic = new AggregatedStatistic(10, 0.1, 3, 8.125, timestamp - 5.Seconds());

            foreach (var responseTime in responseTimes)
            {
                statisticBucket.Report(Accepted(responseTime));
            }

            var smoothedStat = statisticBucket
                               .Aggregate(timestamp)
                               .Smooth(previousStatistic, 15.Seconds());

            smoothedStat.Timestamp.Should().Be(timestamp);
            smoothedStat.Mean.Should().BeApproximately(expectedMean, 0.001);
            smoothedStat.StdDev.Should().BeApproximately(expectedStdDev, 0.001);
            smoothedStat.TotalCount.Should().BeApproximately(9.433, 0.001);
            smoothedStat.ErrorFraction.Should().BeApproximately(0.071, 0.001);
        }
        public void Update_should_add_new_statistic()
        {
            var clusterStatistic  = new AggregatedStatistic(10, 0.2, 3, 15, DateTime.UtcNow);
            var replicasStatistic = new Dictionary <Uri, AggregatedStatistic>()
            {
                [new Uri("http://r1")] = new AggregatedStatistic(10, 0.2, 7, 2.5, DateTime.UtcNow),
                [new Uri("http://r2")] = new AggregatedStatistic(10, 0.2, 8, 9, DateTime.UtcNow),
                [new Uri("http://r3")] = new AggregatedStatistic(10, 0.2, 0.5, 0.35, DateTime.UtcNow),
            };

            statisticsHistory.Update(new AggregatedClusterStatistic(clusterStatistic, replicasStatistic), statisticTTL);

            var clusterStats = statisticsHistory.Get();

            clusterStats.Cluster.Should().Be(clusterStatistic);
            clusterStats.Replicas.Count.Should().Be(3);
            foreach (var(replica, statistic) in clusterStats.Replicas)
            {
                statistic.Should().Be(replicasStatistic[replica]);
            }
        }
        public void Update_should_delete_obsolete_statistics()
        {
            var r1 = new Uri("http://r1");
            var r2 = new Uri("http://r2");
            var r3 = new Uri("http://r3");
            var clusterStatistic  = new AggregatedStatistic(10, 0.2, 3, 15, DateTime.UtcNow);
            var replicasStatistic = new Dictionary <Uri, AggregatedStatistic>()
            {
                [r1] = new AggregatedStatistic(10, 0.2, 7, 2.5, DateTime.UtcNow - 20.Seconds()),
                [r2] = new AggregatedStatistic(10, 0.2, 8, 9, DateTime.UtcNow - 40.Seconds()),
                [r3] = new AggregatedStatistic(10, 0.2, 0.5, 0.35, DateTime.UtcNow - 2.Minutes()),
            };

            statisticsHistory.Update(new AggregatedClusterStatistic(clusterStatistic, replicasStatistic), 1.Hours());

            var clusterStats = statisticsHistory.Get();

            clusterStats.Cluster.Should().Be(clusterStatistic);
            clusterStats.Replicas.Count.Should().Be(3);
            foreach (var(replica, statistic) in clusterStats.Replicas)
            {
                statistic.Should().Be(replicasStatistic[replica]);
            }

            var newReplicasStats = new Dictionary <Uri, AggregatedStatistic>()
            {
                [r2] = new AggregatedStatistic(10, 0.2, 5, 10, DateTime.UtcNow)
            };

            statisticsHistory.Update(new AggregatedClusterStatistic(clusterStatistic, newReplicasStats), 1.Minutes());

            clusterStats = statisticsHistory.Get();
            clusterStats.Cluster.Should().Be(clusterStatistic);
            clusterStats.Replicas.Count.Should().Be(2);
            clusterStats.Replicas[r1].Should().Be(replicasStatistic[r1]);
            clusterStats.Replicas[r2].Should().Be(newReplicasStats[r2]);
        }
        public void Update_should_correct_update_current_statistic()
        {
            var r1 = new Uri("http://r1");
            var r2 = new Uri("http://r2");
            var r3 = new Uri("http://r3");
            var clusterStatistic  = new AggregatedStatistic(10, 0.2, 3, 15, DateTime.UtcNow);
            var replicasStatistic = new Dictionary <Uri, AggregatedStatistic>()
            {
                [r1] = new AggregatedStatistic(10, 0.2, 7, 2.5, DateTime.UtcNow),
                [r2] = new AggregatedStatistic(10, 0.2, 8, 9, DateTime.UtcNow),
                [r3] = new AggregatedStatistic(10, 0.2, 0.5, 0.35, DateTime.UtcNow),
            };

            statisticsHistory.Update(new AggregatedClusterStatistic(clusterStatistic, replicasStatistic), statisticTTL);

            var clusterStats = statisticsHistory.Get();

            clusterStats.Cluster.Should().Be(clusterStatistic);
            foreach (var(replica, statistic) in clusterStats.Replicas)
            {
                statistic.Should().Be(replicasStatistic[replica]);
            }

            var newCluster  = new AggregatedStatistic(10, 0.2, 1, 1, DateTime.UtcNow);
            var newReplicas = new Dictionary <Uri, AggregatedStatistic>()
            {
                [new Uri("http://r3")] = new AggregatedStatistic(10, 0.2, 9.5, 1.35, DateTime.UtcNow),
            };

            statisticsHistory.Update(new AggregatedClusterStatistic(newCluster, newReplicas), statisticTTL);

            clusterStats = statisticsHistory.Get();
            clusterStats.Cluster.Should().Be(newCluster);
            clusterStats.Replicas[r1].Should().Be(replicasStatistic[r1]);
            clusterStats.Replicas[r2].Should().Be(replicasStatistic[r2]);
            clusterStats.Replicas[r3].Should().Be(newReplicas[r3]);
        }