static void SuccessAsync()
 {
     CodeTimer.TimeAsync(true, "success: async void", Iterations, () =>
     {
         Task t = TestMethodSuccessAsync();
         OnFaultOrSuccessAsync(t, SuccessAction, "PUBLISH");
         return(t);
     });
 }
 static void SuccessCW()
 {
     CodeTimer.TimeAsync(true, "success: ContinueWith", Iterations, () =>
     {
         Task t = TestMethodSuccessAsync();
         t.OnFaultOrSuccess(FaultOrSuccessContinuationAction);
         return(t);
     });
 }
 static void SyncHack2()
 {
     CodeTimer.TimeAsync(true, "sync success: async void hack2", Iterations, () =>
     {
         Task t = TestMethodSyncSuccessAsync();
         OnFaultOrSuccessAsyncHack2(t, FaultOrSuccessContinuationAction);
         return(t);
     });
 }
 static void TcsHack2()
 {
     CodeTimer.TimeAsync(true, "sync success: async void hack2", Iterations, () =>
     {
         var tcs = new TaskCompletionSource <int>();
         tcs.TrySetResult(0);
         OnFaultOrSuccessAsyncHack2(tcs.Task, FaultOrSuccessContinuationAction);
         return(tcs.Task);
     });
 }
 static void TcsHack()
 {
     CodeTimer.TimeAsync(true, "sync succcess: async void hack", Iterations, () =>
     {
         var tcs = new TaskCompletionSource <int>();
         tcs.TrySetResult(0);
         OnFaultOrSuccessAsyncHack(tcs.Task, SuccessAction, "PUBLISH");
         return(tcs.Task);
     });
 }
 static void TcsCW()
 {
     CodeTimer.TimeAsync(true, "ContinueWith", Iterations, () =>
     {
         var tcs = new TaskCompletionSource <int>();
         tcs.TrySetResult(0);
         tcs.Task.OnFaultOrSuccess(FaultOrSuccessContinuationAction);
         return(tcs.Task);
     });
 }
        public static long Run()
        {
            long      ran        = 0;
            const int Iterations = 350 * 1000; // 100 * 1000 * 1000;

            CodeTimer.TimeAsync(true, "direct", 4, async() =>
            {
                int threads = Environment.ProcessorCount;
                int len     = Iterations / threads;
                var ttasks  = new Task[threads];
                for (int ti = 0; ti < threads; ti++)
                {
                    ttasks[ti] = Task.Factory.StartNew(
                        async() =>
                    {
                        var tasks = new Task[len];
                        for (int i = 0; i < len; i++)
                        {
                            tasks[i] = TestAsync().TryWithTimeout(TimeSpan.FromMilliseconds(100));
                        }
                        await Task.WhenAll(tasks);
                    },
                        TaskCreationOptions.LongRunning);
                }
                await Task.WhenAll(ttasks);
            });

            CodeTimer.TimeAsync(true, "timer wheel", 4, async() =>
            {
                int threads = Environment.ProcessorCount;
                int len     = Iterations / threads;
                var ttasks  = new Task[threads];
                for (int ti = 0; ti < threads; ti++)
                {
                    ttasks[ti] = Task.Factory.StartNew(
                        async() =>
                    {
                        var tasks = new Task[len];
                        for (int i = 0; i < len; i++)
                        {
                            tasks[i] = TestAsync().TryWithTimeoutWheel(100);
                        }
                        await Task.WhenAll(tasks);
                    },
                        TaskCreationOptions.LongRunning);
                }
                await Task.WhenAll(ttasks);
            });

            return(ran);
        }
        public static long Run()
        {
            long      ran        = 0;
            const int depth      = 5;
            const int Iterations = 100 * 1000; // 100 * 1000 * 1000;

            CodeTimer.TimeAsync(true, "try", Iterations, async() =>
            {
                try
                {
                    Try <bool> result = await NestedTryFailureAsync(depth);
                    if (!result.Success)
                    {
                        ran++;
                    }
                }
                catch (Exception)
                {
                    ran++;
                }
            });

            CodeTimer.TimeAsync(true, "exception no stack", Iterations, async() =>
            {
                try
                {
                    await NestedExceptionNoTraceFailureAsync(depth);
                }
                catch (Exception)
                {
                    ran++;
                }
            });

            CodeTimer.TimeAsync(true, "exception", Iterations, async() =>
            {
                try
                {
                    await NestedExceptionFailureAsync(depth);
                }
                catch (Exception)
                {
                    ran++;
                }
            });

            return(ran);
        }
Exemplo n.º 9
0
        static long MeasureAsyncVsContinueWith()
        {
            const int iterations = 1000000;

            long ran = 0;

            Task cachedTask = Task.FromResult(0);

            CodeTimer.TimeAsync(true, "Empty Async",
                                iterations,
                                () => cachedTask);

            CodeTimer.TimeAsync(true, "Await",
                                iterations,
                                async() =>
            {
                ran += 1;
                ran += await MeasureAsyncVsContinueWith_TaskSource();
            });

            CodeTimer.TimeAsync(true, "ContinueWith",
                                iterations,
                                () =>
            {
                ran += 1;
                return(MeasureAsyncVsContinueWith_TaskSource()
                       .ContinueWith(t => { ran += t.Result; },
                                     TaskContinuationOptions.OnlyOnRanToCompletion));
            });

            CodeTimer.TimeAsync(true, "ContinueWith on Awaiter",
                                iterations,
                                () =>
            {
                ran            += 1;
                Task <int> task = MeasureAsyncVsContinueWith_TaskSource();
                task.GetAwaiter().OnCompleted(() => ran += task.Result);
                return(task);
            });
            return(ran);
        }
Exemplo n.º 10
0
        static long MeasureHMHVsPipeline()
        {
            const int iterations = 1000;
            long      ran        = 0;

            Adelegate lastFunc = c => Task.FromResult(c.Value + 1);

            Adelegate a1 = async(context) =>
            {
                int result = await lastFunc(context);

                return(result + 1);
            };

            Adelegate a2 = async context =>
            {
                int result = await a1(context);

                return(result - 1);
            };

            Bdelegate lastFuncB = c =>
            {
                c.Value = c.Value + 1;
                return(TaskConstants.Completed);
            };

            Bdelegate b1 = context =>
            {
                context.Value = context.Value + 1;
                return(null);
            };

            Bdelegate b2 = context =>
            {
                context.Value = context.Value - 1;
                return(null);
            };

            CodeTimer.TimeAsync(
                true,
                "Chained async",
                iterations,
                async() =>
            {
                int result = await a2(new MeasureHPContext());
                unchecked
                {
                    ran += result;
                }
            });

            Bdelegate[] pipeline = { lastFuncB, b1, b2 };

            CodeTimer.TimeAsync(
                true,
                "Iterated async",
                iterations,
                async() =>
            {
                var ctx = new MeasureHPContext();
                foreach (Bdelegate d in pipeline)
                {
                    Task t = d(ctx);
                    if (t != null)
                    {
                        await t;
                    }
                }
                unchecked
                {
                    ran += ctx.Value;
                }
            });
            return(ran);
        }
        public static long Run()
        {
            long      ran        = 0;
            const int Iterations = 1000 * 1000; // 100 * 1000 * 1000;

            CodeTimer.Time(true, "async void", Iterations, () =>
            {
                Task t = TestMethodFailureAsync();
                OnFaultAsync(t, "PUBLISH");
            });

            CodeTimer.Time(true, "ContinueWith", Iterations, () => { TestMethodFailureAsync().OnFault(FaultContinuationAction); });

            CodeTimer.Time(true, "async void hack", Iterations, () =>
            {
                Task t = TestMethodFailureAsync();
                OnFaultAsyncHack(t, "PUBLISH");
            });

            CodeTimer.TimeAsync(true, "success: async void", Iterations, () =>
            {
                Task t = TestMethodSuccessAsync();
                OnFaultAsync(t, "PUBLISH");
                return(t);
            });

            CodeTimer.TimeAsync(true, "success: ContinueWith", Iterations, () =>
            {
                Task t = TestMethodSuccessAsync();
                t.OnFault(FaultContinuationAction);
                return(t);
            });

            CodeTimer.TimeAsync(true, "success: async void hack", Iterations, () =>
            {
                Task t = TestMethodSuccessAsync();
                OnFaultAsyncHack(t, "PUBLISH");
                return(t);
            });

            CodeTimer.TimeAsync(true, "sync succcess: async void", Iterations, () =>
            {
                Task t = TestMethodSyncSuccessAsync();
                OnFaultAsync(t, "PUBLISH");
                return(t);
            });

            CodeTimer.TimeAsync(true, "sync succcess: ContinueWith", Iterations, () =>
            {
                Task t = TestMethodSyncSuccessAsync();
                t.OnFault(FaultContinuationAction);
                return(t);
            });

            CodeTimer.TimeAsync(true, "sync succcess: async void hack", Iterations, () =>
            {
                Task t = TestMethodSyncSuccessAsync();
                OnFaultAsyncHack(t, "PUBLISH");
                return(t);
            });

            return(ran);
        }