public void DoWork() { while (HasNextReport) { Console.Out.Write($"{threadNum}."); PerfCounters pc = RunOneReport(cmdLine, false); perfCounters.Add(pc); } }
static async Task Main(string[] args) { // get connected string url = ConfigurationManager.AppSettings["url"]; if ((!url.EndsWith("/")) && (!url.EndsWith("\\"))) { url += "/"; } Console.Out.WriteLine($"Connecting to URL {url}"); client = new WindwardClient(new Uri(url)); VersionInfo version = await client.GetVersion(); Console.Out.WriteLine($"REST server version = {version}"); // if no arguments, then we list out the usage. if (args.Length < 2) { DisplayUsage(); return; } // the try here is so we can print out an exception if it is thrown. This code does minimal error checking and no other // exception handling to keep it simple & clear. try { Console.Out.WriteLine("Running in {0}-bit mode", IntPtr.Size * 8); // parse the arguments passed in. This method makes no calls to Windward, it merely organizes the passed in arguments. CommandLine cmdLine = CommandLine.Factory(args); // This turns on log4net logging. You can also call log4net.Config.XmlConfigurator.Configure(); directly. // If you do not make this call, then there will be no logging (which you may want off). var logRepository = LogManager.GetRepository(Assembly.GetEntryAssembly()); XmlConfigurator.Configure(logRepository, new FileInfo("RunReport.config")); logWriter.Info($"Starting RunReport ({string.Join(", ", args)})"); DateTime startTime = DateTime.Now; // run one report if (!cmdLine.IsPerformance) { PerfCounters perfCounters = await RunOneReport(cmdLine, args.Length == 2); PrintPerformanceCounter(startTime, perfCounters, false); } else { string dirReports = Path.GetDirectoryName(Path.GetFullPath(cmdLine.ReportFilename)) ?? ""; if (!Directory.Exists(dirReports)) { Console.Error.WriteLine($"The directory {dirReports} does not exist"); return; } // drop out threads - twice the number of cores. int numThreads = cmdLine.NumThreads; numReportsRemaining = cmdLine.NumReports; ReportWorker[] workers = new ReportWorker[numThreads]; for (int ind = 0; ind < numThreads; ind++) { workers[ind] = new ReportWorker(ind, new CommandLine(cmdLine)); } Task[] threads = new Task[numThreads]; for (int ind = 0; ind < numThreads; ind++) { threads[ind] = workers[ind].DoWork(); } Console.Out.WriteLine($"Start time: {startTime.ToLongTimeString()}, {numThreads} threads, {cmdLine.NumReports} reports"); Console.Out.WriteLine(); for (int ind = 0; ind < numThreads; ind++) { threads[ind].Start(); } // we wait await Task.WhenAll(threads); PerfCounters perfCounters = new PerfCounters(); for (int ind = 0; ind < numThreads; ind++) { perfCounters.Add(workers[ind].perfCounters); } Console.Out.WriteLine(); PrintPerformanceCounter(startTime, perfCounters, true); } } catch (Exception ex) { logWriter.Error("RunReport", ex); string indent = " "; Console.Error.WriteLine(); Console.Error.WriteLine("Error(s) running the report"); while (ex != null) { Console.Error.WriteLine($"{indent}Error: {ex.Message} ({ex.GetType().FullName})\n{indent} stack: {ex.StackTrace}\n"); ex = ex.InnerException; indent += " "; } throw; } }