-
Notifications
You must be signed in to change notification settings - Fork 272
/
AddRemoveSteadyState.cs
98 lines (81 loc) · 3.51 KB
/
AddRemoveSteadyState.cs
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
// 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 more information.
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Collections.Immutable;
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Extensions;
using MicroBenchmarks;
namespace System.Collections.Tests
{
[BenchmarkCategory(Categories.Libraries, Categories.Collections, Categories.GenericCollections)]
[GenericTypeArguments(typeof(int))] // value type
[GenericTypeArguments(typeof(string))] // reference type
public class Add_Remove_SteadyState<T> // serialized producer/consumer throughput when the collection has reached a steady-state size
{
private ConcurrentBag<T> _concurrentBag;
private ConcurrentQueue<T> _concurrentQueue;
private ConcurrentStack<T> _concurrentStack;
private Queue<T> _queue;
private Stack<T> _stack;
private ImmutableQueue<T> _immutableQueue;
private ImmutableStack<T> _immutableStack;
[Params(Utils.DefaultCollectionSize)]
public int Count;
[GlobalSetup(Target = nameof(ConcurrentBag))]
public void SetupConcurrentBag() => _concurrentBag = new ConcurrentBag<T>(ValuesGenerator.ArrayOfUniqueValues<T>(Count));
[Benchmark]
public void ConcurrentBag()
{
_concurrentBag.TryTake(out T item);
_concurrentBag.Add(item);
}
[GlobalSetup(Target = nameof(ConcurrentQueue))]
public void SetupConcurrentQueue() => _concurrentQueue = new ConcurrentQueue<T>(ValuesGenerator.ArrayOfUniqueValues<T>(Count));
[Benchmark]
public void ConcurrentQueue()
{
_concurrentQueue.TryDequeue(out T item);
_concurrentQueue.Enqueue(item);
}
[GlobalSetup(Target = nameof(ConcurrentStack))]
public void SetupConcurrentStack() => _concurrentStack = new ConcurrentStack<T>(ValuesGenerator.ArrayOfUniqueValues<T>(Count));
[Benchmark]
public void ConcurrentStack()
{
_concurrentStack.TryPop(out T item);
_concurrentStack.Push(item);
}
[GlobalSetup(Target = nameof(Queue))]
public void SetupQueue() => _queue = new Queue<T>(ValuesGenerator.ArrayOfUniqueValues<T>(Count));
[Benchmark]
public void Queue()
{
T item = _queue.Dequeue();
_queue.Enqueue(item);
}
[GlobalSetup(Target = nameof(Stack))]
public void SetupStack() => _stack = new Stack<T>(ValuesGenerator.ArrayOfUniqueValues<T>(Count));
[Benchmark]
public void Stack()
{
T item = _stack.Pop();
_stack.Push(item);
}
[GlobalSetup(Target = nameof(ImmutableQueue))]
public void SetupImmutableQueue() => _immutableQueue = Immutable.ImmutableQueue.CreateRange<T>(ValuesGenerator.ArrayOfUniqueValues<T>(Count));
[Benchmark]
public void ImmutableQueue()
{
_immutableQueue = _immutableQueue.Dequeue(out T item).Enqueue(item);
}
[GlobalSetup(Target = nameof(ImmutableStack))]
public void SetupImmutableStack() => _immutableStack = Immutable.ImmutableStack.CreateRange<T>(ValuesGenerator.ArrayOfUniqueValues<T>(Count));
[Benchmark]
public void ImmutableStack()
{
_immutableStack = _immutableStack.Pop(out T item).Push(item);
}
}
}