| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131 |
- using System;
- using System.Collections.Generic;
- using System.Globalization;
- using System.IO;
- using System.Linq;
- using System.Net;
- using System.Net.Cache;
- using System.Text;
- using System.Threading;
- using System.Threading.Tasks;
- namespace Test
- {
- public class PressureTest
- {
- private static string url = "http://www.shvber.com/MessageWall/WriteMsg";
- private static string Send(string str)
- {
- ServicePointManager.DefaultConnectionLimit = 512;
- string respResult = null;
- HttpWebRequest req = null;
- try
- {
- GC.Collect();
- Uri uri = new Uri(url);
- ServicePoint spSite = ServicePointManager.FindServicePoint(uri);
- spSite.ConnectionLimit = 50;
- Stream streamSend = null;
- req = (HttpWebRequest)WebRequest.Create(url);
- req.Method = "POST";
- req.ContentType = "application/json";
- req.Accept = "*/*";
- req.Timeout = 5 * 60 * 60 * 1000;
- req.UserAgent = "Mozilla-Firefox";
- //这个在Post的时候,一定要加上,如果服务器返回错误,他还会继续再去请求,不会使用之前的错误数据,做返回数据
- req.ServicePoint.Expect100Continue = false;
- HttpRequestCachePolicy noCachePolicy = new HttpRequestCachePolicy(HttpRequestCacheLevel.NoCacheNoStore);
- req.CachePolicy = noCachePolicy;
- try
- {
-
- byte[] data = Encoding.UTF8.GetBytes(str);
- req.ContentLength = data.Length;
- streamSend = req.GetRequestStream();
- streamSend.Write(data, 0, data.Length);
- streamSend.Close();
- }
- catch (WebException wex)
- {
- streamSend?.Close();
- return null;
- }
- catch (Exception ex)
- {
- streamSend?.Close();
- return null;
- }
- try
- {
- Stream streamRequest = null;
- try
- {
- streamRequest = req.GetResponse().GetResponseStream();
- if (streamRequest != null)
- using (StreamReader reader = new StreamReader(streamRequest))
- {
- respResult = reader.ReadToEnd();
- }
- }
- finally
- {
- streamRequest?.Close();
- }
- }
- catch (WebException httpwex)
- {
- Stream streamRequest = null;
- try
- {
- streamRequest = httpwex.Response.GetResponseStream();
- if (streamRequest != null)
- using (StreamReader reader = new StreamReader(streamRequest))
- {
- respResult = reader.ReadToEnd();
- }
- }
- finally
- {
- streamRequest?.Close();
- }
- }
- catch (Exception httpex)
- {
- }
- finally
- {
- streamSend.Close();
- }
- }
- catch (Exception eee)
- {
- }
- finally
- {
- req?.Abort();
- }
- return respResult;
- }
- public static void Test()
- {
-
- for (int i = 0; i < 1000; i++)
- {
- ThreadPool.QueueUserWorkItem(new WaitCallback(state =>
- {
- string s = state as string;
- var str =$"\"{{\\\"Avatar\\\"=\\\"/Content/img/MsgWall/r6.png\\\",\\\"Mobile\\\"=\\\"13347997190\\\",\\\"NickName\\\"=\\\"压力测试{s}\\\",\\\"MsgContent\\\"=\\\"压力测试压力测试压力测试压力测试压力测试压力测试压力测试压力测试压力测试压力测试压力测试压力测试压力测试压力测试压力测试压力测试压力测试压力测试压力测试压力测试压力测试压力测试压力测试压力测试压力测试压力测试压力测试压力测试压力测试压力测试\\\",\\\"Gender\\\"=\\\"{new Random().Next(0, 2)}\\\",\\\"MsgType\\\"=\\\"{new Random().Next(0, 1)}\\\"}}\"";
-
- Send(str);
- ThreadPool.GetMaxThreads(out var workerThreads, out var completionPortThreads);
- Console.WriteLine(DateTime.Now.ToString(CultureInfo.InvariantCulture) + "--- " +s +" ----workerThreads=" + workerThreads + "--CompletionPortThreads" + completionPortThreads);
- }),i);
-
- }
-
- }
- }
- }
|