-
Notifications
You must be signed in to change notification settings - Fork 0
/
DemoJobs.cs
51 lines (44 loc) · 1.66 KB
/
DemoJobs.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
using System.Diagnostics;
using System.Runtime.InteropServices;
namespace Universe.HangfireCpuUsage.DemoWebApplication;
public class DemoJobs
{
public async Task MultiThreadCpuStressThenFall(int threadsCount, int requiredCpuUsage)
{
MultiThreadCpuStress(threadsCount, requiredCpuUsage);
throw new InvalidOperationException("Operation failed on purpose");
}
public async Task MultiThreadCpuStress(int threadsCount, int requiredCpuUsage)
{
List<Task> tasks = new List<Task>();
for (int i = 1; i <= threadsCount; i++)
tasks.Add(Task.Run(() => LoadCpu(1, requiredCpuUsage, true)));
Task.WaitAll(tasks.ToArray());
}
public async Task CpuStress(int requiredCpuUsage)
{
await MultiThreadCpuStress(1, requiredCpuUsage);
}
public async Task Sleep(int duration, CancellationToken cancellationToken)
{
await Task.Delay(duration, cancellationToken);
}
private void LoadCpu(int minDuration, int minCpuUsage, bool needKernelLoad = true)
{
Stopwatch sw = Stopwatch.StartNew();
CpuUsage.CpuUsage firstUsage = CpuUsage.CpuUsage.GetByThread().Value;
bool isDone = false;
while (!isDone)
{
for(int i=1; i <= (needKernelLoad ? 3 : 0); i++)
{
IntPtr ptr = Marshal.AllocHGlobal(2 * 1024);
Marshal.FreeHGlobal(ptr);
}
CpuUsage.CpuUsage nextUsage = CpuUsage.CpuUsage.GetByThread().Value;
isDone =
sw.ElapsedMilliseconds >= minDuration
&& (nextUsage - firstUsage).TotalMicroSeconds >= minCpuUsage * 1000L;
}
}
}