|
| 1 | +using System; |
| 2 | +using System.ComponentModel.DataAnnotations; |
| 3 | +using System.Reactive.Subjects; |
| 4 | +using BenchmarkDotNet.Attributes; |
| 5 | +using Reactive.Bindings; |
| 6 | +using Reactive.Bindings.Extensions; |
| 7 | + |
| 8 | +namespace ReactivePropertyBenchmark; |
| 9 | + |
| 10 | +public partial class BasicUsages |
| 11 | +{ |
| 12 | + [Benchmark] |
| 13 | + public ReactiveCommand CreateReactiveCommand() => new ReactiveCommand(); |
| 14 | + |
| 15 | + [Benchmark] |
| 16 | + public ReactiveCommandSlim CreateReactiveCommandSlim() => new ReactiveCommandSlim(); |
| 17 | + |
| 18 | + |
| 19 | + |
| 20 | + [Benchmark] |
| 21 | + public ReactiveCommand BasicUsecaseForReactiveCommand() |
| 22 | + { |
| 23 | + var subject = new Subject<bool>(); |
| 24 | + var cmd = subject.ToReactiveCommand(); |
| 25 | + cmd.Subscribe(x => { }); |
| 26 | + cmd.Execute(); |
| 27 | + subject.OnNext(true); |
| 28 | + cmd.Execute(); |
| 29 | + return cmd; |
| 30 | + } |
| 31 | + |
| 32 | + [Benchmark] |
| 33 | + public ReactiveCommandSlim BasicUsecaseForReactiveCommandSlim() |
| 34 | + { |
| 35 | + var subject = new Subject<bool>(); |
| 36 | + var cmd = subject.ToReactiveCommandSlim(); |
| 37 | + cmd.Subscribe(x => { }); |
| 38 | + cmd.Execute(); |
| 39 | + subject.OnNext(true); |
| 40 | + cmd.Execute(); |
| 41 | + return cmd; |
| 42 | + } |
| 43 | + |
| 44 | + [Benchmark] |
| 45 | + public ReactiveProperty<string> ReactivePropertyValidation() |
| 46 | + { |
| 47 | + var rp = new ReactiveProperty<string>("") |
| 48 | + .SetValidateNotifyError(x => string.IsNullOrEmpty(x) ? "invalid" : null); |
| 49 | + rp.Value = "xxx"; // valid |
| 50 | + rp.Value = ""; // invalid |
| 51 | + return rp; |
| 52 | + } |
| 53 | + |
| 54 | + [Benchmark] |
| 55 | + public ValidatableReactiveProperty<string> ValidatableReactivePropertyValidation() |
| 56 | + { |
| 57 | + var rp = new ValidatableReactiveProperty<string>( |
| 58 | + "", |
| 59 | + x => string.IsNullOrEmpty(x) ? "invalid" : null); |
| 60 | + rp.Value = "xxx"; // valid |
| 61 | + rp.Value = ""; // invalid |
| 62 | + return rp; |
| 63 | + } |
| 64 | + |
| 65 | + [Benchmark] |
| 66 | + public ReactivePropertyVM ReactivePropertyValidationFromPoco() |
| 67 | + { |
| 68 | + var vm = new ReactivePropertyVM(); |
| 69 | + vm.Name.Value = "valid"; |
| 70 | + vm.Name.Value = ""; |
| 71 | + vm.Dispose(); |
| 72 | + return vm; |
| 73 | + } |
| 74 | + |
| 75 | + [Benchmark] |
| 76 | + public ValidatableReactivePropertyVM ValidatableReactivePropertyValidationFromPoco() |
| 77 | + { |
| 78 | + var vm = new ValidatableReactivePropertyVM(); |
| 79 | + vm.Name.Value = "valid"; |
| 80 | + vm.Name.Value = ""; |
| 81 | + vm.Dispose(); |
| 82 | + return vm; |
| 83 | + } |
| 84 | + |
| 85 | + [Benchmark] |
| 86 | + public IObservable<string> ObservePropertyLegacy() |
| 87 | + { |
| 88 | + var p = new Person(); |
| 89 | + return p.ObservePropertyLegacy(x => x.Name); |
| 90 | + } |
| 91 | + |
| 92 | + [Benchmark] |
| 93 | + public ReactivePropertySlim<string> ToReactivePropertyAsSynchronizedSlim() |
| 94 | + { |
| 95 | + var p = new Person(); |
| 96 | + return p.ToReactivePropertySlimAsSynchronized(x => x.Name); |
| 97 | + } |
| 98 | +} |
| 99 | + |
| 100 | +public class ReactivePropertyVM : IDisposable |
| 101 | +{ |
| 102 | + private Person _model = new(); |
| 103 | + |
| 104 | + [Required] |
| 105 | + public ReactiveProperty<string> Name { get; } |
| 106 | + |
| 107 | + public ReactivePropertyVM() |
| 108 | + { |
| 109 | + Name = _model.ToReactivePropertyAsSynchronized(x => x.Name, |
| 110 | + ignoreValidationErrorValue: true) |
| 111 | + .SetValidateAttribute(() => Name); |
| 112 | + } |
| 113 | + |
| 114 | + public void Dispose() |
| 115 | + { |
| 116 | + Name.Dispose(); |
| 117 | + } |
| 118 | +} |
| 119 | + |
| 120 | +public class ValidatableReactivePropertyVM : IDisposable |
| 121 | +{ |
| 122 | + private Person _model = new(); |
| 123 | + |
| 124 | + [Required] |
| 125 | + public ValidatableReactiveProperty<string> Name { get; } |
| 126 | + |
| 127 | + public ValidatableReactivePropertyVM() |
| 128 | + { |
| 129 | + Name = _model.ToReactivePropertySlimAsSynchronized(x => x.Name) |
| 130 | + .ToValidatableReactiveProperty( |
| 131 | + () => Name, |
| 132 | + disposeSource: true); |
| 133 | + } |
| 134 | + |
| 135 | + public void Dispose() |
| 136 | + { |
| 137 | + Name.Dispose(); |
| 138 | + } |
| 139 | +} |
0 commit comments