Esempio n. 1
0
        private void btnParallelDispatch_Click(object sender, EventArgs e)
        {
            warmup();
            var unsecure    = chkUnsecureEcho.Checked;
            var argsMarshal = chkArgsMarshalling.Checked;

            Text        = "Working...";
            tbNote.Text = "Started...";
            var w = Stopwatch.StartNew();

            var client = new JokeContractClient(cbo.Text);

            client.DispatchTimeoutMs = 5 * 1000;
            client.TimeoutMs         = 40 * 1000;
            if (!unsecure && chkImpersonate.Checked)
            {
                client.Headers.Add(new AuthenticationHeader(new IDPasswordCredentials(tbID.Text, tbPwd.Text)));
            }

            var totalCalls = tbCallsPerReactor.Text.AsInt();


            var calls   = new ConcurrentQueue <Call>();
            var derrors = new ConcurrentQueue <string>();

            System.Threading.Tasks.Parallel.For(0, totalCalls,
                                                (i) =>
            {
                try
                {
                    calls.Enqueue(new Call(
                                      argsMarshal ?
                                      client.Async_UnsecEchoMar("Call number {0} ".Args(i))
                                         :
                                      (unsecure ? client.Async_UnsecureEcho("Call number {0} ".Args(i))
                                                  : client.Async_Echo("Call number {0} ".Args(i)))));
                }
                catch (Exception err)
                {
                    derrors.Enqueue("{0}: {1}\r\n".Args(1, err.ToMessageWithType()));
                }
            });



            string estr = null;

            while (derrors.TryDequeue(out estr))
            {
                tbNote.Text += estr;
            }


            var reactor = new CallReactor(calls);

            var callsPlaced = w.ElapsedMilliseconds;

            reactor.Wait();


            var stats = new ConcurrentDictionary <CallStatus, int>();

            foreach (var call in reactor.Calls)
            {
                if (call.CallSlot.CallStatus == CallStatus.ResponseError)
                {
                    var msg = call.CallSlot.ResponseMsg;
                    Text = msg.ExceptionData.Message;
                }
                stats.AddOrUpdate(call.CallSlot.CallStatus, 1, (_, k) => k + 1);
            }

            var sb = new StringBuilder();

            foreach (var k in stats.Keys)
            {
                sb.AppendLine("{0} = {1} times".Args(k, stats[k]));
            }

            tbNote.Text += sb.ToString();

            var allFinished = w.ElapsedMilliseconds;

            Text = "Placed {0:n2} calls in {1:n2} ms, then waited {2:n2} ms for finish, total time {3:n2} ms @ {4:n2} calls/sec "
                   .Args
                   (
                totalCalls,
                callsPlaced,
                allFinished - callsPlaced,
                allFinished,
                totalCalls / (allFinished / 1000d)
                   );
        }