/// <summary> /// 发送压力测试的请求包 /// </summary> /// <param name="file"></param> /// <param name="result"></param> private void SendFastMsgReq(CaseFileData file, out string result) { try { foreach (CaseFunction funitem in file.Functions) { int nSendTimes = 0; do { bool bNeedWait = (file.SendCount - file.RecCount) >= file.UpStage; if (bNeedWait) { file.ResetEvent.Reset(); if (!file.ResetEvent.WaitOne(10000)) { file.SendCount = file.RecCount; //每秒钟判断一次是否达到积压上限 continue; } } nSendTimes++; #region 发送包 LDsdkDefineEx.LDFastMessageAdapter fastmsg = new LDFastMessageAdapter(funitem.FunID, funitem.FunType); //读取储存默认值的Excel文档,给未复制的参数赋予默认值 foreach (var filedItem in funitem.Fileds) { string FiledValue = filedItem.FiledValue; int filedType = filedItem.FiledType; string filedName = filedItem.FiledName; FiledValue = GetParamValue(funitem, filedItem); //如果获取参数值失败,直接退出 if (FiledValue == "error") { result = "error"; return; } if (string.IsNullOrEmpty(FiledValue)) { //没有默认值,取默认值 if (!filedItem.HaveDefaultValue) { FiledValue = GetDefaultValue(filedItem.FiledName, FiledValue); filedItem.DefaultValue = FiledValue; filedItem.HaveDefaultValue = true; } FiledValue = filedItem.DefaultValue; } UtilTool.SetFastMsgValueById(fastmsg, filedType, FiledValue, filedItem.FiledTag); } int sendId = Interlocked.Increment(ref LastSendId); fastmsg.SetInt32(LDBizTagDefine.LDSdkTag.LDTAG_PACKETID, sendId); //赋值完毕,异步发送数据包 lock (ReqLock) { ReqAllList.Add(new ReqDTO() { SendID = sendId, CaseData = file }); } int nRet = (int)ConnectionManager.Instance.CurConnection.Connect.AsyncSend(funitem.FunID, fastmsg, 5000); if (nRet > 0) { Interlocked.Increment(ref file.SendCount); Interlocked.Increment(ref file.SendOkCount); } else { Interlocked.Increment(ref file.SendFailCount); } fastmsg.FreeMsg(); #endregion }while (funitem.SendTimes > nSendTimes); } result = "OK"; } catch (Exception error) { this.Dispatcher.Invoke(() => { MessageBox.Show($"{error.Message} \r\n {error.StackTrace}"); }); result = "error"; } }
/// <summary> /// 死循环处理队列里的应答包 /// </summary> private void DealRespPackage() { Task.Run( () => { while (true) { RespDataPackageModel RDP = new RespDataPackageModel(); if (RespDataPackageModel.DataQueue.TryDequeue(out RDP)) { Task.Run( () => { int hSend = RDP.hSend; LDFastMessageAdapter lpFastMsg = RDP.lpFastMsg; DateTime timeStamp = DateTime.Now; ReqDTO req = null; lock (ReqLock) { req = ReqAllList.FirstOrDefault(o => o.SendID == hSend); ReqAllList.Remove(req); } if (req != null && lpFastMsg != null) { Interlocked.Increment(ref req.CaseData.RecCount); //递增应答数量 if (req.CaseData.SendCount - req.CaseData.RecCount <= req.CaseData.DownStage) { req.CaseData.ResetEvent.Set(); } //功能号 string strFunCode = lpFastMsg.GetString(LDSdkTag.LDTAG_FUNCID); /***********************************郑建翔新加部分,用于计算应答频率***************************************/ lock (lockFlag) { //如果集合中不存这个功能号则添加 if (ReplyRateInfos.Count(i => i.functionId == strFunCode) == 0) { DateTime startTime = DateTime.Now; this.Dispatcher.Invoke(() => ReplyRateInfos.Add(new ReplyRateModel(strFunCode, startTime))); /*****************************接收到每个功能号的第一条应答时,保存应答信息**************************/ //文件保存路径 string savePath = Environment.CurrentDirectory + @"\ScriptOutPut.txt"; LogAnswerPackageInfo.OutPutResult(lpFastMsg, savePath); } else { //如果集合中已经存在这个功能号,开始更新并计算数据 ReplyRateModel replyInfo = ReplyRateInfos.FirstOrDefault(i => i.functionId == strFunCode); replyInfo.timeStamp = timeStamp; replyInfo.replyCount++; } } /*******************************************修改结束*********************************************************/ int iErrorNo = lpFastMsg.GetInt32(LDSdkTag.LDTAG_ERRORNO); // LogHelper.Logger.Info("系统错误号:" + iErrorNo); if (iErrorNo != 0) { //如果是中间件返回的错误 // Interlocked.Increment(ref req.CaseData.RecFailCount);//递增应答失败数量 } else { string strErrorCode = lpFastMsg.GetString(LDBizTag.LDBIZ_ERROR_CODE_STR); //LogHelper.Logger.Info("业务错误号:" + strErrorCode); Interlocked.Increment(ref req.CaseData.RecOkCount); //递增应答成功数量 if (!"0".Equals(strErrorCode)) { Interlocked.Increment(ref req.CaseData.RecFailCount); //递增应答失败数量 string strErrorInfo = lpFastMsg.GetString(LDBizTag.LDBIZ_ERROR_INFO_STR); string strErrorPrompt = lpFastMsg.GetString(LDBizTag.LDBIZ_ERROR_PROMPT_STR); string logInfo = string.Format("功能号[{0}],errorCode[{1}],errorinfo[{2}],errorprompt[{3}]", strFunCode, strErrorCode, strErrorInfo, strErrorPrompt); LogHelper.Logger.Error(logInfo); } //else //{ // Interlocked.Increment(ref req.CaseData.RecOkCount);//递增应答成功数量 //} } } if (lpFastMsg != null) { lpFastMsg.FreeMsg(); } } ); } //else //{ // pauseFlag.WaitOne(10); //} } } ); }