Skip to content

Commit 58eaa67

Browse files
authored
Drain threads for memory cache hit rate analysis (#553)
* drain threads * faster * fix title ---------
1 parent a60f44e commit 58eaa67

File tree

3 files changed

+29
-1
lines changed

3 files changed

+29
-1
lines changed

BitFaster.Caching.HitRateAnalysis/Arc/Runner.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public async Task Run()
3131

3232
this.config.Analysis.WriteToConsole();
3333
Analysis<long>.WriteToFile(this.config.Name, this.config.Analysis);
34-
Analysis<long>.Plot(this.config.Title, this.config.Name, this.config.Analysis);
34+
Analysis<long>.Plot(this.config.Name, this.config.Title, this.config.Analysis);
3535
}
3636

3737
private int AnalyzeSmall()

BitFaster.Caching.HitRateAnalysis/MemoryCacheAdaptor.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ public V GetOrAdd(K key, Func<K, V> valueFactory)
6363
entry.SetSize(1);
6464

6565
this.metrics.requestMissCount++;
66+
ThreadPoolInspector.WaitForEmpty();
6667
}
6768
else
6869
{
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
using System;
2+
using System.Threading;
3+
4+
namespace BitFaster.Caching.HitRateAnalysis
5+
{
6+
internal class ThreadPoolInspector
7+
{
8+
public static void WaitForEmpty()
9+
{
10+
while (ThreadPool.PendingWorkItemCount > 0)
11+
{
12+
Thread.Yield();
13+
14+
// This is very hacky, but by experimentation 300 Sleep(0) consistently takes longer
15+
// than cache maintenance giving stable results with around 25% run time penalty.
16+
// Sleep(1) makes the test take 50x longer.
17+
if (ThreadPool.PendingWorkItemCount == 0)
18+
{
19+
for (int i = 0; i < 300; i++)
20+
{
21+
Thread.Sleep(0);
22+
}
23+
}
24+
}
25+
}
26+
}
27+
}

0 commit comments

Comments
 (0)