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

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
    {
        ///
        /// 设置在这个函数里
        ///

        private void Settings()
        {
            URL_DIC . Add( "http://localhost:29649/Default.aspx" , 50);
            URL_DIC . Add( "http://localhost:29649/Default3.aspx" , 10);
        }
        ///
        /// 启动该函数
        ///

        public void Start()
        {
            this . Settings();

            this . Init();

            this . Main();
        }
        ///
        /// 更改展示在这个函数里
        ///

        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);
                }
            }
        }

        ///
        /// 下载超时时间,单位ms(毫秒)
        ///

        int DOWNLOAD_TIMEOUT = 1000;

        ///
        /// 最大线程数,并发量
        ///

        int MAX_THREAD = 100;

        ///
        /// 每个线程下载的数量
        ///

        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;
        }

        ///
        /// 测试访问的URL第一个参数为URL,第二个参数为所占的比例
        ///

        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;
                    }
                }
            }
        }
        ///
        /// 此函数随机取地址需要改进一下
        ///

        ///
        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;
            }
        }

    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值