Ejemplo n.º 1
0
        private TcpPackage Pack(Guid correlationId, VerificationEvent evnt)
        {
            var readDto = new ClientMessageDto.ReadEvent(correlationId, evnt.EventStreamId,
                                                         evnt.ShouldBeVersion);

            return new TcpPackage(TcpCommand.ReadEvent, readDto.Serialize());
        }
Ejemplo n.º 2
0
        public bool Execute(CommandProcessorContext context, string[] args)
        {
            var eventStreamId = "test-stream";
            var fromNumber = 0;

            if (args.Length > 0)
            {
                if (args.Length > 2)
                    return false;

                eventStreamId = args[0];

                if (args.Length == 2)
                    fromNumber = int.Parse(args[1]);
            }

            context.IsAsync();

            var readDto = new ClientMessageDto.ReadEvent(Guid.NewGuid(), eventStreamId, fromNumber);
            var package = new TcpPackage(TcpCommand.ReadEvent, readDto.Serialize());

            var sw = new Stopwatch();

            context.Client.CreateTcpConnection(
                context,
                connectionEstablished: conn =>
                {
                    context.Log.Info("[{0}]: Reading...", conn.EffectiveEndPoint);
                    sw.Start();
                    conn.EnqueueSend(package.AsByteArray());
                },
                handlePackage: (conn, pkg) =>
                {
                    if (pkg.Command != TcpCommand.ReadEventCompleted)
                    {
                        context.Fail(reason: string.Format("Unexpected TCP package: {0}.", pkg.Command));
                        return;
                    }

                    sw.Stop();

                    var dto = pkg.Data.Deserialize<ClientMessageDto.ReadEventCompleted>();

                    context.Log.Info("READ events from <{0}>:\n\n"
                                     + "\tCorrelationId: {1}\n"
                                     + "\tEventStreamId: {2}\n"
                                     + "\tEventNumber:   {3}\n"
                                     + "\tReadResult:    {4}\n"
                                     + "\tEventType:     {5}\n"
                                     + "\tData:          {6}\n"
                                     + "\tMetadata:      {7}\n",
                                     eventStreamId,
                                     dto.CorrelationId,
                                     dto.EventStreamId,
                                     dto.EventNumber,
                                     (SingleReadResult) dto.Result,
                                     dto.EventType,
                                     Encoding.UTF8.GetString(dto.Data ?? new byte[0]),
                                     Encoding.UTF8.GetString(dto.Metadata ?? new byte[0]));

                    context.Log.Info("Read request took: {0}.", sw.Elapsed);

                    PerfUtils.LogTeamCityGraphData(string.Format("{0}-latency-ms", Keyword), (int)sw.ElapsedMilliseconds);

                    conn.Close();
                    context.Success();
                },
                connectionClosed: (connection, error) =>
                {
                    if (error == SocketError.Success)
                        context.Success();
                    else
                        context.Fail();
                });

            context.WaitForCompletion();
            return true;
        }
Ejemplo n.º 3
0
        private void ReadFlood(CommandProcessorContext context, string eventStreamId, int clientsCnt, long requestsCnt)
        {
            context.IsAsync();

            var clients = new List<TcpTypedConnection<byte[]>>();
            var threads = new List<Thread>();
            var autoResetEvent = new AutoResetEvent(false);
            var sw2 = new Stopwatch();

            long 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;

                var client = context.Client.CreateTcpConnection(
                    context,
                    (conn, pkg) =>
                    {
                        Interlocked.Increment(ref received);

                        var localAll = Interlocked.Increment(ref all);
                        if (localAll % 1000 == 0) Console.Write(".");
                        if (localAll % 100000 == 0)
                        {
                            var elapsed = sw2.Elapsed;
                            sw2.Restart();
                            context.Log.Trace("\nDONE TOTAL {0} READS IN {1} ({2:0.0}/s).",
                                              localAll,
                                              elapsed,
                                              1000.0*100000/elapsed.TotalMilliseconds);
                        }
                        if (localAll == requestsCnt)
                            autoResetEvent.Set();
                    },
                    connectionClosed: (conn, err) =>
                    {
                        if (all < requestsCnt)
                            context.Fail(null, "Socket was closed, but not all requests were completed.");
                        else
                            context.Success();
                    });

                client.ConnectionClosed += (_, __) => context.Log.Debug("READS sent: {0}, received: {1}", sent, received);
                clients.Add(client);

                threads.Add(new Thread(() => 
                {
                    for (int j = 0; j < count; ++j)
                    {
                        var read = new ClientMessageDto.ReadEvent(Guid.NewGuid(), eventStreamId, 0);
                        var package = new TcpPackage(TcpCommand.ReadEvent, read.Serialize());
                        client.EnqueueSend(package.AsByteArray());
                        
                        Interlocked.Increment(ref sent);
                        while (sent - received > context.Client.Options.ReadWindow)
                            Thread.Sleep(1);
                    }
                }));
            }

            var sw = Stopwatch.StartNew();
            sw2.Start();
            foreach (var thread in threads)
            {
                thread.IsBackground = true;
                thread.Start();
            }

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

            foreach (var client in clients)
            {
                client.Close();
            }

            context.Log.Info("Completed. READS done: {0}.", all);

            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", all))
            );

            PerfUtils.LogTeamCityGraphData(string.Format("{0}-{1}-{2}-reqPerSec", Keyword, clientsCnt, requestsCnt),
                        (int)reqPerSec);
         
            context.Success();
        }
Ejemplo n.º 4
0
        private void Read(Status status, int readerIdx, CommandProcessorContext context, AutoResetEvent finishedEvent)
        {
            TcpTypedConnection<byte[]> connection;
            var iteration = new AutoResetEvent(false);

            var successes = 0;
            var fails = 0;

            var rnd = new Random();

            var streamIdx = -1;
            var eventidx = -1;

            Action<TcpTypedConnection<byte[]>, TcpPackage> packageReceived = (conn, pkg) =>
            {
                var dto = pkg.Data.Deserialize<ClientMessageDto.ReadEventCompleted>();
                switch ((SingleReadResult)dto.Result)
                {
                    case SingleReadResult.Success:
                        if (Equal(_streams[streamIdx], eventidx, dto.EventType, dto.Data))
                        {
                            successes++;
                            if (successes % 1000 == 0)
                                status.ReportReadsProgress(readerIdx, successes, fails);
                        }
                        else
                        {
                            fails++;
                            status.ReportReadError(readerIdx, _streams[streamIdx], eventidx);
                        }
                        break;
                    case SingleReadResult.NotFound:
                    case SingleReadResult.NoStream:
                    case SingleReadResult.StreamDeleted:
                        fails++;
                        status.ReportNotFoundOnRead(readerIdx, _streams[streamIdx], eventidx);
                        break;
                    default:
                        throw new ArgumentOutOfRangeException();
                }

                iteration.Set();
            };
            Action<TcpTypedConnection<byte[]>> established = _ => { };
            Action<TcpTypedConnection<byte[]>, SocketError> closed = null;
            closed = (_, __) =>
            {
                Thread.Sleep(TimeSpan.FromSeconds(1));
                connection = context.Client.CreateTcpConnection(context, packageReceived, cn => iteration.Set(), closed, false);
            };

            connection = context.Client.CreateTcpConnection(context, packageReceived, established, closed, false);

            while (!_stopReading)
            {
                streamIdx = NextStreamForReading(rnd, readerIdx);
                int head;
                lock (_heads)
                    head = _heads[streamIdx];

                if (head > 0)
                {
                    eventidx = NextRandomEventVersion(rnd, head);
                    var stream = _streams[streamIdx];
                    var read = new ClientMessageDto.ReadEvent(Guid.Empty, stream, eventidx);
                    var package = new TcpPackage(TcpCommand.ReadEvent, read.Serialize());

                    connection.EnqueueSend(package.AsByteArray());
                    iteration.WaitOne();
                }
                else
                    Thread.Sleep(100);
            }

            status.ReportReadsProgress(readerIdx, successes, fails);
            status.FinilizeStatus(readerIdx, fails == 0);
            connection.Close();
            finishedEvent.Set();
        }
Ejemplo n.º 5
0
 public override TcpPackage CreateNetworkPackage(Guid correlationId)
 {
     lock (_stepMoveLock)
     {
         switch (_step)
         {
             case 0:
                 var writeDto = new ClientMessageDto.WriteEvents(correlationId,
                                                                 _event.EventStreamId,
                                                                 _event.ExpectedVersion,
                                                                 new[] { new ClientMessageDto.Event(_event.Event) });
                 return new TcpPackage(TcpCommand.WriteEvents, correlationId, writeDto.Serialize());
             case 1:
                 var readDto = new ClientMessageDto.ReadEvent(correlationId, 
                                                              _event.EventStreamId, 
                                                              _event.ShouldBeVersion);
                 return new TcpPackage(TcpCommand.ReadEvent, correlationId, readDto.Serialize());
             default:
                 throw new ArgumentOutOfRangeException("_step", "step can be 0 or 1 for write task");
         }
     }
 }