模拟并发调用并记录所花费时间

   public void TestParallel()
        {
            int arrayLength = 500;
            int[] parallelTimes = new int[arrayLength];
            long[] ElapsedMilliseconds = new long[arrayLength];

            for (var index = 0; index <= parallelTimes.Length - 1; index++)
            {
                parallelTimes[index] = index;
            }

            ParallelOptions options = new ParallelOptions()
            {
                MaxDegreeOfParallelism = arrayLength
            };
            //记录程序执行所花费时间
            var bv = System.Diagnostics.Stopwatch.StartNew();
            bv.Start();

            Parallel.ForEach(parallelTimes, options, (v) =>
            {
                //调用具体方法
                ElapsedMilliseconds[v] = callService();

            });

            bv.Stop();

            long serverTotalElapsed = new long();
            foreach (var ms in ElapsedMilliseconds)
            {
                serverTotalElapsed += ms;
            }


            var clientTotalElapsed = bv.ElapsedMilliseconds;

            Console.Read();
        }
阅读更多
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/xu_guowei/article/details/51546843
文章标签: 模拟并发执行
个人分类: C# .net
上一篇域验证方式生成OrgService
下一篇异步执行
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭