-
Notifications
You must be signed in to change notification settings - Fork 0
/
Worker.cs
160 lines (143 loc) · 4.9 KB
/
Worker.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
151
152
153
154
155
156
157
158
159
160
/**
* WebStressTool
* Web Stress Tool, testing hiload sites
*
* Created by SharpDevelop.
* User: Enikeishik
* Date: 26.12.2017
* Time: 10:32
*
* @copyright Copyright (C) 2005 - 2017 Enikeishik <enikeishik@gmail.com>. All rights reserved.
* @author Enikeishik <enikeishik@gmail.com>
* @license GNU General Public License version 2 or later; see LICENSE.txt
*/
using System;
using System.Threading;
using System.Net;
namespace WebStressTool
{
/// <summary>
/// Make requests to url.
/// </summary>
public class Worker
{
protected const int threadsLimit = 32768;
protected const int threadWait = 10;
protected readonly string url;
protected readonly int requestTimeout;
protected Thread[] threads;
public bool IsAlive
{
get; protected set;
}
public delegate void WorkResultDelegate(Worker sender, WorkerResult result);
public event WorkResultDelegate onWorkResult;
public Worker(string url, int requestTimeout)
{
this.url = url;
this.requestTimeout = requestTimeout;
}
public void DoWork(int iterateNum, int threadsCount)
{
System.Net.ServicePointManager.DefaultConnectionLimit = threadsCount;
threads = new Thread[threadsCount];
for (int i = 0; i < threadsCount; i++) {
threads[i] = new Thread(ThreadProc);
threads[i].Start(new WorkerData(iterateNum, i + 1, threadsCount));
Thread.Sleep(0);
}
Thread thAwait = new Thread(AwaitRequests);
thAwait.Start();
Thread.Sleep(0);
}
public void Abort()
{
foreach (Thread t in threads) {
t.Abort();
Thread.Sleep(0);
}
}
protected void ThreadProc(object data)
{
WorkerData d = (WorkerData) data;
HttpWebRequest request = (HttpWebRequest) WebRequest.Create(url);
request.Timeout = requestTimeout;
request.KeepAlive = false;
request.ProtocolVersion = HttpVersion.Version10;
Uri uri = new Uri(url);
if (UriHostNameType.Dns == Uri.CheckHostName(uri.Host))
request.Host = uri.Host;
HttpWebResponse response = null;
try {
//todo: try to use request.GetResponseAsync() C# 5
response = (HttpWebResponse) request.GetResponse();
DebugOutput(
"(" + Thread.CurrentThread.ManagedThreadId + ")" +
" Response " +
"[" + d.iterateNum + "|" + d.threadNum + "/" + d.threadsCount + "]: " +
response.StatusCode
);
if (null != onWorkResult)
onWorkResult(this, new WorkerResult(d, response, ""));
} catch (Exception e) {
DebugOutput(
"(" + Thread.CurrentThread.ManagedThreadId + ")" +
" Error " +
"[" + d.iterateNum + "|" + d.threadNum + "/" + d.threadsCount + "]: " +
e.Message
);
if (null != onWorkResult)
onWorkResult(this, new WorkerResult(d, null, e.Message));
} finally {
if (null != response) {
response.Close();
}
}
}
[System.Diagnostics.Conditional("DEBUG")]
protected void DebugOutput(string data)
{
System.Diagnostics.Debug.WriteLine(data);
}
protected void AwaitRequests()
{
IsAlive = true;
bool alive = true;
while (alive) {
alive = false;
foreach (Thread t in threads) {
if (null != t && t.IsAlive) {
alive = true;
Thread.Sleep(threadWait);
break;
}
}
}
IsAlive = false;
}
}
public struct WorkerData
{
public int iterateNum;
public int threadNum;
public int threadsCount;
public WorkerData(int iterateNum, int threadNum, int threadsCount)
{
this.iterateNum = iterateNum;
this.threadNum = threadNum;
this.threadsCount = threadsCount;
}
}
public class WorkerResult
{
public WorkerData data;
public HttpWebResponse response;
public string error;
public WorkerResult(WorkerData data, HttpWebResponse response, string error)
{
this.data = data;
this.response = response;
this.error = error;
}
}
}