Last active
March 1, 2021 21:28
-
-
Save AArnott/066b065c4408b7c5fb252dcfa538dedb to your computer and use it in GitHub Desktop.
Demonstrates scheduling unbounded work and applying a throttle to keep the threadpool responsive.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Diagnostics; | |
using System.Threading; | |
using System.Threading.Tasks; | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
Console.WriteLine("*** WITHOUT throttling ***"); | |
MeasureWork(false); | |
Console.WriteLine("*** WITH throttling ***"); | |
MeasureWork(true); | |
} | |
private static void MeasureWork(bool throttle) | |
{ | |
TaskScheduler scheduler = throttle | |
? new ConcurrentExclusiveSchedulerPair(TaskScheduler.Default, Environment.ProcessorCount * 2).ConcurrentScheduler | |
: TaskScheduler.Default; | |
Console.WriteLine("Race has begun."); | |
var sw = Stopwatch.StartNew(); | |
var workerTasks = new Task[800]; | |
for (int i = 0; i < workerTasks.Length; i++) | |
{ | |
workerTasks[i] = Task.Factory.StartNew(Work, CancellationToken.None, TaskCreationOptions.None, scheduler); | |
} | |
Task allFinished = Task.WhenAll(workerTasks); | |
allFinished.ContinueWith(_ => sw.Stop()); | |
while (!allFinished.IsCompleted) | |
{ | |
Thread.Sleep(500); | |
Console.WriteLine("Threadpool responded in: " + MeasureThreadPoolResponsiveness()); | |
} | |
Console.WriteLine($"FINISHED in {sw.Elapsed}"); | |
} | |
static void Work() | |
{ | |
SpinWait.SpinUntil(() => false, 40); | |
} | |
static TimeSpan MeasureThreadPoolResponsiveness() | |
{ | |
var sw = Stopwatch.StartNew(); | |
Task.Run(() => sw.Stop()).Wait(); | |
return sw.Elapsed; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Diagnostics; | |
using System.Threading; | |
using System.Threading.Tasks; | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
Console.WriteLine("*** WITHOUT throttling ***"); | |
MeasureWork(false); | |
Console.WriteLine("*** WITH throttling ***"); | |
MeasureWork(true); | |
} | |
private static void MeasureWork(bool throttled) | |
{ | |
SemaphoreSlim semaphore = throttled ? new SemaphoreSlim(Environment.ProcessorCount * 2) : null; | |
Console.WriteLine("Race has begun."); | |
var sw = Stopwatch.StartNew(); | |
var workerTasks = new Task[800]; | |
for (int i = 0; i < workerTasks.Length; i++) | |
{ | |
workerTasks[i] = Task.Run(async delegate | |
{ | |
if (semaphore != null) | |
{ | |
await semaphore.WaitAsync(); | |
} | |
await WorkAsync(); | |
semaphore?.Release(); | |
}); | |
} | |
Task allFinished = Task.WhenAll(workerTasks); | |
allFinished.ContinueWith(_ => sw.Stop()); | |
while (!allFinished.IsCompleted) | |
{ | |
Thread.Sleep(500); | |
Console.WriteLine("Threadpool responded in: " + MeasureThreadPoolResponsiveness()); | |
} | |
Console.WriteLine($"FINISHED in {sw.Elapsed}"); | |
} | |
static async Task WorkAsync() | |
{ | |
SpinWait.SpinUntil(() => false, 20); | |
await Task.Yield(); | |
SpinWait.SpinUntil(() => false, 20); | |
} | |
static TimeSpan MeasureThreadPoolResponsiveness() | |
{ | |
var sw = Stopwatch.StartNew(); | |
Task.Run(() => sw.Stop()).Wait(); | |
return sw.Elapsed; | |
} | |
} |
Good point, @binki. That would explain why the throttled variety ran mysteriously faster.
With your change and output that shows throttling made it slower, I suspect some optimization or picking a more efficient scheduler, or a different concurrency level than I did would reduce the difference between the two.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@AArnott Have you considered comparing the numbers after letting the thread pool warm up? I think that getting numbers from the throttled variant when the threadpool has not scaled itself up to the workload yet gives the throttling numbers an unfair advantage if you’re going to be comparing the
FINISHED in
times. These are the numbers from a run after I modified the sync version to use warmup: