public Post ( string url, string body, string contentType, Action |
||
url | string | |
body | string | |
contentType | string | |
onSuccess | Action |
|
onException | Action |
|
return | void |
public bool Execute(CommandProcessorContext context, string[] args) { var eventStreamId = "test-stream"; var expectedVersion = ExpectedVersion.Any; string metadata = "{'user' : 'test'}"; var requireMaster = false; var data = "{'a' : 3 }"; if (args.Length > 0) { if (args.Length != 5) return false; eventStreamId = args[0]; expectedVersion = args[1].ToUpper() == "ANY" ? ExpectedVersion.Any : int.Parse(args[1]); data = args[2]; metadata = args[3]; requireMaster = bool.Parse(args[4]); } context.IsAsync(); var client = new HttpAsyncClient(); var url = context.Client.HttpEndpoint.ToHttpUrl("/streams/{0}", eventStreamId); context.Log.Info("Writing to {0}...", url); var msg = "[{'eventType': 'fooevent', 'eventId' : '" + Guid.NewGuid() + "'" + ",'data' : " + data + ", 'metadata' : " + metadata + "}]"; var sw = Stopwatch.StartNew(); client.Post( url, msg, Codec.Json.ContentType, new Dictionary<string, string> { {SystemHeaders.ExpectedVersion, expectedVersion.ToString()}, {SystemHeaders.RequireMaster, requireMaster ? "True" : "False"} }, TimeSpan.FromMilliseconds(10000), response => { sw.Stop(); if (response.HttpStatusCode == HttpStatusCode.Created) context.Log.Info("Successfully written"); else { context.Log.Info("Error while writing: [{0}] - [{1}]", response.HttpStatusCode, response.StatusDescription); context.Log.Info("Response:\n{0}\n\n{1}\n", string.Join("\n", response.Headers.AllKeys.Select(x => string.Format("{0,-20}: {1}", x, response.Headers[x]))), response.Body); } context.Log.Info("Write request took: {0}.", sw.Elapsed); context.Success(); }, e => { context.Log.ErrorException(e, "Error during POST"); context.Fail(e, "Error during POST."); }); return true; }
public void of_http_requests_routing() { const int iterations = 100000; IPublisher inputBus = new NoopPublisher(); var bus = InMemoryBus.CreateTest(); var queue = new QueuedHandlerThreadPool(bus, "Test", true, TimeSpan.FromMilliseconds(50)); var multiQueuedHandler = new MultiQueuedHandler(new IQueuedHandler[]{queue}, null); var providers = new AuthenticationProvider[] {new AnonymousAuthenticationProvider()}; var httpService = new HttpService(ServiceAccessibility.Public, inputBus, new TrieUriRouter(), multiQueuedHandler, "http://localhost:12345/"); HttpService.CreateAndSubscribePipeline(bus, providers); var fakeController = new FakeController(iterations, null); httpService.SetupController(fakeController); httpService.Handle(new SystemMessage.SystemInit()); var rnd = new Random(); var sw = Stopwatch.StartNew(); var httpClient = new HttpAsyncClient(); for (int i = 0; i < iterations; ++i) { var route = fakeController.BoundRoutes[rnd.Next(0, fakeController.BoundRoutes.Count)]; switch (route.Item2) { case HttpMethod.Get: httpClient.Get(route.Item1,TimeSpan.FromMilliseconds(10000), x => { }, x => { throw new Exception();}); break; case HttpMethod.Post: httpClient.Post(route.Item1, "abracadabra", ContentType.Json, TimeSpan.FromMilliseconds(10000), x => { }, x => { throw new Exception(); }); break; case HttpMethod.Delete: httpClient.Delete(route.Item1, TimeSpan.FromMilliseconds(10000), x => { }, x => { throw new Exception(); }); break; case HttpMethod.Put: httpClient.Put(route.Item1, "abracadabra", ContentType.Json, TimeSpan.FromMilliseconds(10000), x => { }, x => { throw new Exception(); }); break; default: throw new Exception(); } } fakeController.CountdownEvent.Wait(); sw.Stop(); Console.WriteLine("{0} request done in {1} ({2:0.00} per sec)", iterations, sw.Elapsed, 1000.0 * iterations / sw.ElapsedMilliseconds); httpService.Shutdown(); multiQueuedHandler.Stop(); }
public bool Execute(CommandProcessorContext context, string[] args) { var eventStreamId = "test-stream"; var expectedVersion = ExpectedVersion.Any; var data = "test-data"; string metadata = null; if (args.Length > 0) { if (args.Length < 3 || args.Length > 4) return false; eventStreamId = args[0]; expectedVersion = args[1].ToUpper() == "ANY" ? ExpectedVersion.Any : int.Parse(args[1]); data = args[2]; if (args.Length == 4) metadata = args[3]; } context.IsAsync(); var client = new HttpAsyncClient(); var url = context.Client.HttpEndpoint.ToHttpUrl("/streams/{0}", eventStreamId); context.Log.Info("Writing to {0}...", url); var request = Codec.Xml.To(new HttpClientMessageDto.WriteEventsText( expectedVersion, new[] { new HttpClientMessageDto.ClientEventText(Guid.NewGuid(), "type", data, metadata) })); var sw = Stopwatch.StartNew(); client.Post( url, request, Codec.Xml.ContentType, response => { sw.Stop(); if (response.HttpStatusCode == HttpStatusCode.Created) context.Log.Info("Successfully written"); else context.Log.Info("Error while writing: [{0}] - [{1}]", response.HttpStatusCode, response.StatusDescription); context.Log.Info("Write request took: {0}.", sw.Elapsed); context.Success(); }, e => { context.Log.ErrorException(e, "Error during POST"); context.Fail(e, "Error during POST."); }); return true; }
private void Flood(CommandProcessorContext context, string eventStreamId, int clientsCnt, int minPerSecond, int maxPerSecond, int runTimeMinutes, int dataSize) { context.IsAsync(); var threads = new List<Thread>(); var doneEvent = new ManualResetEvent(false); var succ = 0; var fail = 0; var requestsCnt = 0; int sent = 0; int received = 0; var watchLockRoot = new object(); var sw = Stopwatch.StartNew(); for (int i = 0; i < clientsCnt; i++) { threads.Add(new Thread(() => { var esId = eventStreamId ?? "Stream-" + Thread.CurrentThread.ManagedThreadId % 3; var client = new HttpAsyncClient(); Action<HttpResponse> succHandler = response => { if (response.HttpStatusCode == HttpStatusCode.Created) { var succDone = Interlocked.Increment(ref succ); if (succDone % maxPerSecond == 0) Console.Write("."); } else { if (Interlocked.Increment(ref fail) % 10 == 0) { context.Log.Info("ANOTHER 10th WRITE FAILED. [{0}] - [{1}]", response.HttpStatusCode, response.StatusDescription); } } Interlocked.Increment(ref requestsCnt); Interlocked.Increment(ref received); }; var sentCount = 0; var sleepTime = 0; var dataSizeCoefficient = 1; var currentMinute = -1; while (true) { TimeSpan elapsed; lock (watchLockRoot) elapsed = sw.Elapsed; if (elapsed.TotalMinutes > runTimeMinutes) { doneEvent.Set(); break; } if (sentCount == 0) { int elapsedMinutesInt = (int)elapsed.TotalMinutes; lock (_randomLockRoot) { sentCount = minPerSecond == maxPerSecond ? maxPerSecond : _random.Next(minPerSecond, maxPerSecond); dataSizeCoefficient = _random.Next(8, 256); } if (currentMinute != elapsedMinutesInt) { currentMinute = elapsedMinutesInt; context.Log.Info("\nElapsed {0} of {1} minutes, sent {2}; next block coef. {3}", elapsedMinutesInt, runTimeMinutes, sent, dataSizeCoefficient); } sleepTime = 1000 / sentCount; } var url = context.Client.HttpEndpoint.ToHttpUrl("/streams/{0}", esId); var dataResultingSize = dataSizeCoefficient * dataSize; var write = new[] { new HttpClientMessageDto.ClientEventText(Guid.NewGuid(), "type", "DATA" + dataResultingSize.ToString(" 00000 ") + new string('*', dataResultingSize), "METADATA" + new string('$', 100))}; var request = Codec.Xml.To(write); client.Post(url, request, Codec.Xml.ContentType, TimeSpan.FromMilliseconds(10000), succHandler, exc => { Interlocked.Increment(ref fail); Interlocked.Increment(ref requestsCnt); context.Log.ErrorException(exc, "Error during POST."); }); Interlocked.Increment(ref sent); Thread.Sleep(sleepTime); sentCount -= 1; while (sent - received > context.Client.Options.WriteWindow/clientsCnt) { Thread.Sleep(1); } } })); } foreach (var thread in threads) { thread.IsBackground = true; thread.Start(); } doneEvent.WaitOne(); sw.Stop(); context.Log.Info("Completed. Successes: {0}, failures: {1}", succ, fail); var reqPerSec = (requestsCnt + 0.0)/sw.ElapsedMilliseconds*1000; context.Log.Info("{0} requests completed in {1}ms ({2:0.00} reqs per sec).", requestsCnt, sw.ElapsedMilliseconds, reqPerSec); PerfUtils.LogData( Keyword, PerfUtils.Row(PerfUtils.Col("clientsCnt", clientsCnt), PerfUtils.Col("requestsCnt", requestsCnt), PerfUtils.Col("ElapsedMilliseconds", sw.ElapsedMilliseconds)), PerfUtils.Row(PerfUtils.Col("successes", succ), PerfUtils.Col("failures", fail)) ); PerfUtils.LogTeamCityGraphData(string.Format("{0}-{1}-{2}-{3}-{4}-reqPerSec", Keyword, clientsCnt, minPerSecond, maxPerSecond, runTimeMinutes), (int)reqPerSec); PerfUtils.LogTeamCityGraphData( string.Format("{0}-{1}-{2}-failureSuccessRate", Keyword, clientsCnt, requestsCnt), 100 * fail / (fail + succ)); context.Success(); }
private void WriteFlood(CommandProcessorContext context, string eventStreamId, int clientsCnt, long requestsCnt) { context.IsAsync(); var threads = new List<Thread>(); var doneEvent = new ManualResetEventSlim(false); long all = 0; long succ = 0; long fail = 0; var sw = new Stopwatch(); var sw2 = new Stopwatch(); for (int i = 0; i < clientsCnt; i++) { var count = requestsCnt / clientsCnt + ((i == clientsCnt - 1) ? requestsCnt % clientsCnt : 0); long sent = 0; long received = 0; threads.Add(new Thread(() => { var esId = eventStreamId ?? ("es" + Guid.NewGuid()); var client = new HttpAsyncClient(); Action onReceived = () => { Interlocked.Increment(ref received); var localAll = Interlocked.Increment(ref all); if (localAll % 10000 == 0) { var elapsed = sw2.Elapsed; sw2.Restart(); context.Log.Trace("\nDONE TOTAL {0} WRITES IN {1} ({2:0.0}/s).", localAll, elapsed, 1000.0 * 10000 / elapsed.TotalMilliseconds); } if (localAll == requestsCnt) doneEvent.Set(); }; Action<HttpResponse> onSuccess = response => { if (response.HttpStatusCode == HttpStatusCode.Created) { if (Interlocked.Increment(ref succ) % 100 == 0) Console.Write('.'); } else { var localFail = Interlocked.Increment(ref fail); if (localFail % 100 == 0) Console.Write('#'); if (localFail % 10 == 0) context.Log.Info("ANOTHER 10th WRITE FAILED. [{0}] - [{1}]", response.HttpStatusCode, response.StatusDescription); } onReceived(); }; Action<Exception> onException = exc => { context.Log.ErrorException(exc, "Error during POST."); if (Interlocked.Increment(ref fail) % 100 == 0) Console.Write('#'); onReceived(); }; for (int j = 0; j < count; ++j) { var url = context.Client.HttpEndpoint.ToHttpUrl("/streams/{0}", esId); var write = new[] { new HttpClientMessageDto.ClientEventText(Guid.NewGuid(), "type", "DATA" + new string('*', 256), "METADATA" + new string('$', 100)) }; var requestString = Codec.Json.To(write); client.Post( url, requestString, Codec.Json.ContentType, new Dictionary<string, string> { }, TimeSpan.FromMilliseconds(10000), onSuccess, onException); var localSent = Interlocked.Increment(ref sent); while (localSent - Interlocked.Read(ref received) > context.Client.Options.WriteWindow/clientsCnt) { Thread.Sleep(1); } } }) { IsBackground = true }); } sw.Start(); sw2.Start(); threads.ForEach(thread => thread.Start()); doneEvent.Wait(); sw.Stop(); var reqPerSec = (all + 0.0) / sw.ElapsedMilliseconds * 1000; context.Log.Info("Completed. Successes: {0}, failures: {1}", Interlocked.Read(ref succ), Interlocked.Read(ref fail)); context.Log.Info("{0} requests completed in {1}ms ({2:0.00} reqs per sec).", all, sw.ElapsedMilliseconds, reqPerSec); PerfUtils.LogData(Keyword, PerfUtils.Row(PerfUtils.Col("clientsCnt", clientsCnt), PerfUtils.Col("requestsCnt", requestsCnt), PerfUtils.Col("ElapsedMilliseconds", sw.ElapsedMilliseconds)), PerfUtils.Row(PerfUtils.Col("successes", succ), PerfUtils.Col("failures", fail))); PerfUtils.LogTeamCityGraphData(string.Format("{0}-{1}-{2}-reqPerSec", Keyword, clientsCnt, requestsCnt), (int)reqPerSec); PerfUtils.LogTeamCityGraphData(string.Format("{0}-{1}-{2}-failureSuccessRate", Keyword, clientsCnt, requestsCnt), (int)(100.0*fail/(fail + succ))); if (Interlocked.Read(ref succ) != requestsCnt) context.Fail(reason: "There were errors or not all requests completed."); else context.Success(); }
private void WriteFlood(CommandProcessorContext context, int clientsCnt, int requestsCnt) { context.IsAsync(); var threads = new List<Thread>(); var doneEvent = new ManualResetEventSlim(false); var succ = 0; var fail = 0; var all = 0; var sw = Stopwatch.StartNew(); for (int i = 0; i < clientsCnt; i++) { var count = requestsCnt / clientsCnt + ((i == clientsCnt - 1) ? requestsCnt % clientsCnt : 0); threads.Add(new Thread(() => { var autoEvent = new AutoResetEvent(false); var eventStreamId = "es" + Guid.NewGuid(); var client = new HttpAsyncClient(); var url = context.Client.HttpEndpoint.ToHttpUrl("/streams/{0}", eventStreamId); Action<HttpResponse> succHandler = response => { if (response.HttpStatusCode == HttpStatusCode.Created) { if (Interlocked.Increment(ref succ)%1000 == 0) Console.Write("."); } else { if (Interlocked.Increment(ref fail)%10 == 0) context.Log.Info("ANOTHER 10th WRITE FAILED. [{0}] - [{1}]", response.HttpStatusCode, response.StatusDescription); } if (Interlocked.Increment(ref all) == requestsCnt) doneEvent.Set(); autoEvent.Set(); }; for (int j = 0; j < count; ++j) { var write = new[] { new HttpClientMessageDto.ClientEventText(Guid.NewGuid(), "type", "DATA" + new string('*', 256), "METADATA" + new string('$', 100))}; var request = Codec.Xml.To(write); client.Post(url, request, Codec.Xml.ContentType, TimeSpan.FromMilliseconds(10000), succHandler, exc => { context.Log.ErrorException(exc, "Error during POST."); Interlocked.Increment(ref fail); if (Interlocked.Increment(ref all) == requestsCnt) doneEvent.Set(); autoEvent.Set(); }); autoEvent.WaitOne(); } }) { IsBackground = true }); } threads.ForEach(thread => thread.Start()); doneEvent.Wait(); sw.Stop(); var reqPerSec = (all + 0.0)/sw.ElapsedMilliseconds*1000; context.Log.Info("Completed. Successes: {0}, failures: {1}", succ, fail); context.Log.Info("{0} requests completed in {1}ms ({2:0.00} reqs per sec).", all, sw.ElapsedMilliseconds, reqPerSec); PerfUtils.LogData(Keyword, PerfUtils.Row(PerfUtils.Col("clientsCnt", clientsCnt), PerfUtils.Col("requestsCnt", requestsCnt), PerfUtils.Col("ElapsedMilliseconds", sw.ElapsedMilliseconds)), PerfUtils.Row(PerfUtils.Col("successes", succ), PerfUtils.Col("failures", fail))); PerfUtils.LogTeamCityGraphData(string.Format("{0}-{1}-{2}-reqPerSec", Keyword, clientsCnt, requestsCnt), (int) reqPerSec); PerfUtils.LogTeamCityGraphData(string.Format("{0}-{1}-{2}-failureSuccessRate", Keyword, clientsCnt, requestsCnt), (int)(100.0*fail/(fail + succ))); PerfUtils.LogTeamCityGraphData(string.Format("{0}-latency-ms", Keyword), (int)(sw.ElapsedMilliseconds / requestsCnt)); if (succ != requestsCnt) context.Fail(reason: "There were errors or not all requests completed."); else context.Success(); }
private void WriteFlood(CommandProcessorContext context, string eventStreamId, int clientsCnt, int requestsCnt) { context.IsAsync(); var threads = new List<Thread>(); var autoResetEvent = new AutoResetEvent(false); var succ = 0; var fail = 0; var all = 0; var sw = Stopwatch.StartNew(); for (int i = 0; i < clientsCnt; i++) { var count = requestsCnt / clientsCnt + ((i == clientsCnt - 1) ? requestsCnt % clientsCnt : 0); int sent = 0; int received = 0; threads.Add(new Thread(() => { var esId = eventStreamId ?? "es" + Guid.NewGuid(); var client = new HttpAsyncClient(); Action<HttpResponse> succHandler = response => { if (response.HttpStatusCode == HttpStatusCode.Created) { if (Interlocked.Increment(ref succ) % 100 == 0) Console.Write("."); } else { if (Interlocked.Increment(ref fail) % 10 == 0) { context.Log.Info("ANOTHER 10th WRITE FAILED. [{0}] - [{1}]", response.HttpStatusCode, response.StatusDescription); } } Interlocked.Increment(ref received); if (Interlocked.Increment(ref all) == requestsCnt) autoResetEvent.Set(); }; for (int j = 0; j < count; ++j) { var url = context.Client.HttpEndpoint.ToHttpUrl("/streams/{0}", esId); var write = new ClientMessageDto.WriteEventText( Guid.Empty, ExpectedVersion.Any, new[] { new ClientMessageDto.EventText(Guid.NewGuid(), "type", "DATA" + new string('*', 256), "METADATA" + new string('$', 100)) }); var requestString = Codec.Xml.To(write); client.Post(url, requestString, Codec.Xml.ContentType, succHandler, exc => { context.Log.ErrorException(exc, "Error during POST."); Interlocked.Increment(ref fail); if (Interlocked.Increment(ref all) == requestsCnt) autoResetEvent.Set(); }); Interlocked.Increment(ref sent); while (sent - received > context.Client.Options.WriteWindow) Thread.Sleep(1); } })); } foreach (var thread in threads) { thread.IsBackground = true; thread.Start(); } autoResetEvent.WaitOne(); sw.Stop(); context.Log.Info("Completed. Successes: {0}, failures: {1}", succ, fail); var reqPerSec = (requestsCnt + 0.0)/sw.ElapsedMilliseconds*1000; context.Log.Info("{0} requests completed in {1}ms ({2:0.00} reqs per sec).", requestsCnt, sw.ElapsedMilliseconds, reqPerSec); PerfUtils.LogData( Keyword, PerfUtils.Row(PerfUtils.Col("clientsCnt", clientsCnt), PerfUtils.Col("requestsCnt", requestsCnt), PerfUtils.Col("ElapsedMilliseconds", sw.ElapsedMilliseconds)), PerfUtils.Row(PerfUtils.Col("successes", succ), PerfUtils.Col("failures", fail)) ); PerfUtils.LogTeamCityGraphData(string.Format("{0}-{1}-{2}-reqPerSec", Keyword, clientsCnt, requestsCnt), (int)reqPerSec); PerfUtils.LogTeamCityGraphData( string.Format("{0}-{1}-{2}-failureSuccessRate", Keyword, clientsCnt, requestsCnt), 100*fail/(fail + succ)); context.Success(); }