Esempio n. 1
0
        private void btnManyDBWorks_Click(object sender, EventArgs e)
        {
            warmup();
            var rCount = tbRecCount.Text.AsInt();
            var waitFrom = tbWaitFrom.Text.AsInt();
            var waitTo = tbWaitTo.Text.AsInt();

            Text = "Working...";
            tbNote.Text = "Started...";
            var w = Stopwatch.StartNew();
            
                var client = new JokeContractClient(cbo.Text);
 client.DispatchTimeoutMs = 5 * 1000;
 client.TimeoutMs = 40 * 1000;             

                var totalCalls = tbCallsPerReactor.Text.AsInt();
                
                
                    var calls = new ConcurrentQueue<Call>();
                    var derrors = new ConcurrentQueue<string>();
                    System.Threading.Tasks.Parallel.For(0, totalCalls, new ParallelOptions{ MaxDegreeOfParallelism = tbReactors.Text.AsInt(16)},
                        (i) =>
                        {
                            try
                            { 
                              calls.Enqueue( new Call(client.Async_DBWork(i.ToString(), rCount, 
                                                                                        waitFrom > 0 ? ExternalRandomGenerator.Instance.NextScaledRandomInteger(waitFrom,waitTo) : 0))); 
                                                       //ExternalRandomGenerator.Instance.NextScaledRandomInteger(1,100),
                                                       //ExternalRandomGenerator.Instance.NextScaledRandomInteger(0,50))) ); 
                            }
                            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} DB calls in {1} ms, then waited {2} ms for finish, total time {3} ms @ {4} calls/sec "
                   .Args
                   (
                     totalCalls,
                     callsPlaced, 
                     allFinished - callsPlaced,
                     allFinished,
                     (1000* totalCalls) / allFinished
                   );


            if (chkAutoDispatch.Checked)
            {
              tmrAuto.Interval = 1000 + ExternalRandomGenerator.Instance.NextScaledRandomInteger(100, 10000);
              tmrAuto.Enabled = true;
                            
            }
        }