Browse Source

4.x: Benchmark Subject

akarnokd 7 years ago
parent
commit
b6f64f93e3

+ 1 - 0
Rx.NET/Source/benchmarks/Benchmarks.System.Reactive/Program.cs

@@ -26,6 +26,7 @@ namespace Benchmarks.System.Reactive
                 typeof(ComparisonAsyncBenchmark),
                 typeof(ScalarScheduleBenchmark),
                 typeof(StableCompositeDisposableBenchmark),
+                typeof(SubjectBenchmark),
                 typeof(ComparisonAsyncBenchmark)
 #if (CURRENT)
                 ,typeof(AppendPrependBenchmark)

+ 44 - 0
Rx.NET/Source/benchmarks/Benchmarks.System.Reactive/SubjectBenchmark.cs

@@ -0,0 +1,44 @@
+// Licensed to the .NET Foundation under one or more agreements.
+// The .NET Foundation licenses this file to you under the Apache 2.0 License.
+// See the LICENSE file in the project root for more information.
+
+using System;
+using System.Reactive.Linq;
+using System.Reactive.Subjects;
+using System.Threading;
+using BenchmarkDotNet.Attributes;
+
+namespace Benchmarks.System.Reactive
+{
+    [MemoryDiagnoser]
+    public class SubjectBenchmark
+    {
+        [Params(1, 10, 100, 1000, 10000, 100000, 1000000)]
+        public int N;
+
+        [Params(0, 1, 2, 3, 4, 5)]
+        public int M;
+
+        private int _store;
+
+        [Benchmark]
+        public object SubjectPush()
+        {
+            var subj = new Subject<int>();
+            var consumers = new IDisposable[M];
+            var m = M;
+            for (var i = 0; i < m; i++)
+            {
+                consumers[i] = subj.Subscribe(v => Volatile.Write(ref _store, v));
+            }
+
+            var n = N;
+            for (var i = 0; i < n; i++)
+            {
+                subj.OnNext(i);
+            }
+
+            return consumers;
+        }
+    }
+}