GroupByCompletion.cs 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. // Licensed to the .NET Foundation under one or more agreements.
  2. // The .NET Foundation licenses this file to you under the MIT License.
  3. // See the LICENSE file in the project root for more information.
  4. using System;
  5. using System.Reactive.Linq;
  6. using BenchmarkDotNet.Attributes;
  7. namespace Benchmarks.System.Reactive
  8. {
  9. /// <summary>
  10. /// Completion of a wide fan-out/in scenario.
  11. /// </summary>
  12. /// <remarks>
  13. /// <para>
  14. /// This was added to address https://github.com/dotnet/reactive/issues/2005 in which completion
  15. /// takes longer and longer to handle as the number of groups increases.
  16. /// </para>
  17. /// <para>
  18. /// The queries in this benchmark represent the common 'fan out/in' pattern in Rx. It is often
  19. /// useful to split a stream into groups to enable per-group processing, and then to recombine
  20. /// the data back into a single stream. These benchmarks don't do any per-group processing, so
  21. /// they might look pointless, but we're trying to measure the minimum unavoidable overhead
  22. /// that any code using this technique will encounter.
  23. /// </para>
  24. /// </remarks>
  25. [MemoryDiagnoser]
  26. public class GroupByCompletion
  27. {
  28. private IObservable<int> observable;
  29. [Params(200_000, 1_000_000)]
  30. public int NumberOfSamples { get; set; }
  31. [Params(10, 100, 1_000, 10_000, 100_000, 150_000, 200_000)]
  32. public int NumberOfGroups { get; set; }
  33. [GlobalSetup]
  34. public void GlobalSetup()
  35. {
  36. var data = new int[NumberOfSamples];
  37. for (var i = 0; i < data.Length; ++i)
  38. {
  39. data[i] = i;
  40. }
  41. observable = data.ToObservable();
  42. }
  43. [Benchmark]
  44. public void GroupBySelectMany()
  45. {
  46. var numberOfGroups = NumberOfGroups;
  47. observable!.GroupBy(value => value % numberOfGroups)
  48. .SelectMany(groupOfInts => groupOfInts)
  49. .Subscribe(intValue => { });
  50. }
  51. [Benchmark]
  52. public void GroupByMerge()
  53. {
  54. var numberOfGroups = NumberOfGroups;
  55. observable!.GroupBy(value => value % numberOfGroups)
  56. .Merge()
  57. .Subscribe(intValue => { });
  58. }
  59. }
  60. }