コード例 #1
0
        public void PerOperationSampler_Update_ShouldAddSamplerToOperationThatDoesntHaveOneYet()
        {
            var samplingRate2 = 0.35;

            var strat = new PerOperationSamplingStrategies
            {
                DefaultSamplingProbability       = _samplingRate,
                DefaultLowerBoundTracesPerSecond = 0.7,
                PerOperationStrategies           = new List <OperationSamplingStrategy>
                {
                    new OperationSamplingStrategy
                    {
                        Operation             = "op1",
                        ProbabilisticSampling = new ProbabilisticSamplingStrategy
                        {
                            SamplingRate = _samplingRate
                        }
                    },
                    new OperationSamplingStrategy
                    {
                        Operation             = "op2",
                        ProbabilisticSampling = new ProbabilisticSamplingStrategy
                        {
                            SamplingRate = samplingRate2
                        }
                    }
                }
            };

            _mockSamplerFactory.NewGuaranteedThroughputProbabilisticSampler(
                Arg.Is <double>(x => x == _samplingRate),
                Arg.Is <double>(x => x == strat.DefaultLowerBoundTracesPerSecond)
                ).Returns(_mockGtpSampler);
            _mockSamplerFactory.NewGuaranteedThroughputProbabilisticSampler(
                Arg.Is <double>(x => x == samplingRate2),
                Arg.Is <double>(x => x == strat.DefaultLowerBoundTracesPerSecond)
                ).Returns(_mockGtpSampler);

            var updated = _testingSampler.Update(strat);

            Assert.True(updated);
            _mockSamplerFactory.Received(1).NewGuaranteedThroughputProbabilisticSampler(Arg.Is(_samplingRate), Arg.Is(strat.DefaultLowerBoundTracesPerSecond));
            _mockSamplerFactory.Received(1).NewGuaranteedThroughputProbabilisticSampler(Arg.Is(samplingRate2), Arg.Is(strat.DefaultLowerBoundTracesPerSecond));
            _mockGtpSampler.Received(0).Update(Arg.Any <double>(), Arg.Any <double>());
        }
コード例 #2
0
        public bool Update(PerOperationSamplingStrategies strategies)
        {
            var isUpdated = false;

            _lowerBound = strategies.DefaultLowerBoundTracesPerSecond;

            var defaultSampler = _factory.NewProbabilisticSampler(strategies.DefaultSamplingProbability);

            if (!defaultSampler.Equals(_defaultSampler))
            {
                _defaultSampler = defaultSampler;
                isUpdated       = true;
            }

            foreach (var strategy in strategies.PerOperationStrategies)
            {
                var operation    = strategy.Operation;
                var samplingRate = strategy.ProbabilisticSampling.SamplingRate;
                if (_samplers.TryGetValue(operation, out var sampler))
                {
                    isUpdated = sampler.Update(samplingRate, _lowerBound) || isUpdated;
                }
                else
                {
                    if (_samplers.Count < _maxOperations)
                    {
                        sampler = (IGuaranteedThroughputProbabilisticSampler)_factory.NewGuaranteedThroughputProbabilisticSampler(samplingRate, _lowerBound);
                        _samplers.Add(operation, sampler);
                        isUpdated = true;
                    }
                    else
                    {
                        _logger.LogInformation($"Exceeded the maximum number of operations ({_maxOperations}) for per operations sampling");
                    }
                }
            }

            return(isUpdated);
        }
コード例 #3
0
        public PerOperationSamplerTests()
        {
            _samplingRate  = 1.0;
            _lowerBound    = 0.5;
            _maxOperations = 3;

            _mockGtpSampler     = Substitute.For <IGuaranteedThroughputProbabilisticSampler>();
            _mockLoggerFactory  = Substitute.For <ILoggerFactory>();
            _mockLogger         = Substitute.For <ILogger <PerOperationSampler> >();
            _mockDefaultSampler = Substitute.For <IProbabilisticSampler>();
            _mockSamplerFactory = Substitute.For <ISamplerFactory>();

            _mockSamplerFactory.NewGuaranteedThroughputProbabilisticSampler(
                Arg.Is <double>(x => x == _samplingRate),
                Arg.Is <double>(x => x == _lowerBound)
                ).Returns(_mockGtpSampler);
            _mockSamplerFactory.NewProbabilisticSampler(
                Arg.Is <double>(x => x == _samplingRate)
                ).Returns(_mockDefaultSampler);
            _mockLoggerFactory.CreateLogger <PerOperationSampler>().Returns(_mockLogger);

            _testingSampler = new PerOperationSampler(_maxOperations, _samplingRate, _lowerBound, _mockLoggerFactory, _mockSamplerFactory);
        }