forked from bonesoul/concurrency-tests
/
Program.cs
229 lines (187 loc) · 9.54 KB
/
Program.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
/*
* Copyright (C) 2011, Hüseyin Uslu - shalafiraistlin@gmail.com
*
* This program is free software: you can redistribute it and/or modify it under the terms of the GNU General
* Public License as published by the Free Software Foundation, either version 3 of the License, or (at your
* option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the
* implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* for more details.
*
* You should have received a copy of the GNU General Public License along with this program. If not, see
* <http://www.gnu.org/licenses/>.
*
*/
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Management;
using System.Threading;
using System.Threading.Tasks;
namespace ConcurrencyTests
{
class Program
{
static void Main(string[] args)
{
string[] feeds = {
"http://www.teamliquid.net/rss/news.xml",
"http://www.diablofans.com/rss/forums/1-diablo-fans-home-page-news/",
"http://feeds.feedburner.com/blizzplanetcom",
"http://www.tehgladiators.com/rss.xml",
"http://www.mmo-champion.com/external.php?do=rss&type=newcontent§ionid=1&days=120&count=10",
"http://wow.joystiq.com/rss.xml",
"http://www.wowhead.com/blog&rss",
"http://www.tentonhammer.com/wow/all/feed",
"http://feeds.feedburner.com/LookingForGroup",
"http://www.d3sanc.com/feed/"
};
const int iterationCount = 10;
var timeSpanSequentials = new TimeSpan[iterationCount];
var timeSpanParallelForEachs = new TimeSpan[iterationCount];
var timeSpanTPLs = new TimeSpan[iterationCount];
var timeSpanThreadPools = new TimeSpan[iterationCount];
//PrintCPUInfo();
Console.WriteLine("Will be parsing a total of {0} feeds.", feeds.Length);
Console.WriteLine("________________________________________________________________________________");
Console.WriteLine("Itr.\tSeq.\tPrlEx\tTPL\tTPool");
Console.WriteLine("________________________________________________________________________________");
for (int i = 0; i < iterationCount; i++)
{
Console.Write("#{0}\t", i + 1);
TimeSpan timeSpanSequential = MeasureSequential(feeds);
timeSpanSequentials[i] = timeSpanSequential;
Console.Write(String.Format("{0:00}.{1:00}s\t",timeSpanSequential.Seconds, timeSpanSequential.Milliseconds / 10));
TimeSpan timeSpanParallelForeach = MeasureParallelForeach(feeds);
timeSpanParallelForEachs[i] = timeSpanParallelForeach;
Console.Write(String.Format("{0:00}.{1:00}s\t", timeSpanParallelForeach.Seconds, timeSpanParallelForeach.Milliseconds / 10));
TimeSpan timeSpanTPL = MeasureTPL(feeds);
timeSpanTPLs[i] = timeSpanTPL;
Console.Write(String.Format("{0:00}.{1:00}s\t", timeSpanTPL.Seconds, timeSpanTPL.Milliseconds / 10));
TimeSpan timeSpanThreadPool = MeasureNativeThreadPool(feeds);
timeSpanThreadPools[i] = timeSpanThreadPool;
Console.Write(String.Format("{0:00}.{1:00}s\t\n", timeSpanThreadPool.Seconds, timeSpanThreadPool.Milliseconds / 10));
}
// print the averages also
Console.WriteLine("\n________________________________________________________________________________");
Console.Write("Avg.\t");
TimeSpan timeSpanSequentialAverage = CalculateAverageTimeSpan(timeSpanSequentials);
Console.Write(String.Format("{0:00}.{1:00}s\t", timeSpanSequentialAverage.Seconds, timeSpanSequentialAverage.Milliseconds / 10));
TimeSpan timeSpanParallelForeachAverage = CalculateAverageTimeSpan(timeSpanParallelForEachs);
Console.Write(String.Format("{0:00}.{1:00}s\t", timeSpanParallelForeachAverage.Seconds, timeSpanParallelForeachAverage.Milliseconds / 10));
TimeSpan timeSpanTPLAverage = CalculateAverageTimeSpan(timeSpanTPLs);
Console.Write(String.Format("{0:00}.{1:00}s\t", timeSpanTPLAverage.Seconds, timeSpanTPLAverage.Milliseconds / 10));
TimeSpan timeSpanThreadPoolAvarage = CalculateAverageTimeSpan(timeSpanThreadPools);
Console.Write(String.Format("{0:00}.{1:00}s\t", timeSpanThreadPoolAvarage.Seconds, timeSpanThreadPoolAvarage.Milliseconds / 10));
Console.WriteLine("\n________________________________________________________________________________");
Console.ReadLine();
}
static TimeSpan MeasureSequential(IEnumerable<string> feedSources)
{
var stopwatch = new Stopwatch();
stopwatch.Start();
foreach (var f in feedSources.Select(feedSource => new FeedParser(feedSource))) { }
stopwatch.Stop();
return stopwatch.Elapsed;
}
static TimeSpan MeasureParallelForeach(IEnumerable<string> feedSources)
{
var stopwatch = new Stopwatch();
stopwatch.Start();
Parallel.ForEach(feedSources, feedSource =>
{
var f = new FeedParser(feedSource);
}
);
stopwatch.Stop();
return stopwatch.Elapsed;
}
static TimeSpan MeasureTPL(IList<string> feedSources)
{
var stopwatch = new Stopwatch();
stopwatch.Start();
var tasks = new Task[feedSources.Count];
for (int i = 0; i < feedSources.Count;i++ )
{
string source = feedSources[i]; /* work-around modified closures */
tasks[i] = Task.Factory.StartNew(() => TPLFeedParserTask(source));
}
Task.WaitAll(tasks);
stopwatch.Stop();
return stopwatch.Elapsed;
}
static void TPLFeedParserTask(string feedSource)
{
var f = new FeedParser(feedSource);
}
static TimeSpan MeasureNativeThreadPool(string[] feedSources)
{
var stopwatch = new Stopwatch();
stopwatch.Start();
var threadPoolDoneEvents = new ManualResetEvent[feedSources.Length];
for (int i = 0; i < feedSources.Length; i++)
{
threadPoolDoneEvents[i] = new ManualResetEvent(false);
var wrapper = new ThreadPoolDataWrapper(threadPoolDoneEvents[i], feedSources[i]);
ThreadPool.QueueUserWorkItem(ThreadPoolTask, wrapper);
}
WaitHandle.WaitAll(threadPoolDoneEvents);
stopwatch.Stop();
return stopwatch.Elapsed;
}
static void ThreadPoolTask(object data)
{
var f = new FeedParser(((ThreadPoolDataWrapper) data).FeedSource);
(data as ThreadPoolDataWrapper).ResetEvent.Set();
}
static TimeSpan CalculateAverageTimeSpan(TimeSpan[] timeSpans)
{
double miliseconds = timeSpans.Sum(t => t.TotalMilliseconds) / timeSpans.Length;
return TimeSpan.FromMilliseconds(miliseconds);
}
static void PrintCPUInfo()
{
/* code taken from: http://stackoverflow.com/questions/1542213/how-to-find-the-number-of-cpu-cores-via-net-c/2670568#2670568 */
int physicalCpuCount = 0;
int coreCount = 0;
bool coreCountSupported;
int logicalCpuCount = 0;
bool logicalCpuCountSupported;
foreach (var item in new ManagementObjectSearcher("Select * from Win32_ComputerSystem").Get())
{
physicalCpuCount = Int32.Parse(item["NumberOfProcessors"].ToString());
}
try
{
foreach (var item in new ManagementObjectSearcher("Select * from Win32_Processor").Get())
{
coreCount += int.Parse(item["NumberOfCores"].ToString());
}
coreCountSupported=true;
}
catch(Exception) { coreCountSupported = false; }
try
{
foreach (var item in new ManagementObjectSearcher("Select * from Win32_ComputerSystem").Get())
{
logicalCpuCount = Int32.Parse(item["NumberOfLogicalProcessors"].ToString());
}
logicalCpuCountSupported = true;
}
catch(Exception) { logicalCpuCountSupported = false; }
Console.WriteLine("Test Environment: {0} physical cpus, {1} cores, {2} logical cpus.", physicalCpuCount, coreCountSupported ? coreCount.ToString() : "NotSupported", logicalCpuCountSupported ? logicalCpuCount.ToString() : "NotSupported");
}
}
class ThreadPoolDataWrapper
{
public ManualResetEvent ResetEvent { get; set; }
public string FeedSource { get; private set; }
public ThreadPoolDataWrapper(ManualResetEvent @event, string feedSource)
{
this.ResetEvent = @event;
this.FeedSource = feedSource;
}
}
}