现在的位置: 首页 > 综合 > 正文

测试网站负载的代码,做个记录[原]

2013年06月25日 ⁄ 综合 ⁄ 共 4220字 ⁄ 字号 评论关闭
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Diagnostics;
using System.Collections;
using System.IO;
using System.Net;

namespace Multithreading.Console
{
    public class MainClass
    {
        /// <summary>
        /// 设置在这个函数里
        /// </summary>
        private void Settings()
        {
            URL_DIC.Add("http://localhost:29649/Default.aspx", 50);
            URL_DIC.Add("http://localhost:29649/Default3.aspx", 10);
        }
        /// <summary>
        /// 启动该函数
        /// </summary>
        public void Start()
        {
            this.Settings();

            this.Init();

            this.Main();
        }
        /// <summary>
        /// 更改展示在这个函数里
        /// </summary>
        private void InitThreadFunc()
        {
            for (int index = 0; index < 100; index++)
            {
                //延迟500ms
                System.Threading.Thread.Sleep(500);
                Stopwatch stopwatch = new Stopwatch();
                try
                {
                    //stopwatch.Start();
                    string result = DownloadFromUrl(Url());
                    //stopwatch.Stop();
                    //System.Console.WriteLine(Thread.CurrentThread.Name + "耗时:" + stopwatch.ElapsedMilliseconds);
                    System.Console.Write(".");
                }
                catch (Exception ex)
                {
                    //stopwatch.Stop();
                    System.Console.Write("*");
                    //System.Console.WriteLine("-------" + Thread.CurrentThread.Name + "耗时:" + stopwatch.ElapsedMilliseconds);
                }
            }
        }

        /// <summary>
        /// 下载超时时间,单位ms(毫秒)
        /// </summary>
        int DOWNLOAD_TIMEOUT=1000;

        /// <summary>
        /// 最大线程数,并发量
        /// </summary>
        int MAX_THREAD = 100;

        /// <summary>
        /// 每个线程下载的数量
        /// </summary>
        int EVERY_THREAD_DOWNLOAD_COUNT=100;

        int MAX_DENOMINATOR=10000;

        private void Init()
        {
            int max = 0;
            foreach (KeyValuePair<string, int> item in URL_DIC)
            {
                max += item.Value;
            }

            int n = 0;
            foreach (KeyValuePair<string, int> item in URL_DIC)
            {
                int length = (int)(((decimal)item.Value) / ((decimal)max) * MAX_DENOMINATOR);
                for (int index = 0; index < length; index++)
                {
                    dic.Add(n, item.Key);
                    n++;
                }
            }
            MAX_DENOMINATOR = dic.Count;
        }

        /// <summary>
        /// 测试访问的URL第一个参数为URL,第二个参数为所占的比例
        /// </summary>
        Dictionary<string,int> URL_DIC=new Dictionary<string,int>();

        private List<Thread> threads = new List<Thread>();  
        Dictionary<int,string> dic=new Dictionary<int,string>();

       
        public void Main()
        {
            for (int index = 0; index < MAX_THREAD; index++)
            {
                Thread thread = new Thread(new ThreadStart(InitThreadFunc));
                thread.Name = "thread" + index;
                threads.Add(thread);
                thread.Start();
            }
            while (threads.Count > 0)
            {
                foreach (Thread thread in threads)
                {
                    if (thread.Join(1000))
                    {
                        threads.Remove(thread);
                        break;
                    }
                }
            }
        }
        /// <summary>
        /// 此函数随机取地址需要改进一下
        /// </summary>
        /// <returns></returns>
        private string Url()
        {
            Random random = new Random();
            return dic[random.Next(0, MAX_DENOMINATOR)];
        }

        public string DownloadFromUrl(string url)
        {
            try
            {
                if (!url.Contains("http://"))
                { url = "http://" + url; }
                url.TrimEnd(new char[] { '\\', '/' });
                HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
                request.Timeout = DOWNLOAD_TIMEOUT;
                HttpWebResponse response = request.GetResponse() as HttpWebResponse;
                System.IO.Stream responseStream = response.GetResponseStream();
                System.IO.StreamReader reader = new System.IO.StreamReader(responseStream, Encoding.UTF8);
                string srcString = reader.ReadToEnd();

                return srcString;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

    }
}

抱歉!评论已关闭.