1
0

SubjectBenchmark.cs 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. // Licensed to the .NET Foundation under one or more agreements.
  2. // The .NET Foundation licenses this file to you under the Apache 2.0 License.
  3. // See the LICENSE file in the project root for more information.
  4. using System;
  5. using System.Reactive.Linq;
  6. using System.Reactive.Subjects;
  7. using System.Threading;
  8. using BenchmarkDotNet.Attributes;
  9. namespace Benchmarks.System.Reactive
  10. {
  11. [MemoryDiagnoser]
  12. public class SubjectBenchmark
  13. {
  14. [Params(1, 10, 100, 1000, 10000, 100000, 1000000)]
  15. public int N;
  16. [Params(0, 1, 2, 3, 4, 5)]
  17. public int M;
  18. private int _store;
  19. [Benchmark]
  20. public object SubjectPush()
  21. {
  22. var subj = new Subject<int>();
  23. var consumers = new IDisposable[M];
  24. var m = M;
  25. for (var i = 0; i < m; i++)
  26. {
  27. consumers[i] = subj.Subscribe(v => Volatile.Write(ref _store, v));
  28. }
  29. var n = N;
  30. for (var i = 0; i < n; i++)
  31. {
  32. subj.OnNext(i);
  33. }
  34. return consumers;
  35. }
  36. }
  37. }