private async Task Init(string[] args) { if (args.Length < 4) { MainConsole.WriteLine("Please restart the program with arguments like \r\n ..exe [threadsize] [requestcount] [requestsize] [targeturl]\r\n This program uploads data in json form, in future updates this can be changed with parameter", RequestSizeInMb); } else { int threadsize = int.Parse(args[0]); int requestcount = int.Parse(args[1]); double requestSize = double.Parse(args[2]); string requestUrl = args[3]; g = new RequestGenerator(requestSize == 0 ? 10 : requestSize); //do requests with 10mb in size g.OnRequestCompleted += G_OnRequestCompleted; g.OnRequestCreated += G_OnRequestCreated; MainConsole.WriteLine($"Generating random data with size {requestSize}mb."); CustomStream data = await g.GenerateData(); MainConsole.WriteLine($"Starting request service with {threadsize} threads and {requestcount} cycles on {requestUrl}"); await g.Start(data, threadsize, requestcount, requestUrl); } }
public async Task Start(CustomStream data, int threadsize, int requestcount, string requestUrl) { Client = new RestClient(requestUrl); Client.Timeout = -1; Parallel.For(1, requestcount, new ParallelOptions { MaxDegreeOfParallelism = threadsize }, delegate(int i) { PerformRequest(data, i); }); }
private void PerformRequest(CustomStream data, int index) { using (MemoryStream s1 = data.GetStream()) { OnRequestCreated?.Invoke($"starting request {index} with datalength: {data.Length}"); RestRequest request = new RestRequest(Method.POST); request.AddHeader("Content-Type", "application/json"); request.Files.Add(new FileParameter { Name = "bestandje", Writer = (s) => { s1.CopyTo(s); }, FileName = "kankerscammers.jpg", ContentLength = data.Length }); DateTime begin = DateTime.Now; IRestResponse response = Client.Execute(request); DateTime end = DateTime.Now; TimeSpan span = end - begin; string time = $" with time {span.TotalMilliseconds}ms"; if (response == null) { OnRequestCompleted?.Invoke("Request " + index + " completed with no result" + time); } else if (response.StatusCode == System.Net.HttpStatusCode.OK) { OnRequestCompleted?.Invoke("Request " + index + " completed OK" + time); } else if (response.StatusCode == System.Net.HttpStatusCode.InternalServerError) { OnRequestCompleted?.Invoke("Request " + index + " completed with internal server error" + time); } else { OnRequestCompleted?.Invoke("Request " + index + " completed with other result: " + response.StatusCode + time); } response = null; request = null; data = null; s1.Dispose(); GC.Collect(); } }