PressureTest.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Globalization;
  4. using System.IO;
  5. using System.Linq;
  6. using System.Net;
  7. using System.Net.Cache;
  8. using System.Text;
  9. using System.Threading;
  10. using System.Threading.Tasks;
  11. namespace Test
  12. {
  13. public class PressureTest
  14. {
  15. private static string url = "http://www.shvber.com/MessageWall/WriteMsg";
  16. private static string Send(string str)
  17. {
  18. ServicePointManager.DefaultConnectionLimit = 512;
  19. string respResult = null;
  20. HttpWebRequest req = null;
  21. try
  22. {
  23. GC.Collect();
  24. Uri uri = new Uri(url);
  25. ServicePoint spSite = ServicePointManager.FindServicePoint(uri);
  26. spSite.ConnectionLimit = 50;
  27. Stream streamSend = null;
  28. req = (HttpWebRequest)WebRequest.Create(url);
  29. req.Method = "POST";
  30. req.ContentType = "application/json";
  31. req.Accept = "*/*";
  32. req.Timeout = 5 * 60 * 60 * 1000;
  33. req.UserAgent = "Mozilla-Firefox";
  34. //这个在Post的时候,一定要加上,如果服务器返回错误,他还会继续再去请求,不会使用之前的错误数据,做返回数据
  35. req.ServicePoint.Expect100Continue = false;
  36. HttpRequestCachePolicy noCachePolicy = new HttpRequestCachePolicy(HttpRequestCacheLevel.NoCacheNoStore);
  37. req.CachePolicy = noCachePolicy;
  38. try
  39. {
  40. byte[] data = Encoding.UTF8.GetBytes(str);
  41. req.ContentLength = data.Length;
  42. streamSend = req.GetRequestStream();
  43. streamSend.Write(data, 0, data.Length);
  44. streamSend.Close();
  45. }
  46. catch (WebException wex)
  47. {
  48. streamSend?.Close();
  49. return null;
  50. }
  51. catch (Exception ex)
  52. {
  53. streamSend?.Close();
  54. return null;
  55. }
  56. try
  57. {
  58. Stream streamRequest = null;
  59. try
  60. {
  61. streamRequest = req.GetResponse().GetResponseStream();
  62. if (streamRequest != null)
  63. using (StreamReader reader = new StreamReader(streamRequest))
  64. {
  65. respResult = reader.ReadToEnd();
  66. }
  67. }
  68. finally
  69. {
  70. streamRequest?.Close();
  71. }
  72. }
  73. catch (WebException httpwex)
  74. {
  75. Stream streamRequest = null;
  76. try
  77. {
  78. streamRequest = httpwex.Response.GetResponseStream();
  79. if (streamRequest != null)
  80. using (StreamReader reader = new StreamReader(streamRequest))
  81. {
  82. respResult = reader.ReadToEnd();
  83. }
  84. }
  85. finally
  86. {
  87. streamRequest?.Close();
  88. }
  89. }
  90. catch (Exception httpex)
  91. {
  92. }
  93. finally
  94. {
  95. streamSend.Close();
  96. }
  97. }
  98. catch (Exception eee)
  99. {
  100. }
  101. finally
  102. {
  103. req?.Abort();
  104. }
  105. return respResult;
  106. }
  107. public static void Test()
  108. {
  109. for (int i = 0; i < 1000; i++)
  110. {
  111. ThreadPool.QueueUserWorkItem(new WaitCallback(state =>
  112. {
  113. string s = state as string;
  114. 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)}\\\"}}\"";
  115. Send(str);
  116. ThreadPool.GetMaxThreads(out var workerThreads, out var completionPortThreads);
  117. Console.WriteLine(DateTime.Now.ToString(CultureInfo.InvariantCulture) + "--- " +s +" ----workerThreads=" + workerThreads + "--CompletionPortThreads" + completionPortThreads);
  118. }),i);
  119. }
  120. }
  121. }
  122. }