-
Notifications
You must be signed in to change notification settings - Fork 4.5k
/
test45929.cs
150 lines (131 loc) · 5 KB
/
test45929.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
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.ExceptionServices;
using System.Threading;
using System.Threading.Tasks;
namespace test45929
{
public class Program
{
static int Main(string[] _)
{
Console.WriteLine("The test started.");
Console.WriteLine("Progress:");
ThreadPool.GetMinThreads(out int _, out int cptMin);
ThreadPool.SetMinThreads(3500, cptMin);
Test.Run();
Console.WriteLine("Finished successfully");
return 100;
}
class Test
{
readonly TestCore methods;
MethodInfo methodInfo;
public Test()
{
methods = new TestCore();
methodInfo = GetMethod("ExceptionDispatchInfoCaptureThrow");
if (methodInfo is null)
{
throw new InvalidOperationException("The methodInfo object is missing or empty.");
}
}
public static void Run()
{
long progress = 0;
var test = new Test();
const int MaxCount = 1000000;
int increment = 100;
bool done = false;
Stopwatch stopwatch = new Stopwatch();
stopwatch.Start();
Console.WriteLine($"{DateTime.Now} : {progress * 100D / MaxCount:000.0}% : {stopwatch.ElapsedMilliseconds}");
Action<int> makeProgress = i =>
{
if (done) return;
long newProgress = Interlocked.Increment(ref progress);
if (newProgress % increment == 0)
{
int newIncrement = (increment * 3) / 2;
if (newIncrement > 10000)
newIncrement = 10000;
increment = newIncrement;
Console.WriteLine($"{DateTime.Now} : {newProgress * 100D / MaxCount:000.0}% : {stopwatch.ElapsedMilliseconds}");
if (stopwatch.ElapsedMilliseconds > 150000)
{
Console.WriteLine($"Attempting to finish early");
done = true;
}
}
test.Invoke();
};
makeProgress(0);
Parallel.For(
1,
MaxCount,
new ParallelOptions() { MaxDegreeOfParallelism = Environment.ProcessorCount },
makeProgress);
}
public void Invoke()
{
try
{
methodInfo.Invoke(methods, null);
}
catch
{
// Ignore
}
}
static MethodInfo GetMethod(string methodName)
{
foreach (MethodInfo method in typeof(TestCore).GetMethods(BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly))
{
if (methodName == method.Name)
{
return method;
}
}
return null;
}
class TestCore
{
const int ExpirySeconds = 1000;
// An exception instance that gets refreshed every ExpirySeconds.
(ExceptionDispatchInfo Exception, DateTime CacheExpiryDateTime) exceptionCache;
/// <summary>
/// Captures and throws the a cached instance of an exception.
/// </summary>
public void ExceptionDispatchInfoCaptureThrow()
{
var error = GetCachedError();
error.Throw();
}
ExceptionDispatchInfo GetCachedError()
{
try
{
var cache = exceptionCache;
if (cache.Exception != null)
{
if (exceptionCache.CacheExpiryDateTime > DateTime.UtcNow)
{
return cache.Exception;
}
}
throw new Exception("Test");
}
catch (Exception ex)
{
ExceptionDispatchInfo edi = ExceptionDispatchInfo.Capture(ex);
exceptionCache = (edi, DateTime.UtcNow.AddSeconds(ExpirySeconds));
return edi;
}
}
}
}
}
}