C#模拟进行压力测试
2024-09-19 00:28:24
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 = ;
private static int _endedConnenctionCount = ;
private static int _failedConnectionCount = ;
private static int _connectionCount = ; static void Main(string[] args)
{
var connectionCount = ;
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 = ;
_endedConnenctionCount = ;
_totalTimeCost = ;
} 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 = ; 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 = * ;
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;
}
}
}
最新文章
- 从零开始学 Java - 我放弃了 .NET ?
- JS组件系列——Bootstrap Table 表格行拖拽(二:多行拖拽)
- sql逻辑执行顺序
- Codeforces 676C Vasya and String(尺取法)
- Fiddler---前端利器
- 敏捷软件开发 原则 模式 与实践 - OCP原则
- 在rails下新建表
- sopcinfo路径改变,nios工程该怎么办?
- MongoDb 2.4 beta新特性——全文索引
- log4net 将日志写入数据库
- 基于visual Studio2013解决算法导论之023队列实现(基于数组)
- VMWARE虚拟机无法访问的三种方法分析
- As input tri-stated
- IDEA 2 的注册码
- ssm简单搭建
- [Swift]LeetCode295. 数据流的中位数 | Find Median from Data Stream
- IOS解析XML文件
- Ubuntu16.04+cuda8.0rc+opencv3.1.0+caffe+Theano+torch7搭建教程
- JavaScript -基础- 函数与对象(三)数组对象
- VMware下ubuntu与Windows实现文件共享的方法 (转)