public Get ( string url, Action |
||
url | string | |
onSuccess | Action |
|
onException | Action |
|
return | void |
public bool Execute(CommandProcessorContext context, string[] args) { var eventStreamId = "test-stream"; var eventNumber = 0; var resolveLinkTos = false; var requireMaster = false; if (args.Length > 0) { if (args.Length > 3) return false; eventStreamId = args[0]; if (args.Length >= 2) eventNumber = int.Parse(args[1]); if (args.Length >= 3) requireMaster = bool.Parse(args[2]); } context.IsAsync(); var client = new HttpAsyncClient(); var readUrl = context.Client.HttpEndpoint.ToHttpUrl("/streams/{0}/{1}?format=json", eventStreamId, eventNumber == -1 ? "head" : eventNumber.ToString()); context.Log.Info("[{0}]: Reading...", context.Client.HttpEndpoint); var sw = Stopwatch.StartNew(); client.Get(readUrl, new Dictionary<string, string> { {SystemHeaders.ResolveLinkTos, resolveLinkTos ? "True" : "False"}, {SystemHeaders.RequireMaster, requireMaster ? "True" : "False"} }, TimeSpan.FromMilliseconds(10000), response => { sw.Stop(); context.Log.Info("[{0} ({1})]: READ events from <{2}>.", response.HttpStatusCode, response.StatusDescription, eventStreamId); 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("Read request took: {0}.", sw.Elapsed); context.Success(); }, e => context.Fail(e, string.Format("READ events from <{0}>: FAILED", eventStreamId))); return true; }
public bool Execute(CommandProcessorContext context, string[] args) { var eventStreamId = "test-stream"; var version = 0; if (args.Length > 0) { if (args.Length > 2) return false; eventStreamId = args[0]; if (args.Length == 2) version = int.Parse(args[1]); } context.IsAsync(); var client = new HttpAsyncClient(); var readUrl = context.Client.HttpEndpoint.ToHttpUrl("/streams/{0}/event/{1}", eventStreamId, version); context.Log.Info("[{0}]: Reading...", context.Client.HttpEndpoint); var sw = Stopwatch.StartNew(); client.Get(readUrl, response => { sw.Stop(); context.Log.Info("READ events from <{0}>: {1}", eventStreamId, response.Body); context.Log.Info("Read request took: {0}.", sw.Elapsed); context.Success(); }, e => context.Fail(e, string.Format("READ events from <{0}>: FAILED", eventStreamId))); return true; }
private void Flood(CommandProcessorContext context, int clientsCnt, int minPerSecond, int maxPerSecond, int runTimeMinutes) { context.IsAsync(); var threads = new List<Thread>(); var autoResetEvent = new AutoResetEvent(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 client = new HttpAsyncClient(); Action<HttpResponse> succHandler = response => { var succDone = Interlocked.Increment(ref succ); if (succDone % maxPerSecond == 0) Console.Write("."); Interlocked.Increment(ref requestsCnt); Interlocked.Increment(ref received); }; var sentCount = 0; var sleepTime = 0; var currentMinute = -1; while (true) { TimeSpan elapsed; lock (watchLockRoot) elapsed = sw.Elapsed; if (elapsed.TotalMinutes > runTimeMinutes) { autoResetEvent.Set(); break; } if (sentCount == 0) { int elapsedMinutesInt = (int)elapsed.TotalMinutes; lock (_randomLockRoot) { sentCount = minPerSecond == maxPerSecond ? maxPerSecond : _random.Next(minPerSecond, maxPerSecond); } if (currentMinute != elapsedMinutesInt) { currentMinute = elapsedMinutesInt; context.Log.Info(Environment.NewLine + "Elapsed {0} of {1} minutes, sent {2}", elapsedMinutesInt, runTimeMinutes, sent); } sleepTime = 1000 / sentCount; } var url = context.Client.HttpEndpoint.ToHttpUrl("/ping"); client.Get(url, succHandler, exc => context.Log.ErrorException(exc, "Error during GET.")); Interlocked.Increment(ref sent); Thread.Sleep(sleepTime); sentCount -= 1; while (sent - received > context.Client.Options.PingWindow/clientsCnt) { 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), (int) ((fail/(succ + 0.0))*100)); context.Success(); }
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(); }
private void ReadFlood(CommandProcessorContext context, string eventStreamId, int clientsCnt, int requestsCnt) { context.IsAsync(); var threads = new List<Thread>(); var autoResetEvent = new AutoResetEvent(false); var readsCnt = 0; 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 client = new HttpAsyncClient(); var url = context.Client.HttpEndpoint.ToHttpUrl("/streams/{0}/event/{1}", eventStreamId, 0); Action<HttpResponse> onSuccess = response => { var readsLocal = Interlocked.Increment(ref readsCnt); Interlocked.Increment(ref received); if (readsLocal % 1000 == 0) Console.Write("."); if (readsLocal == requestsCnt) autoResetEvent.Set(); }; for (int j = 0; j < count; ++j) { client.Get(url, onSuccess, e => { context.Log.ErrorException(e, "Error during GET"); var readsLocal = Interlocked.Increment(ref readsCnt); if (readsLocal == requestsCnt) autoResetEvent.Set(); }); Interlocked.Increment(ref sent); while (sent - received > context.Client.Options.ReadWindow) Thread.Sleep(1); } })); } var sw = Stopwatch.StartNew(); foreach (var thread in threads) { thread.IsBackground = true; thread.Start(); } autoResetEvent.WaitOne(); sw.Stop(); context.Log.Info("Completed. READS done: {0}.", readsCnt); 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("readsCnt", readsCnt))); PerfUtils.LogTeamCityGraphData(string.Format("{0}-{1}-{2}-reqPerSec", Keyword, clientsCnt, requestsCnt), (int) reqPerSec); context.Success(); }
private void ReadFlood(CommandProcessorContext context, string eventStreamId, int clientsCnt, long requestsCnt) { context.IsAsync(); var threads = new List<Thread>(); var doneEvent = new ManualResetEventSlim(false); long succ = 0; long fail = 0; long all = 0; 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 client = new HttpAsyncClient(); var url = context.Client.HttpEndpoint.ToHttpUrl("/streams/{0}/{1}?format=json", eventStreamId, 0); 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} READS IN {1} ({2:0.0}/s).", localAll, elapsed, 1000.0 * 10000 / elapsed.TotalMilliseconds); } if (localAll == requestsCnt) doneEvent.Set(); }; Action<HttpResponse> onSuccess = response => { if (Interlocked.Increment(ref succ) % 100 == 0) Console.Write("."); onReceived(); }; Action<Exception> onException = exc => { context.Log.ErrorException(exc, "Error during GET"); if (Interlocked.Increment(ref fail) % 100 == 0) Console.Write("#"); onReceived(); }; for (int j = 0; j < count; ++j) { client.Get(url, TimeSpan.FromMilliseconds(10000), onSuccess, onException); var localSent = Interlocked.Increment(ref sent); while (localSent - Interlocked.Read(ref received) > context.Client.Options.ReadWindow/clientsCnt) { Thread.Sleep(1); } } }) { IsBackground = true }); } var sw = Stopwatch.StartNew(); sw2.Start(); threads.ForEach(thread => thread.Start()); doneEvent.Wait(); sw.Stop(); var reqPerSec = (all + 0.0)/sw.ElapsedMilliseconds*1000; context.Log.Info("Completed. READS succ: {0}, fail: {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("readsCnt", all))); PerfUtils.LogTeamCityGraphData(string.Format("{0}-{1}-{2}-reqPerSec", Keyword, clientsCnt, requestsCnt), (int) reqPerSec); if (Interlocked.Read(ref succ) == requestsCnt) context.Success(); else context.Fail(); }
private void PingFlood(CommandProcessorContext context, 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; for (int i = 0; i < clientsCnt; i++) { var count = requestsCnt / clientsCnt + ((i == clientsCnt - 1) ? requestsCnt % clientsCnt : 0); long received = 0; long sent = 0; threads.Add(new Thread(() => { var client = new HttpAsyncClient(); var url = context.Client.HttpEndpoint.ToHttpUrl("/ping"); Action<HttpResponse> onSuccess = response => { Interlocked.Increment(ref received); if (Interlocked.Increment(ref succ) % 1000 == 0) Console.Write('.'); if (Interlocked.Increment(ref all) == requestsCnt) doneEvent.Set(); }; Action<Exception> onException = e => { context.Log.ErrorException(e, "Error during GET"); Interlocked.Increment(ref received); if (Interlocked.Increment(ref fail) % 1000 == 0) Console.Write('#'); if (Interlocked.Increment(ref all) == requestsCnt) doneEvent.Set(); }; for (int j = 0; j < count; ++j) { client.Get(url, TimeSpan.FromMilliseconds(10000), onSuccess, onException); var localSent = Interlocked.Increment(ref sent); while (localSent - Interlocked.Read(ref received) > context.Client.Options.PingWindow/clientsCnt) { Thread.Sleep(1); } } }) { IsBackground = true }); } var sw = Stopwatch.StartNew(); threads.ForEach(thread => thread.Start()); doneEvent.Wait(); sw.Stop(); var reqPerSec = (all + 0.0) / sw.ElapsedMilliseconds * 1000; 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.LogTeamCityGraphData(string.Format("{0}-{1}-{2}-reqPerSec", Keyword, clientsCnt, requestsCnt), (int)reqPerSec); if (Interlocked.Read(ref succ) == requestsCnt) context.Success(); else context.Fail(); }