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.Collections;
using System.Web;
using System.Threading;
using System.Diagnostics;
namespace ManyCardCaculateTest_V1._00
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
StartPosition = FormStartPosition.CenterParent;
this.FormBorderStyle = FormBorderStyle.None; //设置窗体为无边框样式
}
int TestTimer = 0;//设定时间
string TestRunFileName = string.Empty;//启动监控程式
string TestExeFileName = string.Empty;//关闭进程名称
int TestCardNumber = 0;//测试矿卡数量
int CancelLoop = 0;//取消开始计数多少轮
double StandardValues = 0.0;//读取矿卡平均测试算率
string CfgName = @"Config.ini";
private void Form1_Load(object sender, EventArgs e)
{
System.Diagnostics.Process[] myProcesses = System.Diagnostics.Process.GetProcessesByName(@"ManyCardCaculateTest V1.00");
if (myProcesses.Length > 1)
{
MessageBox.Show("程式已启动");
Application.Exit();
}
ReaderCfg(CfgName);//读取配置信息
ReadLogText(OldLogTextName);//读取目录下所有文本信息
if (OldLogTextName.Count >= 1)
MoveTextLog(OldLogTextName);
InitialConsoleSet();//初始化
Thread thread1 = new Thread(new ThreadStart(CallBath));
thread1.Start();//调用批处理
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 void ReaderCfg(string CfgFileName)//获取配置信息
{
string[] KeyWord = { "TestTimer", "TestRunFileName", "TestExeFileName", "TestCardNumber", "CancelLoop", "StandardValues" };
FileStream fs = new FileStream(CfgFileName,FileMode.Open,FileAccess.Read);
StreamReader sr = new StreamReader(fs,Encoding.Default);//默认读取文件可识别中文
try
{
string Temp = string.Empty;
while((Temp=sr.ReadLine())!=null)
{
string[] Array = Temp.Split(new string[] { "=" }, StringSplitOptions.RemoveEmptyEntries);//截取字符串
if (Array[0].Trim() == KeyWord[0]) TestTimer = Convert.ToInt32(Array[1].Trim());
else if (Array[0].Trim() == KeyWord[1]) TestRunFileName = Array[1].Trim();
else if (Array[0].Trim() == KeyWord[2]) TestExeFileName = Array[1].Trim();
else if (Array[0].Trim() == KeyWord[3]) TestCardNumber = Convert.ToInt32(Array[1].Trim());
else if (Array[0].Trim() == KeyWord[4]) CancelLoop = Convert.ToInt32(Array[1].Trim());
else if (Array[0].Trim() == KeyWord[5]) StandardValues = Convert.ToDouble(Array[1].Trim());
}
}
catch(Exception ex)
{
MessageBox.Show("配置文件读取错误:"+ex.Message);
Application.Exit();
}
finally
{
sr.Close();
fs.Close();
}
}
public bool IsOk(string[] Key,string TextName)
{
foreach(string ss in Key)
{
if (ss == TextName) return false;
}
return true;
}
List<string> OldLogTextName = new List<string>();
public void ReadLogText(List<string> Key)//移动当前目录下的txt文件
{
try
{
string directory = string.Empty;
directory = System.IO.Directory.GetCurrentDirectory();
DirectoryInfo dinfo = new DirectoryInfo(directory);
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()))
{
Key.Add(fsinfo.Name.Trim());
}
}
}
}
catch(Exception ex)
{
MessageBox.Show("目录文件读取错误:"+ex.Message);
}
}
public void MoveTextLog(List<string> Key)//移动测试生成的数据到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 Key)
{
string defaultPath = directory + @"\" + ff;
FileInfo file = new FileInfo(defaultPath);
if (File.Exists(dirPath + @"\" + ff)) File.Delete(dirPath + @"\" + ff);
file.MoveTo(dirPath+@"\"+ff);
}
}
public void SetConsoleColorFone(Label Key,Color Key1,string Key2)
{
Key.Text = Key2;
Key.BackColor = Key1;
}
List<Label> kk = new List<Label>();//填入控件数组
public void InitialConsoleSet()//初始化控件设置
{
kk.Add(this.label2);
kk.Add(this.label3);
kk.Add(this.label4);
kk.Add(this.label5);
kk.Add(this.label6);
kk.Add(this.label7);
kk.Add(this.label8);
kk.Add(this.label9);
kk.Add(this.label10);
int n = 0;
foreach(Label ss in kk)
{
if (n < TestCardNumber) SetConsoleColorFone(ss, Color.GreenYellow, "测试中...");
else SetConsoleColorFone(ss,Color.Red,"N/A");
n++;
}
}
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();//结束进程
}
}
}
List<string> NowTextName = new List<string>();
List<string> CreateLogStr = new List<string>();
//List<string> ShowLogStr = new List<string>();
double []OneCardTotalCount={0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0};
int TestTatalLoop = 1;
public bool CountTestCalculate()//计算测试LOG信息
{
bool Flag = false;
string[] GPUSTR = { "GPU0", "GPU1", "GPU2", "GPU3", "GPU4", "GPU5", "GPU6", "GPU7", "GPU8" };
ReadLogText(NowTextName);
foreach(string LogName in NowTextName)
{
FileStream fs = new FileStream(LogName,FileMode.Open,FileAccess.Read);
StreamReader sr = new StreamReader(fs,Encoding.Default);
string Temp = string.Empty;
int nn11 = 1;
try
{
while ((Temp = sr.ReadLine()) != null)
{
string[] Array = Temp.Split(new string[] { " ", ":" }, StringSplitOptions.RemoveEmptyEntries);
if (Array.Length>=7&&Array[5].Trim() == "ETH")
{
string[] nnn = Array[6].Trim().Split(new string[] { " " }, StringSplitOptions.RemoveEmptyEntries);
if (nnn[0] == "GPU0")
{
if (nn11 > CancelLoop)
{
CreateLogStr.Add(TestTatalLoop.ToString() + "." + Array[6].Trim());
this.richTextBox1.AppendText(TestTatalLoop.ToString() + @">." + Array[6].Trim() + "\n");
string[] Array1 = Array[6].Trim().Split(new string[] { " Mh/s" }, StringSplitOptions.RemoveEmptyEntries);
foreach (string yy in Array1)
{
string[] GPUVALUES = yy.Trim().Split(new string[] { " ", "," }, StringSplitOptions.RemoveEmptyEntries);
int n = 0;
foreach (string gg in GPUSTR)
{
string yyy = string.Empty;
yyy = GPUVALUES[0].Trim();
if (gg == GPUVALUES[0].Trim())
OneCardTotalCount[n] += Convert.ToDouble(GPUVALUES[1].Trim());
n++;
}
}
TestTatalLoop++;
}
nn11++;
}
}
}
Flag = true;
}
catch(Exception ex)
{
MessageBox.Show("测试数据统计出错:"+ex.Message);
return false;
}
finally
{
sr.Close();
fs.Close();
}
}
this.richTextBox1.AppendText("\n");
this.richTextBox1.AppendText("\n");
this.richTextBox1.AppendText("\n");
double totalcountV = 0.0;
for (int n = 0; n < TestCardNumber;n++ )
{
totalcountV += OneCardTotalCount[n];
if (OneCardTotalCount[n] / (TestTatalLoop - 1) >= StandardValues)
{
string str1 = string.Format("{0:F}", OneCardTotalCount[n] / (TestTatalLoop - 1)).Trim();
kk[n].Text = "平均算率:" + str1 + @"Mh/s" + " 结果:PASS";
kk[n].BackColor = Color.Green;
}
else
{
string str1 = string.Format("{0:F}", OneCardTotalCount[n] / (TestTatalLoop - 1)).Trim();
kk[n].Text = "平均算率:" + str1 + @"Mh/s" + " 结果:FAIL";
kk[n].BackColor = Color.Red;
Flag = false;
}
}
richTextBox1.AppendText("矿卡测试最终List:测试矿卡=" + TestCardNumber + "张," + "标准单卡平均算率=" + StandardValues.ToString().Trim() + @"Mh/s,实际测试总算率=" + string.Format("{0:F}", totalcountV) + @"Mh/s,单卡平均算率=" + string.Format("{0:F}", ((totalcountV / TestCardNumber) / (TestTatalLoop - 1))).Trim() + @"Mh/s,测试最终结果=" + (Flag ? "PASS" : "FAIL") + "\n");
richTextBox1.AppendText("单矿卡测试数据:\n");
int nn = 0;
foreach(Label ss in kk)
{
if (nn < TestCardNumber)
{
richTextBox1.AppendText(" "+GPUSTR[nn] + ss.Text.Trim() + "\n");
//TotalValuesStr += GPUSTR[nn]+ ss.Text.Trim() + ",";
nn++;
}
else
break;
}
//richTextBox1.AppendText(TotalValuesStr);
return Flag;
}
bool TestValues = false;
private void timer1_Tick(object sender, EventArgs e)
{
if (TestTimer != 0)
{
label1.Font = new Font("宋体", 18, FontStyle.Bold);
label1.BackColor = Color.ForestGreen;
label1.Text = "剩余测试时间: " + TestTimer.ToString() + "S";
TestTimer--;
}
else
{
KillProcess();//结束进程
label1.Text = "测试完成计算测试结果中...";
TestValues=CountTestCalculate();
if (!TestValues) label1.ForeColor = Color.Red;
label1.Text = TestCardNumber.ToString()+"张矿卡测试结果:" + (TestValues ? "PASS" : "FAIL");
if (TestValues)
{
timer2.Enabled = true;
}
else
{
button1.Enabled = true;
}
richTextBox1.Enabled = true;
timer1.Enabled = false;
}
}
public void CreateWriteTestLog(string FileName)
{
FileStream fs = new FileStream(FileName,FileMode.Create,FileAccess.Write);
StreamWriter sw = new StreamWriter(fs,Encoding.Default);
try
{
foreach(var line in richTextBox1.Lines)
{
sw.WriteLine(line+"\n");
}
}
catch(Exception ex)
{
MessageBox.Show("测试Log写入错误:"+ex.Message);
}
finally
{
sw.Close();
fs.Close();
}
}
int ExitMainProgream = 0;
private void timer2_Tick(object sender, EventArgs e)
{
label1.Text=((5-ExitMainProgream).ToString())+".秒";
label1.Font=new Font("宋体",16,FontStyle.Bold);
label1.BackColor = Color.Gray;
label1.ForeColor = Color.GreenYellow;
if (ExitMainProgream == 0)
{
CreateWriteTestLog(DateTime.Now.ToString("yyyyMMddHHmmss.") + @"log");
}
else if (ExitMainProgream == 5)
{
BootShowResult(TestValues);
timer2.Enabled = false;
}
ExitMainProgream++;
}
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();
}
private void button1_Click(object sender, EventArgs e)
{
timer2.Enabled = true;
}
}
}
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 ManyCardCaculateTest_V1._00
{
public partial class FAIL : Form
{
public FAIL()
{
InitializeComponent();
StartPosition = FormStartPosition.CenterParent;
this.FormBorderStyle = FormBorderStyle.None; //设置窗体为无边框样式
this.WindowState = FormWindowState.Maximized;
}
private void button1_Click(object sender, EventArgs e)
{
Shutdown();
Application.Exit();
}
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.6);
int y = (int)(0.5 * (this.Height - label1.Height));
label1.Location = new System.Drawing.Point(x, y);
//设置Button1控件座标
base.OnResize(e);
int x1 = (int)(0.5 * (this.Width - button1.Width));
//int y1 = (int)(button1.Location.Y*0.8);
int y1 = (int)(0.93 * (this.Height - button1.Height));
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");//执行关机命令
}
}
}
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 ManyCardCaculateTest_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.6);
int y=(int)(0.5*(this.Height-label1.Height));
label1.Location = new System.Drawing.Point(x, y);
//设置Button1控件座标
base.OnResize(e);
int x1 = (int)(0.5 * (this.Width - button1.Width));
//int y1 = (int)(button1.Location.Y*0.8);
int y1 = (int)(0.93 * (this.Height - button1.Height));
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;
}
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();
}
}
}