Ejemplo n.º 1
0
        private void Work(CaseGroup caseGroup)
        {
            string result = "";

            try
            {
                CaseFileData data = null;
                try
                {
                    data = LoadCaseData(caseGroup.FileName, caseGroup.FunList);
                }
                catch (Exception e)
                {
                    LogHelper.Logger.Error("发现文件格式错误", e);
                    return;
                }

                data.ID        = caseGroup.ID;
                data.UpStage   = caseGroup.UpStage;
                data.DownStage = caseGroup.DownStage;
                caseList.Add(data);

                if (caseGroup.LoopCount == 0)   //发送次数为0时,无限循环
                {
                    while (IsWork)
                    {
                        SendFastMsgReq(data, out result);
                        if (result == "error")
                        {
                            return;
                        }
                    }
                }
                else
                {
                    for (int i = 1; i < caseGroup.LoopCount; i++)
                    {
                        if (!IsWork)
                        {
                            return;
                        }

                        SendFastMsgReq(data, out result);
                        if (result == "error")
                        {
                            return;
                        }
                    }
                    StartEnable = true;
                }
            }
            catch (Exception error)
            {
                this.Dispatcher.Invoke(() => { MessageBox.Show($"{error.Message} \r\n {error.StackTrace}"); });
            }
        }
Ejemplo n.º 2
0
        /// <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";
            }
        }
Ejemplo n.º 3
0
        /// <summary>
        /// 加载案例数据
        /// </summary>
        /// <param name="filePath"></param>
        /// <param name="funList"></param>
        /// <returns></returns>
        public CaseFileData LoadCaseData(String filePath, List <CaseFunction> funList)
        {
            CaseFileData datas = new CaseFileData();

            try
            {
                if (!File.Exists(filePath))
                {
                    return(datas);
                }
                datas.FilePath = @filePath;
                XElement rootXML = XDocument.Load(filePath).Element("bizmsgs");
                var      funsXml = rootXML.Elements("function");
                foreach (var item in funsXml)
                {
                    string functionid = item.Attribute("functionid").Value;
                    string packettype = item.Attribute("packettype").Value;
                    var    CheckFun   = funList.FirstOrDefault(o => o.Check == 1 && o.FunID == functionid);
                    if (packettype.Equals("0") && CheckFun != null)
                    {
                        string       functionname = item.Attribute("functionname").Value;
                        CaseFunction fun          = new CaseFunction()
                        {
                            FunID   = functionid,
                            FunType = 0,
                            Check   = 1,
                            FunName = functionname
                        };
                        //发送次数
                        if (item.Attribute("times") != null)
                        {
                            string sendTimes  = item.Attribute("times").Value;
                            int    nSendTimes = 1;
                            int.TryParse(sendTimes, out nSendTimes);
                            fun.SendTimes = nSendTimes;
                        }
                        datas.Functions.Add(fun);
                        var filedsXml = item.Elements("field");

                        LDsdkDefineEx.LDFastMessageAdapter fastmsg = new LDFastMessageAdapter(functionid, 0);
                        LDRecordAdapter lpRecord = fastmsg.GetBizBodyRecord();

                        int iIndex = 0;
                        foreach (var filedItem in filedsXml)
                        {
                            string fieldid   = filedItem.Attribute("fieldid").Value;
                            string fieldname = filedItem.Attribute("fieldname").Value;
                            int    filedtype = lpRecord.GetFieldType(iIndex);

                            PARAMTYPE paramtype = PARAMTYPE.emFile;
                            string    value     = "";
                            if (filedItem.Attribute("value") != null)
                            {
                                value = filedItem.Attribute("value").Value;
                            }
                            if (filedItem.Attribute("paramtype") != null)
                            {
                                string strparamtype = filedItem.Attribute("paramtype").Value;
                                if ("file".Equals(strparamtype))
                                {
                                    paramtype = PARAMTYPE.emFile;
                                }
                            }
                            CaseFiled filed = new CaseFiled()
                            {
                                FiledTag   = int.Parse(fieldid),
                                FiledName  = fieldname,
                                FiledValue = value,
                                FiledType  = filedtype,
                                FiledIndex = iIndex,
                                ParamType  = paramtype
                            };
                            var fileElemt = filedItem.Element("file");
                            if (fileElemt != null && paramtype == PARAMTYPE.emFile)
                            {
                                filed.ColumnName = fieldname;
                                if (fileElemt.Attribute("columnname") != null)
                                {
                                    filed.ColumnName = fileElemt.Attribute("columnname").Value;
                                }

                                if (fileElemt.Attribute("filename") != null)
                                {
                                    filed.FileName = fileElemt.Attribute("filename").Value;
                                    CSVFile csvFile = null;
                                    if (File.Exists(filed.FileName) && !fun.AllCsvFile.TryGetValue(filed.FileName, out csvFile))
                                    {
                                        var data = CSVFileHelper.OpenCSV(filed.FileName);
                                        if (data != null)
                                        {
                                            csvFile          = new CSVFile();
                                            csvFile.FileName = filed.FiledName;
                                            csvFile.Table    = data;
                                            fun.AllCsvFile.Add(filed.FileName, csvFile);
                                        }
                                    }
                                    if (csvFile != null && string.IsNullOrWhiteSpace(csvFile.DefaultFieldName))
                                    {
                                        csvFile.DefaultFieldName = fieldname;
                                    }
                                }



                                filed.SelectRow = VALUETYPE.emSameLine;
                                if (fileElemt.Attribute("selectrow") != null)
                                {
                                    try
                                    {
                                        string SelectRow = fileElemt.Attribute("selectrow").Value;
                                        if ("sequence".Equals(SelectRow))
                                        {
                                            filed.SelectRow = VALUETYPE.emSequence;
                                        }
                                        else if (new Regex("same_line").Match(SelectRow).Success)
                                        {
                                            filed.SameLineColumn = SelectRow.Split('[')[1].Split(']')[0];
                                            filed.SelectRow      = VALUETYPE.emSameLine;
                                        }
                                        else if ("random".Equals(SelectRow))
                                        {
                                            //默认("random".Equals(SelectRow))
                                            filed.SelectRow = VALUETYPE.emRandom;
                                        }
                                    }
                                    catch (Exception)
                                    {
                                        //throw;
                                    }
                                }
                            }
                            fun.Fileds.Add(filed);
                            iIndex++;
                        }
                        fastmsg.FreeMsg();
                    }
                }

                return(datas);
            }
            catch (Exception error)
            {
                this.Dispatcher.Invoke(() => { MessageBox.Show($"{error.Message} \r\n {error.StackTrace}"); });
                return(datas);
            }
        }