forked from reactiveui/ReactiveUI
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReactiveCommandCreateBenchmark.cs
More file actions
109 lines (94 loc) · 4.23 KB
/
ReactiveCommandCreateBenchmark.cs
File metadata and controls
109 lines (94 loc) · 4.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
// Copyright (c) 2019 .NET Foundation and Contributors. All rights reserved.
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for full license information.
using System;
using System.Reactive.Linq;
using System.Reactive.Subjects;
using System.Threading.Tasks;
using BenchmarkDotNet.Attributes;
namespace ReactiveUI.Benchmarks
{
/// <summary>
/// Benchmarks associated with the ReactiveCommand object.
/// </summary>
[ClrJob]
[CoreJob]
[MemoryDiagnoser]
[MarkdownExporterAttribute.GitHub]
public class ReactiveCommandCreateBenchmark
{
private readonly IObservable<bool> _canExecute = new Subject<bool>().AsObservable();
/// <summary>
/// Gets or sets from observable.
/// </summary>
public Subject<MockViewModel> FromObservable { get; set; }
/// <summary>
/// Gets or sets from task.
/// </summary>
public Task FromTask { get; set; }
/// <summary>
/// Setup for all benchmark instances being run.
/// </summary>
[GlobalSetup]
public void Setup()
{
FromObservable = new Subject<MockViewModel>();
FromTask = Task.CompletedTask;
}
/// <summary>
/// Benchmark for creating a ReactiveCommand.
/// </summary>
/// <returns>The command.</returns>
[Benchmark(Baseline = true)]
public object CreateReactiveCommand() => ReactiveCommand.Create(() => { });
/// <summary>
/// Benchmark for creating a ReactiveCommand from an observable.
/// </summary>
/// <returns>The command.</returns>
[Benchmark]
public object CreateReactiveCommandFromObservable() => ReactiveCommand.CreateFromObservable(() => FromObservable.AsObservable());
/// <summary>
/// Benchmark for creating a ReactiveCommand a task.
/// </summary>
/// <returns>The command.</returns>
[Benchmark]
public object CreateReactiveCommandFromTask() => ReactiveCommand.CreateFromTask(async () => await FromTask);
/// <summary>
/// Benchmark for creating a ReactiveCommand with a can execute observable.
/// </summary>
/// <returns>The command.</returns>
[Benchmark]
public object CreateWithCanExecute() => ReactiveCommand.Create(() => { }, _canExecute);
/// <summary>
/// Benchmark for creating a ReactiveCommand from an observable with a can execute observable.
/// </summary>
/// <returns>The command.</returns>
[Benchmark]
public object CreateFromObservableWithCanExecute() => ReactiveCommand.CreateFromObservable(() => FromObservable.AsObservable(), _canExecute);
/// <summary>
/// Benchmark for creating a ReactiveCommand from a task with a can execute observable.
/// </summary>
/// <returns>The command.</returns>
[Benchmark]
public object CreateFromTaskWithCanExecute() => ReactiveCommand.CreateFromTask(async () => await FromTask, _canExecute);
/// <summary>
/// Benchmark for creating a ReactiveCommand with a can execute observable.
/// </summary>
/// <returns>The command.</returns>
[Benchmark]
public object CreateWithCanExecuteAndScheduler() => ReactiveCommand.Create(() => { }, _canExecute, RxApp.MainThreadScheduler);
/// <summary>
/// Benchmark for creating a ReactiveCommand from an observable with a can execute observable.
/// </summary>
/// <returns>The command.</returns>
[Benchmark]
public object CreateFromObservableWithCanExecuteAndScheduler() => ReactiveCommand.CreateFromObservable(() => FromObservable.AsObservable(), _canExecute, RxApp.MainThreadScheduler);
/// <summary>
/// Benchmark for creating a ReactiveCommand from a task with a can execute observable.
/// </summary>
/// <returns>The command.</returns>
[Benchmark]
public object CreateFromTaskWithCanExecuteAndScheduler() => ReactiveCommand.CreateFromTask(async () => await FromTask, _canExecute, RxApp.MainThreadScheduler);
}
}