Ejemplo n.º 1
0
        public static HttpSettings Parse(params object[] args)
        {
            if (args.Length == 0)
                return null;

            dynamic instance = new HttpSettings();
            var namedArgs = GetNamedArgs(args);
            foreach (var namedArg in namedArgs)
            {
                instance[namedArg.Item1] = namedArg.Item2;
            }

            return instance;
        }
Ejemplo n.º 2
0
        private void ConcurrentExecute(HttpSettings setting, ConcurrentBag<HttpResult> results)
        {
            _requestTimes = setting.Times;
            var echoCount = setting.Times / 10;

            Console.WriteLine("Benchmarking {0}", setting.Url);
            if(setting.Warmup > 0)
            {
                Console.WriteLine(" warmup {0} requests", setting.Warmup);
                for (int i = 0; i < setting.Warmup; i++)
                {
                    HttpGet(setting);
                }
            }
            Console.WriteLine("");

            var threads = new List<Thread>();
            var wait = new AutoResetEvent(false);
            for (var c = 0; c < setting.Concurrent; c++)
            {
                threads.Add(new Thread(() =>
                {
                    while (_requestTimes > 0)
                    {
                        Interlocked.Decrement(ref _requestTimes);

                        if (setting.WaitMilliseconds > 0)
                            Thread.Sleep(setting.WaitMilliseconds);

                        var result = HttpGet(setting);
                        results.Add(result);

                        var requestedCount = results.Count;
                        if (echoCount >= 2 && requestedCount % echoCount == 0)
                            Console.WriteLine("Completed {0} requests", requestedCount);
                    }

                    if (results.Count >= setting.Times && _requestTimes <= 0)
                    {
                        wait.Set();
                    }
                }));
            }

            threads.AsParallel().ForAll(t => t.Start());
            wait.WaitOne();

            Console.WriteLine("");
        }
Ejemplo n.º 3
0
        public IEnumerable<HttpResult> Execute(HttpSettings setting)
        {
            if (setting == null)
                throw new ArgumentNullException("setting");

            if (!string.IsNullOrWhiteSpace(setting.BasicAuthentication))
            {
                var account = setting.BasicAuthentication.Split(':');
                var credential = new NetworkCredential(account[0], account[1]);
                _credentialCache = new CredentialCache { { setting.Url, "Basic", credential } };
            }

            var results = new ConcurrentBag<HttpResult>();
            ConcurrentExecute(setting, results);

            return results;
        }
Ejemplo n.º 4
0
        private HttpResult HttpGet(HttpSettings setting)
        {
            if (setting == null)
            {
                throw new ArgumentNullException("setting");
            }

            var result = new HttpResult {
                ManagedThreadId = Thread.CurrentThread.ManagedThreadId, Start = DateTime.Now
            };
            var sw = new Stopwatch();

            sw.Start();

            var request = (HttpWebRequest)WebRequest.Create(setting.Url);

            request.Method          = "GET";
            request.PreAuthenticate = true;
            request.UserAgent       = _userAgent;
            if (_credentialCache != null)
            {
                request.Credentials = _credentialCache;
            }

            try
            {
                var response = (HttpWebResponse)request.GetResponse();
                result.Status         = (int)response.StatusCode;
                result.TransferLength = ResponseBytes(response);
            }
            catch (WebException we)
            {
                var response = we.Response as HttpWebResponse;
                if (response != null)
                {
                    result.Status         = (int)response.StatusCode;
                    result.TransferLength = ResponseBytes(response);
                }
            }

            sw.Stop();
            result.ElapsedMilliseconds = sw.ElapsedMilliseconds;
            result.End = DateTime.Now;

            return(result);
        }
Ejemplo n.º 5
0
        public IEnumerable <HttpResult> Execute(HttpSettings setting)
        {
            if (setting == null)
            {
                throw new ArgumentNullException("setting");
            }

            if (!string.IsNullOrWhiteSpace(setting.BasicAuthentication))
            {
                var account    = setting.BasicAuthentication.Split(':');
                var credential = new NetworkCredential(account[0], account[1]);
                _credentialCache = new CredentialCache {
                    { setting.Url, "Basic", credential }
                };
            }

            var results = new ConcurrentBag <HttpResult>();

            ConcurrentExecute(setting, results);

            return(results);
        }
Ejemplo n.º 6
0
        static void PrintResults(HttpSettings setting, IEnumerable<HttpResult> results)
        {
            if (setting == null)
                throw new ArgumentNullException("setting");

            if (results == null)
                throw new ArgumentNullException("results");

            var totalElapsed = results.Sum(r => r.ElapsedMilliseconds);
            var totalTransferred = results.Sum(r => r.TransferLength);
            var totalTime = (results.Max(r => r.End) - results.Min(r => r.Start)).TotalMilliseconds;
            Console.WriteLine("Concurrent level:\t{0:N0}", setting.Concurrent);
            Console.WriteLine("Time taken for tests:\t{0:F3} seconds", totalTime / 1000);

            Console.WriteLine("Complete requests:\t{0:N0}", results.Count(r => r.Status == 200));
            Console.WriteLine("Failed requests:\t{0:N0}", results.Count(r => r.Status != 200));
            Console.WriteLine("Total transferred:\t{0:N0} bytes", results.Sum(r => r.TransferLength));
            var requestPerSec = 1000.0 / ((double)totalTime / results.Count());
            var timePerSec = (double)totalTime / results.Count();
            Console.WriteLine("Request per second:\t{0:F3} (#/sec)", requestPerSec);
            Console.WriteLine("Time per request:\t{0:F3} [ms]", timePerSec);
            Console.WriteLine("Transfer rate:  \t{0:F2} [Kbytes/sec] received", ((double)totalTransferred / 1024) / ((double)totalTime / 1000.0));

            Console.WriteLine("");
            var elapseds = (from r in results
                            group r by r.ElapsedMilliseconds
                            into timeGroup
                            select new
                                       {
                                           ElapsedMilliseconds = timeGroup.Key,
                                           Percent = (double) timeGroup.Count()/setting.Times
                                       } as dynamic
                           ).OrderBy(e => e.ElapsedMilliseconds);

            Console.WriteLine(" fastest:\t{0} ms", elapseds.Min(e => (long)e.ElapsedMilliseconds));
            Console.WriteLine(" average:\t{0} ms", (long)elapseds.Average(e => (long)e.ElapsedMilliseconds));
            Console.WriteLine(" longest:\t{0} ms", elapseds.Max(e => (long)e.ElapsedMilliseconds));
            Console.WriteLine("");
            Console.WriteLine("Percentage of the requests (ms)");
            Console.WriteLine("");
            var percents = new List<dynamic>();
            percents.AddRange(elapseds);
            if (elapseds.Count() > 10)
            {
                var totalPercents = 0.0;
                var sectionPoint = 0.5;
                percents.Clear();
                foreach (var elapsed in elapseds)
                {
                    totalPercents += elapsed.Percent;
                    if (totalPercents >= sectionPoint || elapsed == elapseds.Last())
                    {
                        dynamic p = new ExpandoObject();
                        p.ElapsedMilliseconds = elapsed.ElapsedMilliseconds;
                        p.Percent = totalPercents;

                        percents.Add(p);
                        sectionPoint += 0.05;
                    }
                }
            }

            foreach (var elapsed in percents.OrderBy(e => e.ElapsedMilliseconds))
            {
                Console.WriteLine("  {0:0#.#0} %:\t{1} ms", elapsed.Percent * 100, elapsed.ElapsedMilliseconds);
            }

            #if DEBUG
            var managedThreads = results.Select(r => r.ManagedThreadId)
                .Distinct()
                .OrderBy(t => t)
                .Select(t => t.ToString());
            var ids = string.Join(", ", managedThreads);
            //if (ids.Length > 80)
            //    ids = ids.Substring(0, 80) + "...";

            Console.WriteLine("");
            Console.WriteLine("Managed Threads:\t{0}\n {1}", managedThreads.Count(), ids);
            #endif
        }
Ejemplo n.º 7
0
        private HttpResult HttpGet(HttpSettings setting)
        {
            if (setting == null)
                throw new ArgumentNullException("setting");

            var result = new HttpResult { ManagedThreadId = Thread.CurrentThread.ManagedThreadId, Start = DateTime.Now };
            var sw = new Stopwatch();
            sw.Start();

            var request = (HttpWebRequest)WebRequest.Create(setting.Url);
            request.Method = "GET";
            request.PreAuthenticate = true;
            request.UserAgent = _userAgent;
            if (_credentialCache != null)
            {
                request.Credentials = _credentialCache;
            }

            try
            {
                var response = (HttpWebResponse)request.GetResponse();
                result.Status = (int)response.StatusCode;
                result.TransferLength = ResponseBytes(response);
            }
            catch (WebException we)
            {
                var response = we.Response as HttpWebResponse;
                if (response != null)
                {
                    result.Status = (int)response.StatusCode;
                    result.TransferLength = ResponseBytes(response);
                }
            }

            sw.Stop();
            result.ElapsedMilliseconds = sw.ElapsedMilliseconds;
            result.End = DateTime.Now;

            return result;
        }
Ejemplo n.º 8
0
        static void PrintResults(HttpSettings setting, IEnumerable <HttpResult> results)
        {
            if (setting == null)
            {
                throw new ArgumentNullException("setting");
            }

            if (results == null)
            {
                throw new ArgumentNullException("results");
            }

            var totalElapsed     = results.Sum(r => r.ElapsedMilliseconds);
            var totalTransferred = results.Sum(r => r.TransferLength);
            var totalTime        = (results.Max(r => r.End) - results.Min(r => r.Start)).TotalMilliseconds;

            Console.WriteLine("Concurrent level:\t{0:N0}", setting.Concurrent);
            Console.WriteLine("Time taken for tests:\t{0:F3} seconds", totalTime / 1000);

            Console.WriteLine("Complete requests:\t{0:N0}", results.Count(r => r.Status == 200));
            Console.WriteLine("Failed requests:\t{0:N0}", results.Count(r => r.Status != 200));
            Console.WriteLine("Total transferred:\t{0:N0} bytes", results.Sum(r => r.TransferLength));
            var requestPerSec = 1000.0 / ((double)totalTime / results.Count());
            var timePerSec    = (double)totalTime / results.Count();

            Console.WriteLine("Request per second:\t{0:F3} (#/sec)", requestPerSec);
            Console.WriteLine("Time per request:\t{0:F3} [ms]", timePerSec);
            Console.WriteLine("Transfer rate:  \t{0:F2} [Kbytes/sec] received", ((double)totalTransferred / 1024) / ((double)totalTime / 1000.0));

            Console.WriteLine("");
            var elapseds = (from r in results
                            group r by r.ElapsedMilliseconds
                            into timeGroup
                            select new
            {
                ElapsedMilliseconds = timeGroup.Key,
                Percent = (double)timeGroup.Count() / setting.Times
            } as dynamic
                            ).OrderBy(e => e.ElapsedMilliseconds);

            Console.WriteLine(" fastest:\t{0} ms", elapseds.Min(e => (long)e.ElapsedMilliseconds));
            Console.WriteLine(" average:\t{0} ms", (long)elapseds.Average(e => (long)e.ElapsedMilliseconds));
            Console.WriteLine(" longest:\t{0} ms", elapseds.Max(e => (long)e.ElapsedMilliseconds));
            Console.WriteLine("");
            Console.WriteLine("Percentage of the requests (ms)");
            Console.WriteLine("");
            var percents = new List <dynamic>();

            percents.AddRange(elapseds);
            if (elapseds.Count() > 10)
            {
                var totalPercents = 0.0;
                var sectionPoint  = 0.5;
                percents.Clear();
                foreach (var elapsed in elapseds)
                {
                    totalPercents += elapsed.Percent;
                    if (totalPercents >= sectionPoint || elapsed == elapseds.Last())
                    {
                        dynamic p = new ExpandoObject();
                        p.ElapsedMilliseconds = elapsed.ElapsedMilliseconds;
                        p.Percent             = totalPercents;

                        percents.Add(p);
                        sectionPoint += 0.05;
                    }
                }
            }


            foreach (var elapsed in percents.OrderBy(e => e.ElapsedMilliseconds))
            {
                Console.WriteLine("  {0:0#.#0} %:\t{1} ms", elapsed.Percent * 100, elapsed.ElapsedMilliseconds);
            }

#if DEBUG
            var managedThreads = results.Select(r => r.ManagedThreadId)
                                 .Distinct()
                                 .OrderBy(t => t)
                                 .Select(t => t.ToString());
            var ids = string.Join(", ", managedThreads);
            //if (ids.Length > 80)
            //    ids = ids.Substring(0, 80) + "...";

            Console.WriteLine("");
            Console.WriteLine("Managed Threads:\t{0}\n {1}", managedThreads.Count(), ids);
#endif
        }
Ejemplo n.º 9
0
 static void PrintHelp()
 {
     Console.WriteLine("Usage: hb [options] [http://]hostname[:port]/path");
     Console.WriteLine("Options are:");
     Console.WriteLine(HttpSettings.GetHelp());
 }