Esempio n. 1
0
        static void Main(string[] args)
        {
            var     cfg = File.ReadAllText(Path.Combine(Environment.CurrentDirectory, "config.json"));
            JObject jo  = (JObject)JsonConvert.DeserializeObject(cfg);

            var ip                 = CodeHelper.GetIP(jo["IP"].ToString());
            var port               = jo["PORT"].ToString();
            var pwd                = TokenHelper.Md5(jo["PASSWORD"].ToString());
            var numberOfRuns       = Convert.ToInt32(jo["NUMBEROFRUNS"].ToString()); // 最大运行次数
            var accountFile        = jo["ACCOUNTFILE"].ToString();
            var accountDetailFile  = jo["ACCOUNTDETAILFILE"].ToString();
            var timeTableId        = jo["TIMETABLEID"].ToString();
            var classId            = jo["CLASSID"].ToString();
            var accounts           = File.ReadAllLines(Path.Combine(Environment.CurrentDirectory, accountFile));
            var teachingPlanId     = jo["TEACHINGPLANID"].ToString();
            var resourceId         = jo["RESOURCEID"].ToString();
            var mqIp               = jo["MQIP"].ToString();
            var mqPort             = jo["MQPORT"].ToString();
            var mqUserName         = jo["MQUSERNAME"].ToString();
            var mqPassword         = jo["MQPASSWORD"].ToString();
            var serverExchangeName = jo["SERVEREXCHANGENAME"].ToString();

            //var maxDegreeOfParallelism = Convert.ToInt32(jo["CONCURRENT"].ToString());
            MQHelper.CreateMqConnection(mqIp, mqPort, mqUserName, mqPassword);
            ConcurrentDictionary <string, string> cd = new ConcurrentDictionary <string, string>();
            BlockingCollection <MData>            bc = new BlockingCollection <MData>();

            // 装置 AccountId 与 Account 的关系
            var accountDetails = File.ReadAllLines(Path.Combine(Environment.CurrentDirectory, accountDetailFile));

            foreach (var item in accountDetails)
            {
                var arr = item.Split('\t');
                cd.TryAdd(arr[1], arr[0]);
            }

            var r = new Random((int)DateTime.Now.ToFileTimeUtc());
            var accountIndexArray = new int[numberOfRuns];

            for (var i = 0; i < numberOfRuns; i++)
            {
                accountIndexArray[i] = r.Next(0, accounts.Length - 1);
            }

            var taskList      = new Task[numberOfRuns];
            var startDateTime = $"[{ DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss fff")}]";

            for (var i = 0; i < numberOfRuns; i++)
            {
                int k = i;
                taskList[i] = new Task(() =>
                {
                    var account = string.Empty;
                    account     = accounts[accountIndexArray[k]];
                    var url     = $"http://{ip}:{port}/oauth2/login.html?client_id=1&response_type=code&username={account}&password={pwd}";
                    SocketHttpHelper.SocketHttpPost(ip, port, url, out string status, out string data);
                    LogHelper.OutputDebugString($"[{k}]{status}, {data}");

                    if (status == SocketHttpHelper.HttpState.S200)
                    {
                        JObject __jo = JsonConvert.DeserializeObject <JObject>(data);
                        try
                        {
                            var t = __jo["code"].ToString() + __jo["state"].ToString();

                            var code             = __jo["code"].ToString(); // 取得CODE
                            var state            = __jo["state"].ToString();
                            string client_secret = "111";
                            string baseUrl       = "http://" + ip + ":" + port;
                            string tokenUrl      = baseUrl + "/oauth2/token";
                            string redirect_uri  = baseUrl + "/oauth2/getToken.do";
                            var refreshToken     = string.Empty;

                            var body = $"grant_type=authorization_code&client_id=1&client_secret={client_secret}&refresh_token={refreshToken}&username={account}&redirect_uri={redirect_uri.Replace("&", "%26%")}&code={code}&state={state}";
                            SocketHttpHelper.SocketHttpPost(ip, port, tokenUrl, body, out status, out data);
                            LogHelper.OutputDebugString($"[{k}]{status}, {data}");

                            // 保存有效数据,用于之后的并发测试
                            if (status == SocketHttpHelper.HttpState.S200)
                            {
                                var joAccessToken = JsonConvert.DeserializeObject <JObject>(data);
                                var accessToken   = joAccessToken["access_token"].ToString();
                                refreshToken      = joAccessToken["refresh_token"].ToString();
                                bc.Add(new MData
                                {
                                    AccessToken  = accessToken,
                                    RefreshToken = refreshToken,
                                    Code         = code,
                                    State        = state,
                                    Account      = account,
                                    AccountId    = cd[account],
                                    ClassId      = classId,
                                    TimeTableId  = timeTableId
                                });
                            }
                        }
                        catch { }
                    }
                });
            }

            Console.WriteLine($"[{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss fff")}]开始获取Code、AccessToken...");
            taskList.ToList().ForEach(a => a.Start());
            //Parallel.For(0, taskList.Length, new ParallelOptions { MaxDegreeOfParallelism = maxDegreeOfParallelism }, i => { taskList[i].Start(); });

            Task.WaitAll(taskList, 1000 * 60 * 10); // 最长等待10分钟
#if true
            #region 并发测试MQ(考勤)
            var bcTaskList   = bc.ToList();
            var mqKaoQinTask = new Task[bcTaskList.Count];

            for (var i = 0; i < bcTaskList.Count; i++)
            {
                var k = i;
                mqKaoQinTask[i] = new Task(() => {
                    var a    = bcTaskList[k];
                    var jObj = new JObject();
                    jObj.Add(new JProperty("studentAccountId", a.AccountId));
                    jObj.Add(new JProperty("timeTableId", a.TimeTableId));
                    jObj.Add(new JProperty("wifiName", "Wifi-liuzhenhua"));
                    jObj.Add(new JProperty("checkWorkStatus", "2"));
                    jObj.Add(new JProperty("checkWorkTime", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")));
                    jObj.Add(new JProperty("classId", a.ClassId));
                    jObj.Add(new JProperty("tocken", a.AccessToken));
                    var jsonPostData = jObj.ToString();
                    LogHelper.OutputDebugString($"MQ::{jsonPostData}");
                    MQHelper.sentMsgToMQqueue("checkwork", jsonPostData);
                });
            }
            ;
            Console.WriteLine($"[{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss fff")}]开始并发测试MQ(考勤)...");
            mqKaoQinTask.ToList().ForEach(a => a.Start());
            //Parallel.For(0, mqKaoQinTask.Length, new ParallelOptions { MaxDegreeOfParallelism = maxDegreeOfParallelism }, i => { mqKaoQinTask[i].Start(); });

            Task.WaitAll(mqKaoQinTask, 1000 * 60 * 5); // 最长等待5分钟
            #endregion

            #region 并发测试MQ(举手发言)
            var mqJuShouFaYanTask = new Task[bcTaskList.Count];
            for (var i = 0; i < bcTaskList.Count; i++)
            {
                var k = i;
                mqJuShouFaYanTask[i] = new Task(() => {
                    var a    = bcTaskList[k];
                    var jObj = new JObject();

                    jObj.Add(new JProperty("Course_ID", teachingPlanId));
                    jObj.Add(new JProperty("Time_Table_ID", a.TimeTableId));
                    jObj.Add(new JProperty("Account_ID", a.AccountId));
                    jObj.Add(new JProperty("accessToken", a.AccessToken));

                    var jsonPostData = jObj.ToString();
                    LogHelper.OutputDebugString($"MQ::{jsonPostData}");
                    MQHelper.sentMsgToMQqueue("student_speak", jObj.ToString());
                });
            }
            ;
            Console.WriteLine($"[{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss fff")}]开始并发测试MQ(举手发言)...");
            mqJuShouFaYanTask.ToList().ForEach(a => a.Start());
            //Parallel.For(0, mqJuShouFaYanTask.Length, new ParallelOptions { MaxDegreeOfParallelism = maxDegreeOfParallelism }, i => { mqJuShouFaYanTask[i].Start(); });

            Task.WaitAll(mqJuShouFaYanTask, 1000 * 60 * 5); // 最长等待5分钟
            #endregion

            #region 并发测试MQ(回答问题)
            var mqHuiDaWenTiTask = new Task[bcTaskList.Count];
            for (var i = 0; i < bcTaskList.Count; i++)
            {
                var k = i;
                mqHuiDaWenTiTask[i] = new Task(() => {
                    var a    = bcTaskList[k];
                    var jObj = new JObject();

                    jObj.Add(new JProperty("Time_Table_ID", a.TimeTableId));
                    jObj.Add(new JProperty("Resource_ID", resourceId));
                    jObj.Add(new JProperty("Course_ID", teachingPlanId));
                    jObj.Add(new JProperty("Account_ID", a.AccountId));
                    jObj.Add(new JProperty("Answer_Time", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")));
                    jObj.Add(new JProperty("type", "1"));
                    jObj.Add(new JProperty("accessToken", a.AccessToken));

                    var jsonPostData = jObj.ToString();
                    LogHelper.OutputDebugString($"MQ::{jsonPostData}");
                    string routingKey = "tea.question.courseId." + a.TimeTableId;
                    MQHelper.sentMsgToMQ(routingKey, jsonPostData, serverExchangeName);
                });
            }
            ;
            Console.WriteLine($"[{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss fff")}]开始并发测试MQ(回答问题)...");
            mqHuiDaWenTiTask.ToList().ForEach(a => a.Start());
            //Parallel.For(0, mqHuiDaWenTiTask.Length, new ParallelOptions { MaxDegreeOfParallelism = maxDegreeOfParallelism }, i => { mqHuiDaWenTiTask[i].Start(); });

            Task.WaitAll(mqHuiDaWenTiTask, 1000 * 60 * 5); // 最长等待5分钟
            #endregion
#endif
            var s = $"Run from {startDateTime} to [{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss fff")}], 总运行次数:{numberOfRuns}. All of task finished. Press enter to exit.";
            LogHelper.OutputDebugString(s);
            Console.WriteLine(s);
            Console.ReadLine();
            Environment.Exit(0);
        }
        static void Main(string[] args)
        {
            var     cfg = File.ReadAllText(Path.Combine(Environment.CurrentDirectory, "config.json"));
            JObject jo  = (JObject)JsonConvert.DeserializeObject(cfg);

            LogHelper.StartService();
            var accountIds  = RemoveEmptyLines(File.ReadAllLines(Path.Combine(Environment.CurrentDirectory, "accountId.txt")));
            var resourceIds = ParseResourceLines(RemoveEmptyLines(File.ReadAllLines(Path.Combine(Environment.CurrentDirectory, "resourceId.txt"))));
            var mqIp        = GetIP(jo["MQIP"].ToString());
            var mqPort      = jo["MQPORT"].ToString();
            var mqUserName  = jo["MQUSERNAME"].ToString();
            var mqPassword  = jo["MQPASSWORD"].ToString();
            var timeTableId = jo["TIMETABLEID"].ToString();
            var courseId    = jo["COURSEID"].ToString();

            var detailIds = RemoveEmptyLines(File.ReadAllLines(Path.Combine(Environment.CurrentDirectory, "detailIds.txt")));


            var connection = MQHelper.CreateMqConnection(mqIp, mqPort, mqUserName, mqPassword);

            IModel channel = connection.CreateModel();

            // 生成随机队列名称
            var queueName = channel.QueueDeclare().QueueName;

            var len     = accountIds.Length * resourceIds.Length; // 最大并发度
            var lstTask = new Task[len];
            var index   = 0;

            for (var m = 0; m < accountIds.Length; m++)
            {
                var aIndex = m;
                for (var n = 0; n < resourceIds.Length; n++)
                {
                    int k      = index;
                    int rIndex = n;
                    lstTask[k] = new Task(() =>
                    {
                        var j = new JObject();

                        var resource_detailIds = new string[detailIds.Length];//detailIds;
                        for (var c = 0; c < detailIds.Length; c++)
                        {
                            var h = c;
                            resource_detailIds[h] = detailIds[h];
                        }
                        var jArray = new JArray();
                        for (var i = 0; i < resource_detailIds.Length; i++)
                        {
                            var jItem = new JObject();
                            var w     = i;
                            jItem["Publish_Details_ID"] = resource_detailIds[w];// resourceIds[rIndex].ResourceDetailId;
                            jItem["StudentAnswer"]      = "B";
                            jItem["RightOrWrong"]       = 0;
                            jItem["Sort"] = w + 1;
                            jArray.Add(jItem);
                        }

                        j["AnswerList"]    = jArray;
                        j["ErrorNo"]       = "1";
                        j["Time_Table_ID"] = timeTableId; // "a9731c294ac7405dab551e69e93d6941";
                        j["Resource_ID"]   = resourceIds[rIndex].ResourceId;
                        j["Course_ID"]     = courseId;
                        j["Account_ID"]    = accountIds[aIndex];
                        j["Answer_Time"]   = "2020-4-8 16:41:07";
                        j["type"]          = "1";
                        j["accessToken"]   = "921a71d35fd7ea9aaa8c75185907d299";

                        var jsonPostData = j.ToString();
                        //LogHelper.OutputDebugString($"MQ::{jsonPostData}");
                        string routingKey      = "tea.question.courseId." + j["Time_Table_ID"].ToString();
                        var serverExchangeName = "server-topic-exchange";
                        //MQHelper.sentMsgToMQ(routingKey, channel, queueName, jsonPostData, serverExchangeName);
                        MQHelper.sentMsgToMQ(routingKey, jsonPostData, serverExchangeName);
                    });
                    index++;
                }
            }
            var ss = $"[{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss fff")}]Start.";

            LogHelper.Trace(ss);
            Console.WriteLine(ss);
            for (var i = 0; i < len; i++)
            {
                lstTask[i].Start();
            }
            Task.WaitAll(lstTask);

            var s = $"[{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss fff")}]Task finished. Total send mq={len}";

            LogHelper.Trace(s);
            Console.WriteLine(s);
            Console.ReadLine();
        }