C# 监控测试算率测试

 

using System;

using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using System.Diagnostics;
using System.Threading;


namespace Calculate_V1._00
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            StartPosition = FormStartPosition.CenterParent;
            this.FormBorderStyle = FormBorderStyle.None;     //设置窗体为无边框样式
        }


        public void ReadCfg(string CFGName)
        {
            FileStream fs = new FileStream(CFGName,FileMode.Open,FileAccess.Read);
            StreamReader sr = new StreamReader(fs, Encoding.Default);
            string[] str = { "TestTimer", "TestRunFileName", "TestExeFileName", "CreateLogDirectory", "StandardValues" };
            try
            {
                string Temp = string.Empty;
                while ((Temp = sr.ReadLine()) != null)
                {
                    string[] Array = Temp.Split(new string[] { "=" }, StringSplitOptions.RemoveEmptyEntries);
                    if (Array[0].Trim() == str[0].Trim()) TestTimer = Convert.ToInt32(Array[1].Trim());
                    else if (Array[0].Trim() == str[1].Trim()) TestRunFileName = Array[1].ToString().Trim();
                    else if (Array[0].Trim() == str[2].Trim()) TestExeFileName = Array[1].ToString().Trim();
                    else if (Array[0].Trim() == str[3].Trim()) CreateLogDirectory = Array[1].ToString().Trim();
                    else if (Array[0].Trim() == str[4].Trim()) StandardValues = Convert.ToDouble(Array[1].Trim()); 
                }
            }
            catch(Exception ex)
            {
                MessageBox.Show("配置读取错误:"+ex.Message);
            }
            finally
            {
                sr.Close();
                fs.Close();
            }


        }
        int TestTimer = 0;
        double StandardValues = 0;
        string TestRunFileName = string.Empty;
        string TestExeFileName = string.Empty;
        string CreateLogDirectory = string.Empty;
        List<string> TextFileName = new List<string>();
        private void Form1_Load(object sender, EventArgs e)
        {
            System.Diagnostics.Process[] myProcesses=System.Diagnostics.Process.GetProcessesByName(@"Calculate V1.00");
            if(myProcesses.Length>1)
            {
                MessageBox.Show("程式已启动");
                Application.Exit();
            }
            ReadCfg("Config.ini");
            Thread thread1 = new Thread(new ThreadStart(CallBath));
            thread1.Start();
            //CallBath();//调用批处理
            timer1.Enabled = true;
        }


        public void CallBath()//调用批处理
        {
            string targetDir = string.Empty;
            targetDir = System.IO.Directory.GetCurrentDirectory() + @"\";
            Process proc = null;
            try
            {
                proc = new Process();
                proc.StartInfo.WorkingDirectory = targetDir;
                proc.StartInfo.FileName = TestRunFileName;
                //proc.StartInfo.Arguments = string.Format("10");//this is argument
                //proc.StartInfo.CreateNoWindow = true;
                //proc.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;//这里设置DOS窗口不显示,经实践可行
                proc.Start();
                proc.WaitForExit();
            }
            catch(Exception ex)
            {
                MessageBox.Show("批处理执行出错"+ex.Message+ex.StackTrace.ToString());
                Application.Exit();
            }
        }


        public bool IsOk(string[] Key,string TextName)
        {
            foreach(string ss in Key)
            {
                if (ss == TextName) return false;
            }
            return true;
        }
        public void GetDirectoryTextFileName(string DirectoryPath)//读取当前目录测试LOG文件
        {
            try
            {
                DirectoryInfo dinfo = new DirectoryInfo(DirectoryPath);
                FileSystemInfo[] fsinfos = dinfo.GetFileSystemInfos();
                string[] FiltrationFile = { @"config.txt", @"dpools.txt", @"epools.txt", @"History.txt", @"License.txt", @"Readme!!!.txt" };
                foreach(FileSystemInfo fsinfo in fsinfos)
                {
                    if(fsinfo is FileInfo)
                    {
                        if(Path.GetExtension(fsinfo.Name)==@".txt"&&IsOk(FiltrationFile,fsinfo.Name.Trim()))
                        {
                            TextFileName.Add(fsinfo.Name.Trim());
                        }
                    }
                }
            }
            catch(Exception ex)
            {
                MessageBox.Show("目录读取错误:"+ex.Message);
            }
        }
        List<string> TestResuleLog = new List<string>();
        public bool CountTestCalculate()//计算测试LOG信息
        {
            string directory = string.Empty;
            directory = System.IO.Directory.GetCurrentDirectory();
            GetDirectoryTextFileName(directory);
            bool Flag = false;
            int StartRunNumber = 0;
            foreach(string LogName in TextFileName)
            {
                FileStream fs = new FileStream(LogName,FileMode.Open,FileAccess.Read);
                StreamReader sr = new StreamReader(fs,Encoding.Default);
                string Temp = string.Empty;
                int n = 1;
                double total = 0.0;
                while((Temp=sr.ReadLine())!=null)
                {
                    string[] Array = Temp.Split(new string[] { "-",":",","}, StringSplitOptions.RemoveEmptyEntries);
                    if(Array.Length>6)
                    {
                        if (Array[4].ToString().Trim() =="Total Speed")
                        {
                            if(StartRunNumber>6)
                            {
                                this.richTextBox1.AppendText(n.ToString() + ".Total Speed:" + Array[5] + "\n");
                                TestResuleLog.Add(n.ToString() + ".Total Speed:" + Array[5]);
                                string[] Valuse = Array[5].Trim().Split(new string[] { " " }, StringSplitOptions.RemoveEmptyEntries);
                                total += Convert.ToDouble(Valuse[0].Trim());
                                n++;
                            }
                            StartRunNumber++;
                        }   
                    }   
                }
                this.richTextBox1.AppendText("\n");
                this.richTextBox1.AppendText("\n");
                this.richTextBox1.AppendText("\n");
                if (total / (n - 1) >= StandardValues)
                {
                    Flag = true;
                    richTextBox1.ForeColor = Color.Green;
                }
                else
                {
                    Flag = false;
                    richTextBox1.ForeColor = Color.Red;
                }
                this.richTextBox1.AppendText("总测试 " + (n - 1) + "次\n测试总算率:" + total + "Mh/s\n平均测试算率:" + total / (n - 1) + "Mh/s\n测试结果:" + (Flag ? "PASS" : "Fail"));
                TestResuleLog.Add("总测试 " + (n - 1) + "次.\n测试总算率:" + total + "Mh/s.\n平均测试算率:" + total / (n - 1) + "Mh/s.\n测试结果:" + (Flag ? "PASS" : "Fail."));
                sr.Close();
                fs.Close();
            }
            return Flag;
        }


        public void BootShowResult(bool OkNo)//跳出最终测试结果
        {
            if(OkNo==true)
            {
                PASS a = new PASS();
                a.Show();   
            }
            else if(OkNo==false)
            {
                Fail a = new Fail();
                a.Show();
            }
            this.Hide();
        }
        bool TestValues = false;
        int ExitMainProgream = 0;


        public void CreateLog(List<string> Key)//生成测试LOG
        {
            FileStream fs = new FileStream(DateTime.Now.ToString("yyyyMMddHHmmss")+@".log",FileMode.Create,FileAccess.Write);
            StreamWriter sw = new StreamWriter(fs,Encoding.Default);
            try
            {
                foreach(string ss in Key)
                {
                    sw.WriteLine(ss);
                }
            }
            catch(Exception ex)
            {
                MessageBox.Show("日志写入出错:"+ex.Message);
            }
            finally
            {
                sw.Close();
                fs.Close();
            }
        }


        public void MoveTextLog()//移动测试生成的数据到LOG目录
        {
            string directory = string.Empty;
            directory = System.IO.Directory.GetCurrentDirectory();
            string dirPath = directory + @"\LOG";
            if (!Directory.Exists(dirPath))
            {
                DirectoryInfo directoryInfo = new DirectoryInfo(dirPath);
                directoryInfo.Create();
            }
            foreach(string ff in TextFileName)
            {
                string defaultPath = directory + @"\" + ff;
                FileInfo file = new FileInfo(defaultPath);
                if (File.Exists(dirPath + @"\" + ff)) File.Delete(dirPath + @"\" + ff);
                file.MoveTo(dirPath+@"\"+ff);
            }
        }


        public void KillProcess()//查找进程、结束进程
        {
            Process[] pro = Process.GetProcesses();//获取已开启的所有进程
            //遍历所有查找到的进程
            for(int i=0;i<pro.Length;i++)
            {
                //判断此进程是否是要查的进程
                if(pro[i].ProcessName.ToString().ToString()==TestExeFileName)
                {
                    pro[i].Kill();//结束进程
                }
            }
        }
        private void timer1_Tick(object sender, EventArgs e)
        {
            if(TestTimer!=0)
            {
                label1.Text = "剩余测试时间: " + TestTimer.ToString() + "S";
                TestTimer--;
            }
            else
            {
                KillProcess();//结束进程
                label1.Text = "测试完成计算测试结果中...";
                TestValues = CountTestCalculate();
                if (!TestValues) label1.ForeColor = Color.Red;
                label1.Text = "测试结果" + (TestValues ? "PASS" : "FAIL");
                CreateLog(TestResuleLog);//生成测试LOG
                MoveTextLog();//移动测试数据
                richTextBox1.Enabled = true;
                timer2.Enabled = true;
                timer1.Enabled = false;
            }
            
        }


        private void timer2_Tick(object sender, EventArgs e)
        {
            if(ExitMainProgream==5)
            {
                BootShowResult(TestValues);
                timer2.Enabled = false;
            }
            ExitMainProgream++;
        }
    }

}

 

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;


namespace Calculate_V1._00
{
    public partial class Fail : Form
    {
        public Fail()
        {
            InitializeComponent();
            StartPosition = FormStartPosition.CenterParent;
            this.FormBorderStyle = FormBorderStyle.None;     //设置窗体为无边框样式
            this.WindowState = FormWindowState.Maximized; 
        }


        private void Fail_Load(object sender, EventArgs e)
        {
            label1.Text = "FAIL";
            label1.Font = new Font("宋体", 350, FontStyle.Bold);
            //设置Lable1控件座标
            base.OnResize(e);
            int x = (int)(0.5 * (this.Width - label1.Width));
            int y = (int)(label1.Location.Y * 0.5);
            label1.Location = new System.Drawing.Point(x, y);


            //设置Button1控件座标
            base.OnResize(e);
            int x1 = (int)(0.5 * (this.Width - button1.Width));
            int y1 = button1.Location.Y;
            button1.Location = new System.Drawing.Point(x1, y1);




            label1.ForeColor = Color.Red;
            timer1.Enabled = true;
        }


        private void timer1_Tick(object sender, EventArgs e)
        {
            label1.ForeColor = Color.Black;
            timer2.Enabled = true;
            timer1.Enabled = false;
        }


        private void timer2_Tick(object sender, EventArgs e)
        {
            label1.ForeColor = Color.Red;
            timer1.Enabled = true;
            timer2.Enabled = false;
        }
        public void Shutdown()//启动关机
        {
            System.Diagnostics.Process myProcess = new System.Diagnostics.Process();
            myProcess.StartInfo.FileName = "cmd.exe";//启动cmd命令
            myProcess.StartInfo.UseShellExecute = false;//是否使用系统外壳程序启动进程
            myProcess.StartInfo.RedirectStandardInput = true;//是否从流中读取
            myProcess.StartInfo.RedirectStandardOutput = true;//是否写入流
            myProcess.StartInfo.RedirectStandardError = true;//是否将错误信息写入流
            myProcess.StartInfo.CreateNoWindow = true;//是否在新窗中启动进程
            myProcess.Start();//启动进程
            myProcess.StandardInput.WriteLine("shutdown -s -t 0");//执行关机命令
        }
        private void button1_Click(object sender, EventArgs e)
        {
            Shutdown();
            Application.Exit();
        }
    }

}

 

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;


namespace Calculate_V1._00
{
    public partial class PASS : Form
    {
        public PASS()
        {
            InitializeComponent();
            StartPosition = FormStartPosition.CenterParent;
            this.FormBorderStyle = FormBorderStyle.None;     //设置窗体为无边框样式
            this.WindowState = FormWindowState.Maximized;    //最大化窗体 
        }


        private void PASS_Load(object sender, EventArgs e)
        {
            label1.Text = "PASS";
            label1.Font = new Font("宋体",350,FontStyle.Bold);


            //设置Lable1控件座标
            base.OnResize(e);
            int x = (int)(0.5 * (this.Width - label1.Width));
            int y = (int)(label1.Location.Y * 0.5);
            label1.Location = new System.Drawing.Point(x, y);


            //设置Button1控件座标
            base.OnResize(e);
            int x1 = (int)(0.5 * (this.Width - button1.Width));
            int y1 = button1.Location.Y;
            button1.Location = new System.Drawing.Point(x1, y1);


            label1.ForeColor = Color.Green;
            timer1.Enabled = true;
        }


        private void timer1_Tick(object sender, EventArgs e)
        {
            this.label1.ForeColor = Color.Black;
            timer2.Enabled = true;
            timer1.Enabled = false;


        }


        private void timer2_Tick(object sender, EventArgs e)
        {
            this.label1.ForeColor = Color.Green;
            timer1.Enabled = true;
            timer2.Enabled = false;
        }


        private void PASS_Resize(object sender, EventArgs e)
        {
            //this.label1.Left = (this.Width - label1.Width) / 2;
        }


        public void Shutdown()
        {
            System.Diagnostics.Process myProcess = new System.Diagnostics.Process();
            myProcess.StartInfo.FileName = "cmd.exe";//启动cmd命令
            myProcess.StartInfo.UseShellExecute = false;//是否使用系统外壳程序启动进程
            myProcess.StartInfo.RedirectStandardInput = true;//是否从流中读取
            myProcess.StartInfo.RedirectStandardOutput = true;//是否写入流
            myProcess.StartInfo.RedirectStandardError = true;//是否将错误信息写入流
            myProcess.StartInfo.CreateNoWindow = true;//是否在新窗中启动进程
            myProcess.Start();//启动进程
            myProcess.StandardInput.WriteLine("shutdown -s -t 0");//执行关机命令
        }
        private void button1_Click(object sender, EventArgs e)
        {
            Shutdown();
            Application.Exit();
        }
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值