public void TimeExpireTest()
        {
            for (var max = 100; max < 200; max++)
            {
                var   profiler   = new Profiler(new ProfilerOptions(ProfilerType.SampleAverageTimeMs, max, 10, null), nameof(TimeExpireTest));
                Int64 totalCount = 0;
                var   avgCount   = 0;
                for (var i = 0; i < 100; i++)
                {
                    var r = ConcurrentRandom.NextInt32();
                    var a = ConcurrentRandom.Next(1, 100);
                    profiler.AddTimeMeasurement(r, a);
                }
                Thread.Sleep(10);
                for (var i = 0; i < max - 100; i++)
                {
                    var r = ConcurrentRandom.NextInt32();
                    var a = ConcurrentRandom.Next(1, 100);
                    totalCount += r;
                    avgCount   += a;
                    profiler.AddTimeMeasurement(r, a);
                }

                if (avgCount != 0 && totalCount != 0)
                {
                    var avg = ((double)totalCount / 10_000D) / avgCount;
                    Assert.InRange(profiler.GetValue(), avg - 0.1D, avg + 0.1D);
                }

                Assert.Equal(String.Format("{0:0,0.00}", profiler.GetValue()), profiler.GetText());

                Assert.Throws <ArgumentOutOfRangeException>(() => profiler.AddTimeMeasurement(1, 0));
                Assert.Throws <ArgumentOutOfRangeException>(() => profiler.AddTimeMeasurement(1, -1));
            }
        }
Пример #2
0
        protected override bool OnShouldRetry(TimeSpan remainingTime, int currentRetryCount, out TimeSpan retryInterval)
        {
            if (currentRetryCount > this.MaxRetryCount)
            {
                retryInterval = TimeSpan.Zero;
                return(false);
            }
            int      totalMilliseconds = (int)(this.DeltaBackoff.TotalMilliseconds * 0.8);
            TimeSpan deltaBackoff      = this.DeltaBackoff;
            int      num            = ConcurrentRandom.Next(totalMilliseconds, (int)(deltaBackoff.TotalMilliseconds * 1.2));
            double   num1           = (Math.Pow(2, (double)currentRetryCount) - 1) * (double)num;
            TimeSpan minimalBackoff = this.MinimalBackoff;
            TimeSpan maximumBackoff = this.MaximumBackoff;
            double   num2           = Math.Min(minimalBackoff.TotalMilliseconds + num1, maximumBackoff.TotalMilliseconds);

            retryInterval = TimeSpan.FromMilliseconds(num2);
            if (base.IsServerBusy)
            {
                retryInterval = retryInterval + RetryPolicy.ServerBusyBaseSleepTime;
            }
            if (retryInterval < (remainingTime - this.TerminationTimeBuffer))
            {
                return(true);
            }
            retryInterval = TimeSpan.Zero;
            return(false);
        }
        public void MaxHistoryTest()
        {
            for (var max = 1; max < 100; max++)
            {
                var   profiler   = new Profiler(new ProfilerOptions(ProfilerType.SampleAverageTimeMs, max, 1000, null), nameof(MaxHistoryTest));
                Int64 totalCount = 0;
                var   avgCount   = 0;
                for (var i = 0; i < 1000; i++)
                {
                    var r = ConcurrentRandom.NextInt32();
                    var a = ConcurrentRandom.Next(1, 100);
                    profiler.AddTimeMeasurement(r, a);
                }
                for (var i = 0; i < max; i++)
                {
                    var r = ConcurrentRandom.NextInt32();
                    var a = ConcurrentRandom.Next(1, 100);
                    totalCount += r;
                    avgCount   += a;
                    profiler.AddTimeMeasurement(r, a);
                }

                var avg = ((double)totalCount / 10_000D) / avgCount;
                Assert.InRange(profiler.GetValue(), avg - 0.1D, avg + 0.1D);
                Assert.Equal(String.Format("{0:0,0.00}", profiler.GetValue()), profiler.GetText());
            }
        }
Пример #4
0
        public static Beatmap GetHighestDiff(IEnumerable <Beatmap> enumerable)
        {
            var dictionary = enumerable.GroupBy(k => k.GameMode).ToDictionary(k => k.Key, k => k.ToList());

            if (dictionary.ContainsKey(GameMode.Circle))
            {
                return(dictionary[GameMode.Circle].Aggregate((i1, i2) => i1.DiffSrNoneStandard > i2.DiffSrNoneStandard ? i1 : i2));
            }
            if (dictionary.ContainsKey(GameMode.Mania))
            {
                return(dictionary[GameMode.Mania].Aggregate((i1, i2) => i1.DiffSrNoneMania > i2.DiffSrNoneMania ? i1 : i2));
            }

            if (dictionary.ContainsKey(GameMode.Catch))
            {
                return(dictionary[GameMode.Catch].Aggregate((i1, i2) => i1.DiffSrNoneCtB > i2.DiffSrNoneCtB ? i1 : i2));
            }

            if (dictionary.ContainsKey(GameMode.Taiko))
            {
                return(dictionary[GameMode.Taiko].Aggregate((i1, i2) => i1.DiffSrNoneTaiko > i2.DiffSrNoneTaiko ? i1 : i2));
            }

            Console.WriteLine(@"Get highest difficulty failed.");
            var randKey = dictionary.Keys.ToList()[Random.Next(dictionary.Keys.Count)];

            return(dictionary[randKey][dictionary[randKey].Count]);
            //enumerable.ToList()[Random.Next(enumerable.Count())];
        }
Пример #5
0
        public void GetTextTest()
        {
            for (var round = 0; round < 100; round++)
            {
                var count     = ConcurrentRandom.Next(1, 100);
                var profilers = new Profiler[count];
                for (var i = 0; i < count; i++)
                {
                    var name = nameof(GetTextTest) + string.Format("{0:0000}", (int)(i / 2));
                    profilers[i] = ProfilerGroup.Default.CreateInstance(new ProfilerOptions(ProfilerType.Counter), name);
                    profilers[i].Set(i + 10);
                }
                ;

                var list = ProfilerGroup.Default.GetMeasurements();

                for (var i = 0; i < profilers.Length; i++)
                {
                    var name = nameof(GetTextTest) + string.Format("{0:0000}", (int)(i / 2));
                    Assert.Equal(name, list[i].Key);
                    Assert.Equal((i + 10).ToString(), list[i].Value);
                }

                for (var i = 0; i < profilers.Length; i++)
                {
                    profilers[i].Dispose();
                }
            }
        }
 private void FindNewMessageSender(string path)
 {
     if (!string.Equals(this.Backlog.Path, path, StringComparison.OrdinalIgnoreCase) || this.Options.BacklogQueueCount <= 1)
     {
         return;
     }
     lock (this.backlogLock)
     {
         if (string.Equals(this.Backlog.Path, path, StringComparison.OrdinalIgnoreCase))
         {
             int    num = ConcurrentRandom.Next(0, this.Options.BacklogQueueCount);
             string str = this.Options.FetchBacklogQueueName(num);
             if (string.Equals(path, str, StringComparison.OrdinalIgnoreCase))
             {
                 num++;
                 if (num >= this.Options.BacklogQueueCount)
                 {
                     num = 0;
                 }
                 str = this.Options.FetchBacklogQueueName(num);
             }
             this.Backlog = this.Options.SecondaryMessagingFactory.CreateMessageSender(str);
             this.Backlog.ShouldLinkRetryPolicy = true;
             this.Backlog.RetryPolicy           = this.Options.SecondaryMessagingFactory.RetryPolicy;
         }
     }
 }
Пример #7
0
        public static IEnumerable <TValue> RandomValues <TKey, TValue>(this IDictionary <TKey, TValue> dict,
                                                                       ConcurrentRandom rand = null)
        {
            rand = rand ?? new ConcurrentRandom();
            List <TValue> values = Enumerable.ToList(dict.Values);
            int           size   = dict.Count;

            while (true)
            {
                yield return(values[rand.Next(size)]);
            }
        }
 public bool MoveNextUri()
 {
     if (this.firstUriIndex != -1)
     {
         if (!this.roundRobin && this.IsLastUri())
         {
             return(false);
         }
         this.currentUriIndex = this.GetNextUriValue();
     }
     else
     {
         int num  = ConcurrentRandom.Next(0, this.uriAddresses.Count);
         int num1 = num;
         this.currentUriIndex = num;
         this.firstUriIndex   = num1;
     }
     return(true);
 }
Пример #9
0
        public static BeatmapEntry GetHighestDiff(this IEnumerable <BeatmapEntry> enumerable)
        {
            var dictionary = enumerable.GroupBy(k => k.GameMode).ToDictionary(k => k.Key, k => k.ToList());

            if (dictionary.ContainsKey(GameMode.Standard))
            {
                if (dictionary[GameMode.Standard].All(k => k.DiffStarRatingStandard.ContainsKey(Mods.None)))
                {
                    return(dictionary[GameMode.Standard].OrderBy(k => k.DiffStarRatingStandard[Mods.None]).Last());
                }
            }
            if (dictionary.ContainsKey(GameMode.Mania))
            {
                if (dictionary[GameMode.Mania].All(k => k.DiffStarRatingMania.ContainsKey(Mods.None)))
                {
                    return(dictionary[GameMode.Mania].OrderBy(k => k.DiffStarRatingMania[Mods.None]).Last());
                }
            }

            if (dictionary.ContainsKey(GameMode.CatchTheBeat))
            {
                if (dictionary[GameMode.CatchTheBeat].All(k => k.DiffStarRatingCtB.ContainsKey(Mods.None)))
                {
                    return(dictionary[GameMode.CatchTheBeat].OrderBy(k => k.DiffStarRatingCtB[Mods.None]).Last());
                }
            }

            if (dictionary.ContainsKey(GameMode.Taiko))
            {
                if (dictionary[GameMode.Taiko].All(k => k.DiffStarRatingTaiko.ContainsKey(Mods.None)))
                {
                    return(dictionary[GameMode.Taiko].OrderBy(k => k.DiffStarRatingTaiko[Mods.None]).Last());
                }
            }

            Console.WriteLine(@"Get highest difficulty failed.");
            var randKey = dictionary.Keys.ToList()[Random.Next(dictionary.Keys.Count)];

            return(dictionary[randKey][dictionary[randKey].Count]);
            //enumerable.ToList()[Random.Next(enumerable.Count())];
        }
Пример #10
0
        public void FormatterTest()
        {
            for (var round = 0; round < 100; round++)
            {
                var profiler = new Profiler(new ProfilerOptions(ProfilerType.SampleAverageTimeMs, 100, 100, "{0:0}"),
                                            nameof(FormatterTest));
                Int64 totalCount = 0;
                var   avgCount   = 0;
                for (var i = 0; i < 100; i++)
                {
                    var r = ConcurrentRandom.NextInt32();
                    var a = ConcurrentRandom.Next(1, 100);
                    totalCount += r;
                    avgCount   += a;
                    profiler.AddTimeMeasurement(r, a);
                }

                var avg = Math.Round(((double)totalCount / 10_000D) / avgCount);
                Assert.Equal(avg.ToString(), profiler.GetText());
            }
        }
Пример #11
0
        public static async Task <World[]> LoadMultipleQueriesRows(int count)
        {
            var worlds = new World[count];

            using var pooledConnection = new PooledConnection(DataProvider.ConnectionString);
            await pooledConnection.OpenAsync();

            var(pooledCommand, dbDataParameter) = CreateReadCommand(pooledConnection);

            using (pooledCommand)
            {
                for (int i = 0; i < count; i++)
                {
                    worlds[i] = await ReadSingleRow(pooledCommand);

                    dbDataParameter.Value = _random.Next(1, 10001);
                }
            }

            return(worlds);
        }
        protected override IEnumerator <IteratorAsyncResult <GetRuntimeEntityDescriptionAsyncResult> .AsyncStep> GetAsyncSteps()
        {
            MessagingClientEtwProvider.TraceClient(() => {
            });
            if (!this.executeOnce)
            {
                while (this.ShouldGetEntityInfo(MessagingExceptionHelper.Unwrap(base.LastAsyncStepException as CommunicationException)))
                {
                    if (!RuntimeEntityDescriptionCache.TryGet(this.entityAddress, out this.runtimeEntityDescription))
                    {
                        this.request = this.CreateOrGetRequestMessage();
                        GetRuntimeEntityDescriptionAsyncResult getRuntimeEntityDescriptionAsyncResult     = this;
                        IteratorAsyncResult <GetRuntimeEntityDescriptionAsyncResult> .BeginCall beginCall = (GetRuntimeEntityDescriptionAsyncResult thisPtr, TimeSpan t, AsyncCallback c, object s) => thisPtr.factory.Channel.BeginRequest(thisPtr.request, t, c, s);
                        yield return(getRuntimeEntityDescriptionAsyncResult.CallAsync(beginCall, (GetRuntimeEntityDescriptionAsyncResult thisPtr, IAsyncResult a) => thisPtr.response = thisPtr.factory.Channel.EndRequest(a), IteratorAsyncResult <TIteratorAsyncResult> .ExceptionPolicy.Continue));

                        if (base.LastAsyncStepException == null)
                        {
                            this.runtimeEntityDescription = GetRuntimeEntityDescriptionAsyncResult.BuildRuntimeEntityDescription(this.response);
                            RuntimeEntityDescriptionCache.AddOrUpdate(this.entityAddress, this.runtimeEntityDescription);
                            this.clientEntity.RuntimeEntityDescription = this.runtimeEntityDescription;
                        }
                        else
                        {
                            if (!base.LastAsyncStepException.IsWrappedExceptionTransient())
                            {
                                yield return(base.CallAsyncSleep(Constants.GetRuntimeEntityDescriptionNonTransientSleepTimeout));
                            }
                            else
                            {
                                yield return(base.CallAsyncSleep(TimeSpan.FromSeconds((double)(this.attempt % 60)) + TimeSpan.FromMilliseconds((double)ConcurrentRandom.Next(1, 1000))));
                            }
                            GetRuntimeEntityDescriptionAsyncResult getRuntimeEntityDescriptionAsyncResult1 = this;
                            getRuntimeEntityDescriptionAsyncResult1.attempt = getRuntimeEntityDescriptionAsyncResult1.attempt + 1;
                        }
                    }
                    else
                    {
                        this.clientEntity.RuntimeEntityDescription = this.runtimeEntityDescription;
                    }
                }
            }
            else
            {
                if (!RuntimeEntityDescriptionCache.TryGet(this.entityAddress, out this.runtimeEntityDescription))
                {
                    this.request = this.CreateOrGetRequestMessage();
                    GetRuntimeEntityDescriptionAsyncResult getRuntimeEntityDescriptionAsyncResult2     = this;
                    IteratorAsyncResult <GetRuntimeEntityDescriptionAsyncResult> .BeginCall beginCall1 = (GetRuntimeEntityDescriptionAsyncResult thisPtr, TimeSpan t, AsyncCallback c, object s) => thisPtr.factory.Channel.BeginRequest(thisPtr.request, t, c, s);
                    yield return(getRuntimeEntityDescriptionAsyncResult2.CallAsync(beginCall1, (GetRuntimeEntityDescriptionAsyncResult thisPtr, IAsyncResult a) => thisPtr.response = thisPtr.factory.Channel.EndRequest(a), IteratorAsyncResult <TIteratorAsyncResult> .ExceptionPolicy.Transfer));

                    this.runtimeEntityDescription = GetRuntimeEntityDescriptionAsyncResult.BuildRuntimeEntityDescription(this.response);
                    RuntimeEntityDescriptionCache.AddOrUpdate(this.entityAddress, this.runtimeEntityDescription);
                }
                this.clientEntity.RuntimeEntityDescription = this.runtimeEntityDescription;
            }
        }