• C#模拟进行压力测试


    using System;
    using System.Diagnostics;
    using System.IO;
    using System.Net;
    using System.Text;
    using System.Threading;
    using Newtonsoft.Json.Linq;
    
    namespace TestKaiPing
    {
        class Program
        {
            private static long _totalTimeCost = 0;
            private static int _endedConnenctionCount = 0;
            private static int _failedConnectionCount = 0;
            private static int _connectionCount = 0;
    
            static void Main(string[] args)
            {
                var connectionCount = 100000;
                var requestThread = new Thread(() => StartRequest(connectionCount)) {IsBackground = true};
                requestThread.Start();
    
                Console.WriteLine("恭喜,成功完成!");
                Console.ReadLine();
            }
    
           
            private static void SendRequest()
            {
                try
                {
                    var url =
                                "http://kpsso.kpedu.com/ajaxLogin?targetService=http://221.194.113.150/dsideal_yy/html/ypt/getLoginInfo.jsp&callback=loginFormState&userName=dscsjs1&password=123456";
                    var r = SendRequestByGet(url);
                    r = r.Replace("loginFormState(", "").Replace(")", "");
                    var o = JObject.Parse(r);
                    var ticket = o["ticket"];
    
                    url = "http://221.194.113.150/dsideal_yy/html/ypt/getLoginInfo.jsp?ticket=" + ticket;
                    r = SendRequestByGet(url);
    
                    IncreaseSuccessConnection();
                }
                catch (Exception)
                {
                    IncreaseFailedConnection();
                }
            }
            private static void Reset()
            {
                _failedConnectionCount = 0;
                _endedConnenctionCount = 0;
                _totalTimeCost = 0;
            }
    
            private static void IncreaseFailedConnection()
            {
                Interlocked.Increment(ref _failedConnectionCount);
                Console.WriteLine("失败个数:"+ _failedConnectionCount);
            }
    
            private static void IncreaseSuccessConnection()
            {
                Interlocked.Increment(ref _endedConnenctionCount);
                Console.WriteLine("成功个数:"+ _endedConnenctionCount);
            }
    
    
            private static void StartRequest(int connectionCount)
            {
                Reset();
                for (var i = 0; i < connectionCount; i++)
                {
                    ThreadPool.QueueUserWorkItem(u => SendRequest());
                }
            }
            public static string SendRequestByGet(string uri)
            {
                string fullhtml = null;
                while (true)
                {
                    try
                    {
                        var req = (HttpWebRequest)WebRequest.Create(uri);
                        req.Method = "GET";
                        req.ServicePoint.ConnectionLimit = int.MaxValue;
                        req.UserAgent = "Mozilla/5.0 (Windows NT 5.1; rv:5.0) Gecko/20100101 Firefox/5.0";
                        req.KeepAlive = true;
                        req.Timeout = 1000 * 10;
                        var resp = (HttpWebResponse)req.GetResponse();
                        if (resp.StatusCode != HttpStatusCode.OK) //如果服务器未响应,那么继续等待相应
                            continue;
                        var sr = new StreamReader(resp.GetResponseStream(), Encoding.UTF8);
                        fullhtml = sr.ReadToEnd().Trim();
                        resp.Close();
                        sr.Close();
                        break;
                    }
                    catch (WebException e)
                    {
                        e.StackTrace.ToString();
                        Trace.WriteLine(e.Message);
                        if (true)
                            continue;
                    }
                }
                return fullhtml;
            }
        }
    }
  • 相关阅读:
    oracle去除字符串中间的空格
    java代理模式
    js方法中的this
    js中访问对象的方法
    Hadoop学习笔记
    查看电脑硬件常用命令
    Ubuntu18.0.4配置Hadoop1.2.1环境
    Entwurfsmuster
    WEB Front-end Development Technology
    Objekt Orientierte Programmierung C++
  • 原文地址:https://www.cnblogs.com/littlehb/p/5867704.html
Copyright © 2020-2023  润新知