-
Notifications
You must be signed in to change notification settings - Fork 44
Expand file tree
/
Copy pathPoolThreadingBenchmarks.cs
More file actions
123 lines (100 loc) · 3.31 KB
/
PoolThreadingBenchmarks.cs
File metadata and controls
123 lines (100 loc) · 3.31 KB
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
using BenchmarkDotNet.Attributes;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
namespace Jering.Javascript.NodeJS.Performance
{
// Benchmarks for threading methods considered for HttpNodeJSPoolServices
[MemoryDiagnoser]
public class PoolThreadingBenchmarks
{
private LockFreeMethod? _lockFree;
private LockMethod? _lock;
private readonly IEnumerable<int> _source = Enumerable.Range(0, 2000);
[GlobalSetup]
public void Setup()
{
_lock = new LockMethod();
_lockFree = new LockFreeMethod();
// Keep benchmarks consistent
ThreadPool.SetMinThreads(Environment.ProcessorCount, Environment.ProcessorCount);
ThreadPool.SetMaxThreads(Environment.ProcessorCount, Environment.ProcessorCount);
}
[Benchmark]
public void Lock()
{
_lock!.GetDummyObject();
}
[Benchmark]
public void LockFree()
{
_lockFree!.GetDummyObject();
}
[Benchmark]
public void LockFree_Parallel()
{
Parallel.ForEach(_source, LockFree_GetDummyObject);
}
private void LockFree_GetDummyObject(int _)
{
_lockFree!.GetDummyObject();
}
[Benchmark]
public void Lock_Parallel()
{
Parallel.ForEach(_source, Lock_GetDummyObject);
}
private void Lock_GetDummyObject(int _)
{
_lock!.GetDummyObject();
}
private class LockMethod
{
private readonly ReadOnlyCollection<object> _dummyObjects;
private readonly int _maxIndex;
private readonly object _dummyObjectsLock = new();
private int _nextIndex;
public int Size { get; }
public LockMethod()
{
var dummyObjects = new ReadOnlyCollection<object>(Enumerable.Range(0, 16).Select(x => new object()).ToList());
_dummyObjects = dummyObjects;
Size = dummyObjects.Count;
_maxIndex = Size - 1;
}
internal object GetDummyObject()
{
int index = 0;
lock (_dummyObjectsLock)
{
if (_nextIndex > _maxIndex)
{
_nextIndex = 0;
}
index = _nextIndex++;
}
return _dummyObjects[index];
}
}
private class LockFreeMethod
{
private readonly ReadOnlyCollection<object> _dummyObjects;
private int _nextIndex;
public int Size { get; }
public LockFreeMethod()
{
var dummyObjects = new ReadOnlyCollection<object>(Enumerable.Range(0, 16).Select(x => new object()).ToList());
_dummyObjects = dummyObjects;
Size = dummyObjects.Count;
}
internal object GetDummyObject()
{
uint index = unchecked((uint)Interlocked.Increment(ref _nextIndex));
return _dummyObjects[(int)(index % Size)];
}
}
}
}