コード例 #1
0
        public async Task GameThreadTimer_DisposeWorks()
        {
            const int TestInterval = 20;
            bool      run          = false;

            using (ITimer gameTimer = new GameThreadTimer())
            {
                gameTimer.Interval = TestInterval;
                gameTimer.Elapsed += (sender, e) => run = true;
                gameTimer.Start();
                await Task.Delay(TestInterval * 2).ConfigureAwait(false);
            }
            Assert.True(run);
        }
コード例 #2
0
        private async Task <(bool EnoughSamples, double AvgInterval)> CollectAverageInterval(int interval, Action <Stopwatch> work)
        {
            const int TargetSampleSize = 175;
            const int AvgPercentile    = 95;

            var times         = new List <long>();
            var testStopwatch = Stopwatch.StartNew();

            using (ITimer gameTimer = new GameThreadTimer())
            {
                gameTimer.Interval = interval;
                gameTimer.Elapsed += (sender, e) =>
                {
                    work(testStopwatch);
                    times.Add(testStopwatch.ElapsedMilliseconds);
                };
                gameTimer.Start();
                await Task.Delay(interval *TargetSampleSize).ConfigureAwait(false);
            }

            if (times.Count < 2)
            {
                return(false, 0);
            }

            var intervals = new List <long>();

            for (int i = 0; i < times.Count - 1; i++)
            {
                intervals.Add(times[i + 1] - times[i]);
            }

            int    intervalCount   = (int)(intervals.Count * AvgPercentile * 0.01);
            var    intervalCutList = intervals.OrderBy(x => x).Take(intervalCount).ToList();
            double intervalAvg     = intervalCutList.Average();

            _output.WriteLine($"Raw interval (count, min, avg, max): ({intervals.Count}, {intervals.Min()}, {intervals.Average()}, {intervals.Max()})");
            _output.WriteLine($"{AvgPercentile} percentile (count, min, avg, max): ({intervalCutList.Count}, {intervalCutList.Min()}, {intervalAvg}, {intervalCutList.Max()})");

            return(true, intervalAvg);
        }