-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
138 lines (119 loc) · 5.01 KB
/
Program.cs
File metadata and controls
138 lines (119 loc) · 5.01 KB
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
using System;
using System.Diagnostics;
using System.Threading;
using Pooler;
class Program {
const int RUNNING_THREADS_IN_TEST_TOTAL = 3000;
const int RUNNING_THREADS_IN_TEST_SIMULTANEOUSLY = 50;
private static Pooler.Base _pool;
private static System.Diagnostics.Stopwatch _stopWatch;
private static System.Threading.Timer _reportTimer;
private static int _taskBeginCounter = 0;
private static int _taskExceptionCounter = 0;
private static int _taskDoneCounter = 0;
private static int _currentlyRunningTasksCounter = 0;
private static int _peakThreadsCounter = 0;
private static object _taskBeginCounterLock = new object { };
private static object _taskExceptionCounterLock = new object { };
private static object _taskDoneCounterLock = new object { };
static void Main (string[] args) {
// init threads pool for parallel pool accepting each task as different call:
Program._initParallelThreadsPool();
// init threads pool for repeater pool accepting each task as the same call:
//Program._initRepeaterThreadsPool();
// start the test:
Program._pool.StartProcessing();
Thread.Sleep(2000);
int origValue = Program._pool.GetMaxRunningTasks();
Program._pool.SetMaxRunningTasks(200);
Thread.Sleep(2000);
Program._pool.SetMaxRunningTasks(origValue);
Console.ReadLine();
}
private static void _initParallelThreadsPool () {
Parallel pool = Parallel.CreateNew(Program.RUNNING_THREADS_IN_TEST_SIMULTANEOUSLY);
pool.AllDone += Program._allDoneHandler;
pool.TaskDone += Program._taskDoneHandler;
pool.TaskException += Program._threadExceptionHandler;
for (int i = 0; i < Program.RUNNING_THREADS_IN_TEST_TOTAL; i++) {
pool.Add(new Pooler.TaskDelegate(Program._testTask), false, ThreadPriority.Lowest);
}
Thread.CurrentThread.Priority = ThreadPriority.Highest;
Program._stopWatch = Stopwatch.StartNew();
Program._reportTimer = new System.Threading.Timer(delegate {
Thread.CurrentThread.Priority = ThreadPriority.Highest;
Program._report();
}, null, 0, 200);
Program._pool = pool;
}
private static void _initRepeaterThreadsPool () {
Repeater pool = Repeater.CreateNew(
Program.RUNNING_THREADS_IN_TEST_SIMULTANEOUSLY,
Program.RUNNING_THREADS_IN_TEST_TOTAL
);
pool.AllDone += Program._allDoneHandler;
pool.TaskDone += Program._taskDoneHandler;
pool.TaskException += Program._threadExceptionHandler;
pool.Set(new Pooler.TaskDelegate(Program._testTask), false, ThreadPriority.Lowest, false);
Thread.CurrentThread.Priority = ThreadPriority.Highest;
Program._stopWatch = Stopwatch.StartNew();
Program._reportTimer = new System.Threading.Timer(delegate {
Thread.CurrentThread.Priority = ThreadPriority.Highest;
Program._report();
}, null, 0, 200);
Program._pool = pool;
}
private static void _taskDoneHandler (Pooler.Base pool, TaskDoneEventArgs poolerTaskDoneEventArgs) {
lock (Program._taskDoneCounterLock) {
Program._taskDoneCounter++;
Program._currentlyRunningTasksCounter = poolerTaskDoneEventArgs.RunningTasksCount;
}
}
private static void _threadExceptionHandler (Pooler.Base pool, ExceptionEventArgs poolThreadExceptionEventArgs) {
lock (Program._taskExceptionCounterLock) {
Program._taskExceptionCounter++;
}
}
private static void _allDoneHandler (Pooler.Base pool, AllDoneEventArgs poolAllDoneEventArgs) {
Program._peakThreadsCounter = poolAllDoneEventArgs.PeakThreadsCount;
if (poolAllDoneEventArgs.Exceptions.Count != Program._taskExceptionCounter) {
throw new Exception("Exceptions counter is wrong!");
}
Program._report();
}
private static void _testTask (Pooler.Base pool) {
var i = 0;
lock (Program._taskBeginCounterLock) {
i = Program._taskBeginCounter;
Program._taskBeginCounter++;
}
Thread.Sleep(10);
if (i % 3 == 0) {
throw new Exception($"Exception modulo 3");
}
}
private static void _report () {
int[] counts = new int[] { 0, 0, 0, 0 };
lock (Program._taskBeginCounterLock) {
counts[0] = Program._taskBeginCounter;
}
lock (Program._taskDoneCounterLock) {
counts[1] = Program._taskDoneCounter;
counts[2] = Program._currentlyRunningTasksCounter;
}
lock (Program._taskExceptionCounterLock) {
counts[3] = Program._taskExceptionCounter;
}
string s = "Started tasks: " + counts[0] + Environment.NewLine
+ "Finished tasks: " + counts[1] + Environment.NewLine
+ "Threads exceptions: " + counts[3] + Environment.NewLine
+ "Currently simultaneously running threads: " + counts[2] + Environment.NewLine
+ "Maximum simultaneously runing threads peak: " + Program._peakThreadsCounter + Environment.NewLine
+ "Spended miliseconds: " + Program._stopWatch.ElapsedMilliseconds;
Console.Clear();
Console.WriteLine(s);
if (counts[1] == Program.RUNNING_THREADS_IN_TEST_TOTAL) {
Program._reportTimer.Dispose();
}
}
}