Пример #1
0
        private void ProcessMessage(string message)
        {
            ConfigurationMessage config = JsonConvert.DeserializeObject<ConfigurationMessage>(message);

            // Check first whether the directories exist
            string dirPath = "Results/session_" + config.session.sessionNr.ToString() + "/" + config.session.browser;
            if (!Directory.Exists(dirPath))
            {
                Directory.CreateDirectory(dirPath);
            }

            Process browserProcess = null;
            switch (config.type)
            {
                case "start":

                    Console.WriteLine("Starting new session: Browser: " + config.session.browser + " Clients: " + config.session.clients + " Address: " + config.address);

                    if (config.session.wireshark && (config.session.simulationType == "interval"))
                    {
                        int duration = config.session.duration + ((config.session.clients + 1) * 3) + 30;

                        string wiresharkFilename = dirPath
                        + "/wireshark_client_"
                        + config.session.connection.Replace(" ", "") + "_"
                        + config.session.clients + "clients_"
                        + config.session.pps + "pps_"
                        + config.session.payloadSize + "bytes_"
                        + config.session.duration + "seconds.pcapng";
                        wiresharkProcess = new Process();
                        wiresharkProcess.StartInfo.FileName = "tshark.exe";
                        wiresharkProcess.StartInfo.Arguments = "-i " + wiresharkInterface + " -a duration:" + duration + " -w " + wiresharkFilename;
                        wiresharkProcess.Start();
                    }

                    browserProcess = new Process();
                    switch(config.session.browser)
                    {
                        case "firefox":
                            browserProcess.StartInfo.FileName = "firefox.exe";
                            browserProcess.StartInfo.Arguments = "-url " + config.address;
                            break;
                        case "chrome":
                            browserProcess.StartInfo.FileName = "chrome.exe";
                            browserProcess.StartInfo.Arguments = "--enable-high-resolution-time " + config.address;
                            break;
                    }
                    for (int i = 0; i < config.session.clients; ++i)
                    {
                        Thread.Sleep(3000);

                        browserProcess.Start();
                        if (i == 0)
                        {
                            browserID = browserProcess.Id;
                        }
                    }

                    break;
                case "stop":

                    ResourceEntry resourceEntry = new ResourceEntry();
                    Process[] browserProcesses = Process.GetProcessesByName(config.session.browser);
                    foreach (Process process in browserProcesses)
                    {
                        resourceEntry.cpu += process.TotalProcessorTime.TotalMilliseconds;
                        resourceEntry.peakWorkingSet += process.PeakWorkingSet64;
                        resourceEntry.peakPagedMemory += process.PeakPagedMemorySize64;
                        resourceEntry.peakVirtualMemory += process.PeakVirtualMemorySize64;
                    }

                    browserProcess = Process.GetProcessById(browserID);
                    if (browserProcess != null)
                    {
                        Console.WriteLine("Closing browser");

                        browserProcess.CloseMainWindow();
                        browserProcess.Close();

                        string resourcesFileName = string.Empty;
                        if (config.session.simulationType == "interval")
                        {
                            resourcesFileName = dirPath
                            + "/resources_client_"
                            + config.session.connection.Replace(" ", "") + "_"
                            + config.session.clients + "clients_"
                            + config.session.pps + "pps_"
                            + config.session.payloadSize + "bytes_"
                            + config.session.duration + "seconds.csv";
                        }
                        else if (config.session.simulationType == "pingpong")
                        {
                            resourcesFileName = dirPath
                            + "/resources_client_"
                            + config.session.connection.Replace(" ", "") + "_"
                            + config.session.packetCount + "packets_"
                            + config.session.payloadSize + "bytes.csv";
                        }

                        File.WriteAllText(resourcesFileName, resourceEntry.ToString());
                    }

                    if (wiresharkProcess != null)
                    {
                        Console.WriteLine("Closing Wireshark");
                        wiresharkProcess.Close();
                        wiresharkProcess = null;
                    }

                    break;
            }
        }