コード例 #1
0
        /// <summary>
        /// The platform calls this method when an instance of your service is placed and ready to execute.
        /// </summary>
        /// <param name="cancellationToken">
        /// The system uses a cancellation token to signal your service when it's time to stop running.
        /// </param>
        /// <returns></returns>
        protected override async Task RunAsync(CancellationToken cancellationToken)
        {
            ConcurrentDictionary <int, CustomObject> dictionary = new ConcurrentDictionary <int, CustomObject>();

            int i = 1;

            while (!cancellationToken.IsCancellationRequested)
            {
                dictionary.AddOrUpdate(i, new CustomObject()
                {
                    Data = i
                }, (k, v) => new CustomObject()
                {
                    Data = v.Data + 1
                });

                ServiceEventSource.Current.ServiceMessage(
                    this,
                    "Total Custom Objects: {0}. Data Average: {1}",
                    dictionary.Count,
                    dictionary.Average(item => item.Value.Data));

                i = i % 10 == 0 ? 1 : i + 1;

                await Task.Delay(TimeSpan.FromSeconds(1), cancellationToken);
            }
        }
コード例 #2
0
        public int GetAverageQueueSize()
        {
            if (!_peerToPackets.Any())
            {
                return(0);
            }

            return((int)_peerToPackets.Average(p => p.Value.Count));
        }
コード例 #3
0
ファイル: Program.cs プロジェクト: weifeng1231/phash
            async Task <PerformanceReport.SummaryStatistics> GenerateStatistics(ConcurrentDictionary <int, Stopwatch> idToTimerMap)
            {
                await Task.Yield();

                var MinEntry        = idToTimerMap.Min(kv => kv.Value.IsRunning ? TimeSpan.MaxValue : kv.Value.Elapsed);
                var MaxEntry        = idToTimerMap.Max(kv => kv.Value.IsRunning ? TimeSpan.MinValue : kv.Value.Elapsed);
                var AverageTimespan = TimeSpan.FromMilliseconds((MaxEntry.TotalMilliseconds - MinEntry.TotalMilliseconds) / 2);
                var Average         = TimeSpan.FromMilliseconds(idToTimerMap.Average(kv => kv.Value.IsRunning ? AverageTimespan.TotalMilliseconds : kv.Value.ElapsedMilliseconds));
                var Sum             = TimeSpan.FromMilliseconds(idToTimerMap.Sum(kv => kv.Value.IsRunning ? TimeSpan.Zero.TotalMilliseconds : kv.Value.ElapsedMilliseconds));

                return(new PerformanceReport.SummaryStatistics(idToTimerMap.Count, MinEntry, MaxEntry, Average, Sum));
            }
コード例 #4
0
        public async Task <double?> GetAveragePercentageChange()
        {
            _allOrganisationDetails = await GetAllOrganisationDetailsWithChangeinPercentage(_allOrganisationDetails);

            return(_allOrganisationDetails.Average(x => x.Value));
        }
コード例 #5
0
        public void simple_thread_time_out_operation_test()
        {
            var             runCount = 20;
            var             res      = new ConcurrentDictionary <int, long>();
            ParallelOptions po       = new ParallelOptions();
            //po.MaxDegreeOfParallelism = 4;
            var totalTime = new Stopwatch();

            totalTime.Start();
            //get a benchmark
            for (int i = 0; i < runCount; i++)
            {
                var sw = new Stopwatch();
                sw.Start();
                try
                {
                    Sleep();
                    //var t = new Task(()=>Sleep(), tokenSource.Token);
                    //t.Wait(tokenSource.Token);
                    // t.RunSynchronously();
                    sw.Stop();
                    res.TryAdd(i, sw.ElapsedMilliseconds);
                }
                catch (TimeoutException)
                {
                    sw.Stop();
                    res.TryAdd(-i, sw.ElapsedMilliseconds);
                }
            }

            int avTime = (int)res.Average(r => r.Value);

            res.Clear();
            var firstRunTime = totalTime.ElapsedMilliseconds;

            totalTime.Restart();
            Parallel.For(0, runCount, (i) =>
            {
                var sw = new Stopwatch();
                sw.Start();
                try
                {
                    var time = CallWithTimeout(Sleep, SleepTime * 3);

                    //var t = new Task(()=>Sleep(), tokenSource.Token);
                    //t.Wait(tokenSource.Token);
                    // t.RunSynchronously();
                    sw.Stop();
                    res.TryAdd(i, sw.ElapsedMilliseconds);
                }
                catch (TimeoutException)
                {
                    sw.Stop();
                    res.TryAdd(-i, sw.ElapsedMilliseconds);
                }
            });
            totalTime.Stop();
            var secondRunTime = totalTime.ElapsedMilliseconds;

            Assert.IsTrue(res.Where(kv => kv.Key < 0).Any(), "Some executions should have timed out");
            Console.WriteLine($"Single thread simple execution time {firstRunTime}, av unit {avTime}ms");
            Console.WriteLine($"Multi thread with time out run time {secondRunTime}, av unit {(int)res.Average(r => r.Value)}ms, number of time outs {res.Count(kv=> kv.Key<0)} out of {runCount}");
        }