0

这是粘贴中的代码

我正在使用此代码对我的服务器进行压力测试。我试过了webpound X 1000 http://local_dev.com

该代码似乎可以工作并且很好。它<100 行,不难理解。简而言之,它使用 webclient 在阻塞和异步模式下访问站点一次(因此它旁边的 while 循环)。然后它反复点击网站计算有多少成功。

问题出在两台不同的机器上,我在异步模式下得到 600-850(使用 0 作为第一个参数),在单线程模式下随机得到 160-350。

运行此应用程序时,我的 CPU 处于空闲状态。根据任务管理器,我的 webapp 从未达到 1%,webpound 不超过 2%,而 cpu 总量从未超过 5%。

这里有什么问题?服务器是使用 fastcgi 的 nginx。我怀疑问题出在 webclient/C# 上。有什么我可以使用的具有相同想法的另一个工具?或者我如何修改它以便我可以每秒击中更多?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Diagnostics;
using System.IO;


namespace webpound
{
    class Program
    {
        static long count = 0;
        static void Main(string[] args)
        {
            var s = new Stopwatch();
            var wc = new WebClient();
            int time = 100, mthread=0;

            if (args.Count() == 0)
            {
                Console.WriteLine("[1 (for single) [ms time]] url");
                return;
            }
            string url = null;
            bool useSingle = false;
            if (args.Count() == 1)
            {
                url = args[0];;
            }
            else if (args.Count() == 2)
            {
                useSingle = args[0] == "1";
                url = args[1];
            }
            else if (args.Count() == 3)
            {
                useSingle = args[0] == "1";
                time = int.Parse(args[1]);
                url = args[2];
            }
            else
            {
                throw new Exception();
            }

            var uri = new Uri(url);
            wc.Headers["User-Agent"] = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:2.0.1) Gecko/20100101 Firefox/4.0.1";
            wc.DownloadStringCompleted += DownloadStringCompletedEventHandler;
            //wc.DownloadProgressChanged += DownloadProgressChangedEventHandler;
            wc.DownloadString(uri);
            wc.DownloadStringAsync(uri);

            while (System.Threading.Interlocked.Read(ref count)==0)
            {
                System.Threading.Thread.Sleep(1);
            }
            count = 0;
            WebException ex1 = null;
            s.Start();
            try
            {
                while (s.ElapsedMilliseconds < time)
                {
                    if (useSingle)
                    {
                        wc.DownloadString(url);
                        count++;
                    }
                    else
                    {
                        var wwc = new WebClient();
                        wwc.Headers["User-Agent"] = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:2.0.1) Gecko/20100101 Firefox/4.0.1";
                        wwc.DownloadStringCompleted += DownloadStringCompletedEventHandler;
                        var url2 = url + mthread.ToString();
                        wwc.DownloadStringAsync(new Uri(url2));
                        mthread++;
                        System.Threading.Thread.Sleep(1);
                    }
                }
            }
            catch(WebException ex)
            {
                ex1 = ex;
            }
            var mycount = count;
            s.Stop();
            if (ex1 == null)
            {
                Console.WriteLine("Finished with {0}/{1} in {2}", mycount,mthread, s.ElapsedMilliseconds);
            }
            else
            {
                var aa = new StreamReader(ex1.Response.GetResponseStream()).ReadToEnd();
                Console.WriteLine(ex1.Message);
                Console.WriteLine("Finished with {0}/{1} in {2}", mycount, mthread, s.ElapsedMilliseconds);
            }
        }

        static void DownloadProgressChangedEventHandler(object sender, DownloadProgressChangedEventArgs e)
        {
        }
        static void DownloadStringCompletedEventHandler(object sender, DownloadStringCompletedEventArgs e)
        {
            System.Threading.Interlocked.Increment(ref count);
        }
    }
}
4

0 回答 0