-
Notifications
You must be signed in to change notification settings - Fork 0
/
TestDownload.cs
250 lines (227 loc) · 11.5 KB
/
TestDownload.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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
//--------------------------------------------------------------------------
// <summary>
//
// </summary>
// <copyright file="TestDownload.cs" company="Chuck Hill">
// Copyright (c) 2020 Chuck Hill.
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public License
// as published by the Free Software Foundation; either version 2.1
// of the License, or (at your option) any later version.
//
// This library 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 Lesser General Public License for more details.
//
// The GNU Lesser General Public License can be viewed at
// http://www.opensource.org/licenses/lgpl-license.php. If
// you unfamiliar with this license or have questions about
// it, here is an http://www.gnu.org/licenses/gpl-faq.html.
//
// All code and executables are provided "as is" with no warranty
// either express or implied. The author accepts no liability for
// any damage or loss of business that this product may cause.
// </copyright>
// <repository>https://github.com/ChuckHill2/DownloadiingTest</repository>
// <author>Chuck Hill</author>
//--------------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;
namespace DownloadingTest
{
public static class TestDownload
{
/// <summary>
/// One shot test for a specified URL
/// </summary>
/// <param name="url">url to download</param>
/// <param name="downloaderType">Which downloader to use.</param>
/// <param name="useNullFilename">True to just retrieve data into Job.Body</param>
public static void TestOne(string url, DownloaderType downloaderType, bool useNullFilename = false)
{
var job = useNullFilename ? new Job(url) : TestCommon.MakeJob(url);
if (job.Filename != null) FileEx.Delete(job.Filename);
HttpDownloader hdl = null;
WebDownloader wdl = null;
Func<Job, bool> download = null;
try
{
if (downloaderType == DownloaderType.Web)
{
wdl = new WebDownloader();
wdl.LogWriter = (s, f) => Log.Write(s, f);
download = j => wdl.DownloadAsync(j).Result;
}
else if (downloaderType == DownloaderType.Http)
{
hdl = new HttpDownloader(true);
hdl.LogWriter = (s, f) => Log.Write(s, f);
download = j => hdl.DownloadAsync(j).Result;
}
else if (downloaderType == DownloaderType.Sync)
{
Downloader.LogWriter = (s, f) => Log.Write(s, f);
download = j => Downloader.Download(j);
}
else if (downloaderType == DownloaderType.Legacy)
{
download = j => LegacyDownloader.Download(j);
}
int t1 = Environment.TickCount;
bool success = download(job);
var duration = (Environment.TickCount - t1) / 1000f;
Log.Write(Severity.Info, $"{job.Url} {(success ? "Successful" : "Failed")} {duration:F2} sec, {job.Url}");
}
catch (Exception ex)
{
Log.Write(Severity.Error, $"TestOne(\"{url}\", {downloaderType}Downloader, NullFile:{useNullFilename})\r\n{ex}");
}
finally
{
hdl?.Dispose();
wdl?.Dispose();
}
}
/// <summary>
/// Download many urls simultaneously.
/// </summary>
/// <param name="test">Which set of built-in urls to use.</param>
/// <param name="downloaderType">Which downloader to use.</param>
/// <param name="maxDegreeOfParallelism">Maximum #threads to use for concurrent downloads. Use 1 for serial downloads.</param>
/// <param name="urlCount">Download only a subset of built-in urls. Default is all.</param>
/// <param name="doRetry">Retry download urls that fail hardccoded conditions.</param>
public static void TestMany(DownloadTest test, DownloaderType downloaderType, int maxDegreeOfParallelism = int.MaxValue, int urlCount = int.MaxValue, bool doRetry = false)
{
IEnumerable<string> urls = TestCommon.GetUrls(test, urlCount);
Log.Write(Severity.None, $"******** {downloaderType} ********");
TestMany(urls, downloaderType, maxDegreeOfParallelism, doRetry);
}
/// <summary>
/// Download many urls simultaneously.
/// </summary>
/// <param name="urls">List of urls to download.</param>
/// <param name="downloaderType">Which downloader to use.</param>
/// <param name="maxDegreeOfParallelism">Maximum #threads to use for concurrent downloads. Use 1 for serial downloads.</param>
/// <param name="doRetry">Retry download urls that fail builtin conditions.</param>
public static void TestMany(IEnumerable<string> urls, DownloaderType downloaderType, int maxDegreeOfParallelism = int.MaxValue, bool doRetry = false)
{
List<Job> downloadJobs = null;
Func<Job, bool> download = null;
int jobNumber = 1; //job sequence order in order to match logging messages
downloadJobs = urls.Select(url => TestCommon.MakeJob(url, jobNumber++)).ToList();
List<float> durations = new List<float>(downloadJobs.Count);
List<Job> redownloadJobs = new List<Job>(Math.Max(4, downloadJobs.Count / 2)); //for doRetry==true
int Concurrency = 0; //number of active threads.
Console.WriteLine("Begin Downloading...");
HttpDownloader hdl = null;
WebDownloader wdl = null;
try
{
if (downloaderType == DownloaderType.Web)
{
wdl = new WebDownloader();
wdl.LogWriter = (s, f) => Log.Write(s, f); //enable logging
download = j => wdl.DownloadAsync(j).Result;
}
else if (downloaderType == DownloaderType.Http)
{
hdl = new HttpDownloader();
hdl.LogWriter = (s, f) => Log.Write(s, f); //enable logging
download = j => hdl.DownloadAsync(j).Result;
}
else if (downloaderType == DownloaderType.Sync)
{
Downloader.LogWriter = (s, f) => Log.Write(s, f); //enable logging
download = j => Downloader.Download(j);
}
else if (downloaderType == DownloaderType.Legacy)
{
download = j => LegacyDownloader.Download(j);
}
//With a value larger than 100 and >500 IMDB jobs, it slows down the IMDB server so much that it times out with TaskCanceledException.
var options = new ParallelOptions { MaxDegreeOfParallelism = maxDegreeOfParallelism };
int tt = Environment.TickCount;
while (downloadJobs.Count > 0)
{
Parallel.ForEach(downloadJobs, options, job =>
{
var concurrency = Interlocked.Increment(ref Concurrency);
int t1 = Environment.TickCount;
var success = download(job);
var duration = (Environment.TickCount - t1) / 1000f;
lock (durations) durations.Add(duration);
// Built-in Conditions: Retry Detection Example
// if (!success && job.FailureCount <= 3) //retry upon failure
// {
// if (job.Exception is HttpResponseExceptionDL) //only in HttpDownloader.DownloadAsync()
// {
// HttpStatusCode status = ((HttpResponseExceptionDL)job.Exception).Response.StatusCode;
// if (status == HttpStatusCode.Continue)
// {
// lock (redownloadJobs) redownloadJobs.Add(job);
// }
// }
// if (job.Exception is WebException webEx)
// {
// WebExceptionStatus status = webEx.Status;
//
// //Occurs when the web server is flooded with our requests due to multi-threading.
// if (status == WebExceptionStatus.RequestCanceled
// || status == WebExceptionStatus.Timeout
// || status == WebExceptionStatus.UnknownError)
// {
// lock (redownloadJobs) redownloadJobs.Add(job);
// }
// }
// if (job.Exception is TaskCanceledException) //if IMDB.com, may be recoverable when running within Parallel.ForEach()
// {
// lock (redownloadJobs) redownloadJobs.Add(job);
// }
// }
Log.Write(Severity.Info, $"{job.JobNumber:0000} {concurrency:0000} {(success ? "Successful" : "Failed")} {duration:F2} sec, {job.Url}");
Interlocked.Decrement(ref Concurrency);
});
if (!doRetry) break; //do not retry. We're done.
// Prepare for retry loop
downloadJobs.Clear();
var temp = downloadJobs;
downloadJobs = redownloadJobs;
redownloadJobs = temp;
if (downloadJobs.Count > 0) Log.Write(Severity.None, $"======== Retry ForEach({downloadJobs.Count}) ===========================================================================================");
}
var totalduration = (Environment.TickCount - tt) / 1000f;
durations.Sort();
Log.Write(Severity.Info, $"Summary " +
$"Count:{downloadJobs.Count} " +
$"Duration:{totalduration:F2} sec, " +
$"Sum:{durations.Sum():F2}, " +
$"Min:{durations[0]:F2}, " +
$"Max:{durations[downloadJobs.Count - 1]:F2}, " +
$"Range:{durations[downloadJobs.Count - 1] - durations[0]:F2}, " +
$"Ave:{durations.Average():F2}, " +
$"Median:{durations[downloadJobs.Count / 2]:F2}"); //not accurate but close enough.
}
catch (Exception ex)
{
Log.Write(Severity.Error, "Unexpected error occurred: " + ex);
Console.WriteLine("Unexpected error occurred: "+ex.Message);
}
finally
{
hdl?.Dispose();
wdl?.Dispose();
}
Console.WriteLine($"Downloading Complete.");
}
}
}