-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
547 lines (438 loc) · 19.2 KB
/
Program.cs
File metadata and controls
547 lines (438 loc) · 19.2 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
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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
using System;
using System.Collections.Generic;
using System.Net;
using System.IO;
using System.Diagnostics;
using System.Collections;
using System.Configuration;
using System.Threading;
using System.Linq;
using System.Text;
using Procurios.Public;
namespace DynPerfStat
{
class Program
{
static void Main(string[] args)
{
string home = Directory.GetCurrentDirectory();
//ServicePointManager.ServerCertificateValidationCallback += (sender, ICertificatePolicy, chain, sslPolicyErrors) => true;
ServicePointManager.ServerCertificateValidationCallback += delegate { return true; };
string ExeFriendlyName = System.AppDomain.CurrentDomain.FriendlyName;
string[] ExeNameBits = ExeFriendlyName.Split('.');
string ExeName = ExeNameBits[0];
bool debug = false;
bool proxy = false;
string Tmapservice = "";
string TMSName = "";
string Txy = "";
string Tx = "";
string Ty = "";
string TImageWidth = "";
string TImageHeight = "";
string TScaleDefault = "500000";
string[] TScales = TScaleDefault.Split(';');
string TDpi = "";
string TFormat = "";
string TNode = "";
string TThink = "";
string TLayers = "";
string TTokenURL = "";
string TLogsURL = "";
string TUser = "";
string TPassword = "";
int c = args.GetUpperBound(0);
// Loop through arguments
for (int n = 0; n < c; n++)
{
string thisKey = args[n].ToLower();
string thisVal = args[n + 1].TrimEnd().TrimStart();
// eval the key
switch (thisKey)
{
case "-xy":
Txy = thisVal;
string[] coord = thisVal.Split(';');
Tx = coord[0];
Ty = coord[1];
break;
case "-mapserver":
Tmapservice = thisVal;
break;
case "-scale":
TScales = thisVal.Split(';');
break;
case "-width":
TImageWidth = thisVal;
break;
case "-height":
TImageHeight = thisVal;
break;
case "-dpi":
TDpi = thisVal;
break;
case "-format":
TFormat = thisVal;
break;
case "-node":
TNode = thisVal;
break;
case "-layers":
TLayers = thisVal;
break;
case "-think":
TThink = thisVal;
break;
case "-debug":
string dbg = thisVal;
if (dbg.ToUpper() == "Y") debug = true;
break;
case "-proxy":
string prx = thisVal;
if (prx.ToUpper() == "Y") proxy = true;
break;
case "-user":
TUser = thisVal;
break;
case "-password":
TPassword = thisVal;
break;
case "-tokenurl":
TTokenURL = thisVal;
break;
case "-logsurl":
TLogsURL = thisVal;
break;
default:
break;
}
}
if (Txy == "") return;
if (Tmapservice == "") return;
string [] temp_ms = Tmapservice.Split('/');
TMSName = temp_ms[temp_ms.GetUpperBound(0) - 1];
double x = 0;
double y = 0;
int dpi = 96;
int ImageWidth = 800;
int ImageHeight = 400;
string Format = "png32";
string Node = "";
int Think = 0;
string layer_ids = "";
string Layers = "D";
int LayerCount = 0;
string SToken = "";
string PToken = "";
string mapservice = Tmapservice;
WebClient client = new WebClient();
client.Credentials = CredentialCache.DefaultCredentials;
if (proxy == true)
{
client.Proxy = WebRequest.DefaultWebProxy;
client.Proxy.Credentials = CredentialCache.DefaultCredentials;
}
if (TTokenURL != "" && TPassword != "" && TUser != "")
{
if (TTokenURL.ToLower().Contains("sharing"))
{
PToken = GetToken(TTokenURL, TUser, TPassword);
}
if (TTokenURL.ToLower().Contains("tokens"))
{
SToken = GetToken(TTokenURL, TUser, TPassword);
}
if (PToken.Contains("Token Error:") || SToken.Contains("Token Error:"))
{
Console.WriteLine(SToken);
Console.WriteLine(PToken);
Environment.Exit(-1);
}
if (PToken != "" && SToken == "")
{
SToken = GetFederatedToken(TTokenURL, PToken);
}
if (debug == true) Console.WriteLine("PToken:" + PToken);
if (debug == true) Console.WriteLine("SToken:" + SToken);
}
string json = "";
try
{
if (SToken != "")
{
json = client.DownloadString(new Uri(mapservice + "?f=json&token=" + SToken));
}
else
{
json = client.DownloadString(new Uri(mapservice + "?f=json"));
}
}
catch (WebException webEx)
{
Console.WriteLine(webEx.Message);
Environment.Exit(-1);
}
if (json.ToLower().Contains("error"))
{
Console.WriteLine(json);
Environment.Exit(-1);
}
if (TLayers != "") Layers = TLayers;
if (Layers != "D")
{
Hashtable root;
ArrayList layers;
root = (Hashtable)Procurios.Public.JSON.JsonDecode(json);
layers = (ArrayList)root["layers"];
LayerCount = layers.Count;
for (int n = 0; n < LayerCount; n++)
{
if (layer_ids != "") layer_ids = layer_ids + ",";
layer_ids = layer_ids + n.ToString();
}
}
if (layer_ids == "") layer_ids = "0";
if (Tx != "" && Ty != "")
{
x = Convert.ToDouble(Tx);
y = Convert.ToDouble(Ty);
}
if (TImageWidth != "") ImageWidth = Convert.ToInt32(TImageWidth);
if (TImageHeight != "") ImageHeight = Convert.ToInt32(TImageHeight);
if (TDpi != "") dpi = Convert.ToInt32(TDpi);
if (TFormat != "") Format = TFormat;
if (TNode != "") Node = TNode;
if (TThink != "") Think = Convert.ToInt32(TThink);
Console.WriteLine(mapservice + "\t" + TMSName);
if (debug == true) Console.WriteLine("DPI:" + dpi);
if (debug == true) Console.WriteLine("ImageFormat: " + Format);
if (debug == true) Console.WriteLine("ImageHeight: " + ImageWidth);
if (debug == true) Console.WriteLine("ImageWidth: " + ImageWidth);
if (debug == true) Console.WriteLine("");
Console.WriteLine("Scale\tTotal Time (ms)\tDownload Time (ms)\tApprox Service Time (ms)\tActual Service Time (ms)\tFile Size (KB)");
foreach (string TScale in TScales)
{
double scale = Convert.ToDouble(TScale);
/* dividing image width by DPI to get it in Inch */
double dblImgWidthInInch = ImageWidth / dpi;
double dblImgHeightInInch = ImageHeight / dpi;
/* converting Inch to meter (assume the map is in meter) */
double dblImgWidthInMapUnit = dblImgWidthInInch * 0.0254;
double dblImgHeightInMapUnit = dblImgHeightInInch * 0.0254;
/* calculating half of map’s height & width at the specific scale */
double dX = (dblImgWidthInMapUnit * scale) / 2;
double dY = (dblImgHeightInMapUnit * scale) / 2;
double XMin = x - dX;
double XMax = x + dX;
double YMin = y - dY;
double YMax = y + dY;
string imgurl = mapservice + "/export?";
imgurl = imgurl + "dpi=" + dpi.ToString();
imgurl = imgurl + "&size=" + ImageWidth.ToString() + "," + ImageHeight.ToString();
imgurl = imgurl + "&format=" + Format;
imgurl = imgurl + "&f=image";
imgurl = imgurl + "&bbox=" + XMin.ToString() + "," + YMin.ToString() + "," + XMax.ToString() + "," + YMax.ToString();
if (Layers == "A") imgurl = imgurl + "&layers=show:" + layer_ids;
if (SToken != "") imgurl = imgurl + "&token=" + SToken;
Stopwatch sw = Stopwatch.StartNew();
double seconds_download_time = 0;
Int64 bytesTotal = 0;
Double FileSize = 0;
try
{
Stream stream = client.OpenRead(new Uri(imgurl));
using (FileStream fileStream = new FileStream(home + "\\" + ExeName + Node + scale.ToString() + "." + GetImageExtension(Format.ToUpper()), FileMode.Create))
{
var buffer = new byte[32768];
int bytesRead;
Int64 bytesReadComplete = 0; // Use Int64 for files larger than 2 gb
// Get the size of the file to download
bytesTotal = Convert.ToInt64(client.ResponseHeaders["Content-Length"]);
Stopwatch sw2 = Stopwatch.StartNew();
// Download file in chunks
while ((bytesRead = stream.Read(buffer, 0, buffer.Length)) > 0)
{
bytesReadComplete += bytesRead;
fileStream.Write(buffer, 0, bytesRead);
}
sw2.Stop();
seconds_download_time = sw2.ElapsedMilliseconds;
}
if (debug == true) Console.WriteLine(imgurl + " OK");
}
catch (WebException webEx)
{
if (debug == true) Console.WriteLine(imgurl + " " + webEx.Message);
}
sw.Stop();
double seconds = sw.ElapsedMilliseconds;
try
{
//Check if the file is bigger than 1K
FileInfo FI = new FileInfo(home + "\\" + ExeName + Node + scale.ToString() + "." + GetImageExtension(Format.ToUpper()));
long FL = FI.Length;
FileSize = FI.Length;
FileSize = Math.Ceiling(FileSize / 1024);
if (FL < 1000)
{
//Probably timeout or error
Console.WriteLine(scale.ToString() + "\terror\terror\terror\terror\t" + FileSize.ToString() );
}
else
{
string actual_service_time = "NA";
if (TLogsURL != "")
{
string filter = "{\"codes\":[100004],\"processIds\":[],\"server\": \"*\",\"services\": [\"" + TMSName + ".MapServer" + "\"],\"machines\": \"*\"}";
if (PToken != "") actual_service_time = GetServiceTime(TLogsURL, filter, PToken, FL); //Use Portal Token
if (SToken != "" && PToken == "") actual_service_time = GetServiceTime(TLogsURL, filter, SToken, FL); //Use Server Token
if (actual_service_time != "error")
{
double dactual_service_time = Convert.ToDouble(actual_service_time);
dactual_service_time = dactual_service_time * 1000;
actual_service_time = dactual_service_time.ToString();
}
}
Console.WriteLine(scale.ToString() + "\t" + seconds.ToString() + "\t" + seconds_download_time.ToString() + "\t" + (seconds - seconds_download_time).ToString() + "\t" + actual_service_time + "\t" + FileSize.ToString());
}
}
catch
{
//Nothing
Console.WriteLine(scale.ToString() + "\tfailure\tfailure\tfailure\tfailure\t" + FileSize.ToString());
}
if (debug == true) Console.WriteLine("Waiting " + Think.ToString() + " milliseconds.");
Thread.Sleep(Think);
}
if (debug == true) Console.WriteLine("Done!");
}
public static string GetImageExtension(string imageFormat)
{
string result = "";
switch (imageFormat)
{
case "PNG":
case "PNG8":
case "PNG24":
case "PNG32":
result = "png";
break;
case "GIF":
result = "gif";
break;
case "JPEG":
case "JPG":
result = "jpg";
break;
default:
return "";
}
return result;
}
public static string GetToken(string tokenurl, string username, string password)
{
string url = "";
if (username == "IWA" && password == "IWA")
{
url = tokenurl + "generateToken?request=getToken&f=json";
}
else
{
url = tokenurl + "generateToken?request=getToken&f=json&username=" + username + "&password=" + password;
}
System.Net.WebRequest request = System.Net.WebRequest.Create(url);
request.Credentials = System.Net.CredentialCache.DefaultNetworkCredentials;
string myToken = "";
string myJSON = "";
try
{
System.Net.WebResponse response = request.GetResponse();
System.IO.Stream responseStream = response.GetResponseStream();
System.IO.StreamReader readStream = new System.IO.StreamReader(responseStream);
myJSON = readStream.ReadToEnd();
Hashtable LogRecord = (Hashtable)JSON.JsonDecode(myJSON);
myToken = (string)LogRecord["token"];
}
catch (WebException we)
{
myToken = "Token Error: " + we.Message;
}
return myToken;
}
public static string GetFederatedToken(string tokenurl, string token)
{
string url = tokenurl + "generateToken?request=getToken&f=json&token=" + token;
System.Net.WebRequest request = System.Net.WebRequest.Create(url);
request.Credentials = System.Net.CredentialCache.DefaultNetworkCredentials;
string myToken = "";
string myJSON = "";
try
{
System.Net.WebResponse response = request.GetResponse();
System.IO.Stream responseStream = response.GetResponseStream();
System.IO.StreamReader readStream = new System.IO.StreamReader(responseStream);
myJSON = readStream.ReadToEnd();
Hashtable LogRecord = (Hashtable)JSON.JsonDecode(myJSON);
myToken = (string)LogRecord["token"];
}
catch (WebException we)
{
myToken = "Token Error: " + we.Message;
}
return myToken;
}
public static string GetServiceTime(string logsurl, string filter, string token, double filesize)
{
string imgurl = logsurl + "/query?";
imgurl = imgurl + "startTime=";
imgurl = imgurl + "&endTime=";
imgurl = imgurl + "&level=FINE";
imgurl = imgurl + "&filterType=json";
imgurl = imgurl + "&filter=" + filter;
imgurl = imgurl + "&pageSize=100";
imgurl = imgurl + "&f=pjson";
if (token != "")
{
imgurl = imgurl + "&token=" + token;
}
string response = "";
WebClient client = new WebClient();
client.Credentials = System.Net.CredentialCache.DefaultNetworkCredentials;
try
{
response = client.DownloadString(new Uri(imgurl));
Hashtable root;
ArrayList LogRecords;
long LogRecordsCount = 0;
root = (Hashtable)Procurios.Public.JSON.JsonDecode(response);
LogRecords = (ArrayList)root["logMessages"];
if (LogRecords == null)
{
return "error";
}
LogRecordsCount = LogRecords.Count;
int n;
for (n = 0; n < LogRecordsCount; n++)
{
Hashtable LogRecord = (Hashtable)LogRecords[n];
string message = (string)LogRecord["message"];
string elapsed = (string)LogRecord["elapsed"];
string methodName = (string)LogRecord["methodName"];
if (message.Contains("Response size is") && methodName == "/export")
{
string reported_size;
int pos1 = message.IndexOf(" characters");
int pos2 = message.IndexOf("is ");
reported_size = message.Substring(pos2+3, pos1 - pos2 -3);
if (int.Parse(reported_size) < filesize + 10 && int.Parse(reported_size) > filesize - 10) return elapsed;
}
}
return "error";
}
catch (WebException webEx)
{
return webEx.Message;
}
}
}
}