55 lines
1.3 KiB
C#
55 lines
1.3 KiB
C#
using System.Diagnostics;
|
|
|
|
namespace Nerfed.Runtime.Systems.Synthetic;
|
|
|
|
internal static class DummyWorkloadRunner
|
|
{
|
|
public static int Run(TimeSpan duration, DummyWorkloadMode mode, int seed)
|
|
{
|
|
return mode switch
|
|
{
|
|
DummyWorkloadMode.Sleep => Sleep(duration, seed),
|
|
DummyWorkloadMode.Compute => Compute(duration, seed),
|
|
_ => Spin(duration, seed),
|
|
};
|
|
}
|
|
|
|
private static int Sleep(TimeSpan duration, int seed)
|
|
{
|
|
if (duration > TimeSpan.Zero)
|
|
{
|
|
Thread.Sleep(duration);
|
|
}
|
|
|
|
return seed;
|
|
}
|
|
|
|
private static int Spin(TimeSpan duration, int seed)
|
|
{
|
|
Stopwatch stopwatch = Stopwatch.StartNew();
|
|
int value = seed;
|
|
while (stopwatch.Elapsed < duration)
|
|
{
|
|
value = unchecked((value * 1664525) + 1013904223);
|
|
Thread.SpinWait(128);
|
|
}
|
|
|
|
return value;
|
|
}
|
|
|
|
private static int Compute(TimeSpan duration, int seed)
|
|
{
|
|
Stopwatch stopwatch = Stopwatch.StartNew();
|
|
int value = seed;
|
|
while (stopwatch.Elapsed < duration)
|
|
{
|
|
for (int i = 0; i < 2048; i++)
|
|
{
|
|
value = unchecked((value << 5) - value + i);
|
|
}
|
|
}
|
|
|
|
return value;
|
|
}
|
|
}
|