コード例 #1
0
        protected CommunicationController(IPublisher publisher)
        {
            Ensure.NotNull(publisher, "publisher");

            _publisher = publisher;
            Client     = new HttpAsyncClient();
        }
コード例 #2
0
        public void SetUp(Action <IHttpService> bootstrap = null)
        {
            _bus = new InMemoryBus($"bus_{_serverEndPoint.Port}");
            var pipelineBus = InMemoryBus.CreateTest();
            var queue       = new QueuedHandlerThreadPool(pipelineBus, "Test", new QueueStatsManager(), true, TimeSpan.FromMilliseconds(50));

            _multiQueuedHandler = new MultiQueuedHandler(new IQueuedHandler[] { queue }, null);
            _multiQueuedHandler.Start();

            _service = new KestrelHttpService(ServiceAccessibility.Private, _bus, new NaiveUriRouter(),
                                              _multiQueuedHandler, false, null, 0, false, _serverEndPoint);
            _internalDispatcher = new InternalDispatcherEndpoint(queue, _multiQueuedHandler);
            _bus.Subscribe(_internalDispatcher);
            KestrelHttpService.CreateAndSubscribePipeline(pipelineBus);
            bootstrap?.Invoke(_service);
            _server = new TestServer(
                new WebHostBuilder()
                .UseStartup(new ClusterVNodeStartup(Array.Empty <ISubsystem>(), queue, _bus, _multiQueuedHandler,
                                                    new TestAuthenticationProvider(),
                                                    new IHttpAuthenticationProvider[] {
                new BasicHttpAuthenticationProvider(new TestAuthenticationProvider()),
                new AnonymousHttpAuthenticationProvider(),
            }, new TestAuthorizationProvider(), new FakeReadIndex(_ => false), 1024 * 1024, _service)));
            _httpMessageHandler = _server.CreateHandler();
            _client             = new HttpAsyncClient(_timeout, _httpMessageHandler);

            HttpBootstrap.Subscribe(_bus, _service);
        }
コード例 #3
0
ファイル: speed_test.cs プロジェクト: vebin/EventStore
        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 HttpAuthenticationProvider[] { new AnonymousHttpAuthenticationProvider() };
            var        httpService        = new HttpService(ServiceAccessibility.Public, inputBus,
                                                            new TrieUriRouter(), multiQueuedHandler, false, "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();
        }
コード例 #4
0
 public GossipController(IPublisher publisher, IPublisher networkSendQueue, TimeSpan gossipTimeout, HttpMessageHandler httpMessageHandler)
     : base(publisher)
 {
     _networkSendQueue = networkSendQueue;
     _gossipTimeout    = gossipTimeout;
     _client           = new HttpAsyncClient(_gossipTimeout, httpMessageHandler);
 }
コード例 #5
0
        public GossipController(IPublisher publisher, IPublisher networkSendQueue, TimeSpan gossipTimeout, Func <X509Certificate, X509Chain, SslPolicyErrors, ValueTuple <bool, string> > serverCertValidator, X509Certificate clientCertificate)
            : base(publisher)
        {
            _networkSendQueue = networkSendQueue;
            _gossipTimeout    = gossipTimeout;

            var socketsHttpHandler = new SocketsHttpHandler {
                SslOptions =
                {
                    RemoteCertificateValidationCallback = (sender,                         certificate, chain, errors) => {
                        var(isValid, error)             = serverCertValidator(certificate, chain,       errors);
                        if (!isValid && error != null)
                        {
                            Log.Error("Server certificate validation error: {e}", error);
                        }
                        return(isValid);
                    },
                    ClientCertificates                  = new X509CertificateCollection()
                }
            };

            if (clientCertificate != null)
            {
                socketsHttpHandler.SslOptions.ClientCertificates.Add(clientCertificate);
            }

            _client = new HttpAsyncClient(_gossipTimeout, socketsHttpHandler);
        }
        /// <summary>
        /// Creates a new <see cref="IEventStoreConnection"/> to EventStore cluster
        /// using specific <see cref="ConnectionSettings"/> and <see cref="ClusterSettings"/>
        /// </summary>
        /// <param name="connectionSettings">The <see cref="ConnectionSettings"/> to apply to the new connection</param>
        /// <param name="clusterSettings">The <see cref="ClusterSettings"/> that determine cluster behavior.</param>
        /// <param name="connectionName">Optional name of connection (will be generated automatically, if not provided)</param>
        /// <returns>a new <see cref="IEventStoreConnection"/></returns>
        public static IEventStoreConnection Create(ConnectionSettings connectionSettings,
                                                   ClusterSettings clusterSettings, string connectionName = null)
        {
            Ensure.NotNull(connectionSettings, "connectionSettings");
            Ensure.NotNull(clusterSettings, "clusterSettings");

            var handler = connectionSettings.CustomHttpMessageHandler;

            if (handler is null && !connectionSettings.ValidateServer)
            {
                handler = new HttpClientHandler {
                    ServerCertificateCustomValidationCallback = delegate { return(true); }
                };
            }

            var discoverClient     = new HttpAsyncClient(connectionSettings.GossipTimeout, handler);
            var endPointDiscoverer = new ClusterDnsEndPointDiscoverer(connectionSettings.Log,
                                                                      clusterSettings.ClusterDns,
                                                                      clusterSettings.MaxDiscoverAttempts,
                                                                      clusterSettings.HttpPort,
                                                                      clusterSettings.GossipSeeds,
                                                                      clusterSettings.GossipTimeout,
                                                                      clusterSettings.NodePreference,
                                                                      CompatibilityMode.Create(connectionSettings.CompatibilityMode),
                                                                      discoverClient);

            return(new EventStoreNodeConnection(connectionSettings, clusterSettings, endPointDiscoverer,
                                                connectionName));
        }
コード例 #7
0
        public void SetUp()
        {
            _bus = new InMemoryBus(string.Format("bus_{0}", _serverEndPoint.Port));

            _service = new HttpService(_bus, new[] { _serverEndPoint.ToHttpUrl() });
            _client  = new HttpAsyncClient();

            HttpBootstrap.Subscribe(_bus, _service);
        }
コード例 #8
0
        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);
        }
コード例 #9
0
        public void Setup()
        {
            _fixture = new Fixture()
                  .Customize(new AutoMoqCustomization())
                  .Customize(new MultipleCustomization());

            _httpService = _fixture.Freeze<Mock<IHttpService>>();
            _httpCachingService = _fixture.Freeze<Mock<IHttpCachingService>>();

            _baseAddress = _fixture.Create<string>();
            _uri = _fixture.Create<string>();
            _headers = _fixture.Create<Dictionary<string, string>>();

            _sut = _fixture.Create<HttpAsyncClient>();
        }
コード例 #10
0
        public void Setup()
        {
            _fixture = new Fixture()
                       .Customize(new AutoMoqCustomization())
                       .Customize(new MultipleCustomization());

            _httpService        = _fixture.Freeze <Mock <IHttpService> >();
            _httpCachingService = _fixture.Freeze <Mock <IHttpCachingService> >();

            _baseAddress = _fixture.Create <string>();
            _uri         = _fixture.Create <string>();
            _headers     = _fixture.Create <Dictionary <string, string> >();

            _sut = _fixture.Create <HttpAsyncClient>();
        }
コード例 #11
0
        public ClusterDnsEndPointDiscoverer(ILogger log,
                                            string clusterDns,
                                            int maxDiscoverAttempts,
                                            int managerExternalHttpPort,
                                            GossipSeed[] gossipSeeds,
                                            TimeSpan gossipTimeout)
        {
            Ensure.NotNull(log, "log");

            _log                     = log;
            _clusterDns              = clusterDns;
            _maxDiscoverAttempts     = maxDiscoverAttempts;
            _managerExternalHttpPort = managerExternalHttpPort;
            _gossipSeeds             = gossipSeeds;
            _gossipTimeout           = gossipTimeout;
            _client                  = new HttpAsyncClient(log);
        }
コード例 #12
0
        public ClusterDnsEndPointDiscoverer(ILogger log,
                                            string clusterDns,
                                            int maxDiscoverAttempts,
                                            int managerExternalHttpPort,
                                            IPAddress[] fakeDnsEntries,
                                            TimeSpan gossipTimeout)
        {
            Ensure.NotNull(log, "log");
            Ensure.NotNullOrEmpty(clusterDns, "clusterDns");

            _log                     = log;
            _clusterDns              = clusterDns;
            _maxDiscoverAttempts     = maxDiscoverAttempts;
            _managerExternalHttpPort = managerExternalHttpPort;
            _fakeDnsEntries          = fakeDnsEntries;
            _gossipTimeout           = gossipTimeout;
            _client                  = new HttpAsyncClient(log);
        }
コード例 #13
0
        public void SetUp()
        {
            _bus = new InMemoryBus(string.Format("bus_{0}", _serverEndPoint.Port));

            {
                var pipelineBus = InMemoryBus.CreateTest();
                var queue       = new QueuedHandlerThreadPool(pipelineBus, "Test", true, TimeSpan.FromMilliseconds(50));
                _multiQueuedHandler = new MultiQueuedHandler(new IQueuedHandler[] { queue }, null);
                var httpAuthenticationProviders = new HttpAuthenticationProvider[] { new AnonymousHttpAuthenticationProvider() };

                _service = new HttpService(ServiceAccessibility.Private, _bus, new NaiveUriRouter(),
                                           _multiQueuedHandler, false, _serverEndPoint.ToHttpUrl());
                HttpService.CreateAndSubscribePipeline(pipelineBus, httpAuthenticationProviders);
                _client = new HttpAsyncClient();
            }

            HttpBootstrap.Subscribe(_bus, _service);
        }
コード例 #14
0
        public ClusterDnsEndPointDiscoverer(ILogger log,
                                            string clusterDns,
                                            int maxDiscoverAttempts,
                                            int managerExternalHttpPort,
                                            GossipSeed[] gossipSeeds,
                                            TimeSpan gossipTimeout,
                                            bool preferRandomNode,
                                            HttpClientHandler clientHandler = null)
        {
            Ensure.NotNull(log, "log");

            _log                     = log;
            _clusterDns              = clusterDns;
            _maxDiscoverAttempts     = maxDiscoverAttempts;
            _managerExternalHttpPort = managerExternalHttpPort;
            _gossipSeeds             = gossipSeeds;
            _gossipTimeout           = gossipTimeout;
            _preferRandomNode        = preferRandomNode;
            _client                  = new HttpAsyncClient(_gossipTimeout, clientHandler);
        }
コード例 #15
0
        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);
        }
コード例 #16
0
        public void SetUp(Action <IHttpService> bootstrap = null)
        {
            _bus = new InMemoryBus($"bus_{_serverEndPoint.Port}");
            var pipelineBus = InMemoryBus.CreateTest();
            var queue       = new QueuedHandlerThreadPool(pipelineBus, "Test", new QueueStatsManager(), true, TimeSpan.FromMilliseconds(50));

            _multiQueuedHandler = new MultiQueuedHandler(new IQueuedHandler[] { queue }, null);
            _multiQueuedHandler.Start();

            _service = new KestrelHttpService(ServiceAccessibility.Private, _bus, new NaiveUriRouter(),
                                              _multiQueuedHandler, false, null, 0, false, _serverEndPoint);
            KestrelHttpService.CreateAndSubscribePipeline(pipelineBus);
            bootstrap?.Invoke(_service);
            _server = new TestServer(
                new WebHostBuilder()
                .UseStartup(new HttpServiceStartup(_service)));
            _httpMessageHandler = _server.CreateHandler();
            _client             = new HttpAsyncClient(_timeout, _httpMessageHandler);

            HttpBootstrap.Subscribe(_bus, _service);
        }
コード例 #17
0
 public UsersClient(ILogger log, TimeSpan operationTimeout)
 {
     _operationTimeout = operationTimeout;
     _client           = new HttpAsyncClient(_operationTimeout);
 }
コード例 #18
0
        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)Math.Round(sw.Elapsed.TotalMilliseconds / requestsCnt));

            if (succ != requestsCnt)
            {
                context.Fail(reason: "There were errors or not all requests completed.");
            }
            else
            {
                context.Success();
            }
        }
コード例 #19
0
 public ElectController(IPublisher publisher) : base(publisher)
 {
     _operationTimeout = TimeSpan.FromMilliseconds(2000); //TODO make these configurable
     _client           = new HttpAsyncClient(_operationTimeout);
 }
コード例 #20
0
        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();
            }
        }
コード例 #21
0
 public ProjectionsClient(ILogger log, TimeSpan operationTimeout)
 {
     _operationTimeout = operationTimeout;
     _client           = new HttpAsyncClient(log);
 }
コード例 #22
0
        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();
        }
コード例 #23
0
        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();
            }
        }
コード例 #24
0
        private void PingFlood(CommandProcessorContext context, int clientsCnt, int requestsCnt)
        {
            context.IsAsync();

            var threads        = new List <Thread>();
            var autoResetEvent = new AutoResetEvent(false);

            var all = 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("/ping");

                    Action <HttpResponse> onsuccess = response =>
                    {
                        Interlocked.Increment(ref received);
                        var pongs = Interlocked.Increment(ref all);

                        if (pongs % 1000 == 0)
                        {
                            Console.Write('.');
                        }

                        if (pongs == requestsCnt)
                        {
                            autoResetEvent.Set();
                        }
                    };

                    Action <Exception> onException = e =>
                    {
                        context.Log.ErrorException(e, "Error during GET");
                        var pongs = Interlocked.Increment(ref all);
                        if (pongs == requestsCnt)
                        {
                            autoResetEvent.Set();
                        }
                    };

                    for (int j = 0; j < count; ++j)
                    {
                        client.Get(url, onsuccess, onException);
                        Interlocked.Increment(ref sent);
                        while (sent - received > context.Client.Options.PingWindow)
                        {
                            Thread.Sleep(1);
                        }
                    }
                }));
            }

            var sw = Stopwatch.StartNew();

            foreach (var thread in threads)
            {
                thread.IsBackground = true;
                thread.Start();
            }

            autoResetEvent.WaitOne();
            sw.Stop();

            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.LogTeamCityGraphData(string.Format("{0}-{1}-{2}-reqPerSec", Keyword, clientsCnt, requestsCnt),
                                           (int)reqPerSec);

            context.Success();
        }
コード例 #25
0
        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);
        }
コード例 #26
0
 public PersistentSubscriptionsClient(ILogger log, TimeSpan operationTimeout)
 {
     _operationTimeout = operationTimeout;
     _client           = new HttpAsyncClient(_operationTimeout);
 }
コード例 #27
0
 public ProjectionsClient(ILogger log)
 {
     _client = new HttpAsyncClient(log);
 }
コード例 #28
0
        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;

            var watchLockRoot = new object();
            var sw            = Stopwatch.StartNew();

            for (int i = 0; i < clientsCnt; i++)
            {
                int sent     = 0;
                int received = 0;

                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)
                        {
                            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();
        }
コード例 #29
0
 public PersistentSubscriptionsClient(ILogger log, TimeSpan operationTimeout, HttpMessageHandler httpMessageHandler = null)
 {
     _operationTimeout = operationTimeout;
     _client           = new HttpAsyncClient(_operationTimeout, httpMessageHandler);
 }
コード例 #30
0
        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();
        }
コード例 #31
0
        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);
        }