一个记录程序运行时间表的控件
2024-07-21 02:24:08
供稿:网友
using system;
using system.collections;
using system.data;
namespace mytools
{
/// <summary>
/// summary description for timetest.
/// </summary>
public class timetest
{
private datatable manager = new datatable("manager");
private datatable timelist = new datatable("timelist");
public timetest()
{
#region initialize the managertable to save the test cases
datacolumn tempcolumn = new datacolumn("name",typeof(system.string));
manager.columns.add(tempcolumn);
tempcolumn = new datacolumn("description",typeof(system.string));
manager.columns.add(tempcolumn);
tempcolumn = new datacolumn("totaltime",typeof(system.timespan));
manager.columns.add(tempcolumn);
tempcolumn = new datacolumn("starttime",typeof(system.datetime));
manager.columns.add(tempcolumn);
tempcolumn = new datacolumn("testcount",typeof(system.int32));
manager.columns.add(tempcolumn);
manager.primarykey = new datacolumn[]{manager.columns["name"]};
#endregion
#region initialize the timelisttable to save the list of time span
tempcolumn = new datacolumn("name",typeof(system.string));
timelist.columns.add(tempcolumn);
tempcolumn = new datacolumn("time",typeof(system.timespan));
timelist.columns.add(tempcolumn);
tempcolumn = new datacolumn("description",typeof(system.string));
timelist.columns.add(tempcolumn);
#endregion
#region initialize a test case
this.addprocess("__maintest__","the default test is created by system!");
#endregion
}
public timetest(string testname,string description)
{
#region initialize the managertable to save the test cases
datacolumn tempcolumn = new datacolumn("name",typeof(system.string));
manager.columns.add(tempcolumn);
tempcolumn = new datacolumn("description",typeof(system.string));
manager.columns.add(tempcolumn);
tempcolumn = new datacolumn("totaltime",typeof(system.timespan));
manager.columns.add(tempcolumn);
tempcolumn = new datacolumn("starttime",typeof(system.datetime));
manager.columns.add(tempcolumn);
tempcolumn = new datacolumn("testcount",typeof(system.int32));
manager.columns.add(tempcolumn);
manager.primarykey = new datacolumn[]{manager.columns["name"]};
#endregion
#region initialize the timelisttable to save the list of time span
tempcolumn = new datacolumn("name",typeof(system.string));
timelist.columns.add(tempcolumn);
tempcolumn = new datacolumn("time",typeof(system.timespan));
timelist.columns.add(tempcolumn);
tempcolumn = new datacolumn("description",typeof(system.string));
timelist.columns.add(tempcolumn);
#endregion
#region initialize a test case
this.addprocess(testname,description);
#endregion
}
private void addprocess(string testname,string description)
{
datarow temprow = this.manager.newrow();
temprow["name"] = testname;
temprow["description"] = description;
temprow["starttime"] = datetime.now;
temprow["totaltime"] = timespan.zero;
temprow["testcount"] = 0;
this.manager.rows.add(temprow);
}
#region begin a test
public void begintest(string testname,string description)
{
datarow temprow = this.manager.rows.find(testname);
if(null != temprow)
{
temprow["starttime"] = datetime.now;
}
else
{
this.addprocess(testname,description);
}
}
public void begintest(string testname)
{
this.begintest(testname,"");
}
public void begin()
{
this.begintest(this.manager.rows[0]["name"].tostring(),"");
}
#endregion
#region end a test
public void endtest(string testname,string description)
{
datarow temprow = this.manager.rows.find(testname);
if(null == temprow)
{
return;
}
datetime temptime = (datetime)temprow["starttime"];
// temprow = this.timelist.newrow();
// temprow["time"] = datetime.now - temptime;
// temprow["name"] = testname;
// temprow["description"] = description;
// this.timelist.rows.add(temprow);
this.manager.rows.find(testname)["starttime"] = datetime.now;
this.manager.rows.find(testname)["totaltime"] = (timespan)(this.manager.rows.find(testname)["totaltime"]) + (datetime.now - temptime);
this.manager.rows.find(testname)["testcount"] = (int)this.manager.rows.find(testname)["testcount"] + 1;
}
public void endtest(string testname)
{
this.endtest(testname,"");
}
public void end()
{
this.endtest(this.manager.rows[0]["name"].tostring(),"");
}
public void end(string description)
{
this.endtest(this.manager.rows[0]["name"].tostring(),description);
}
#endregion
public datatable gettestlistbyname(string testname)
{
datatable rtntable = this.timelist.clone();
rtntable.columns["time"].datatype = typeof(system.double);
datarow row;
foreach(datarow temprow in this.timelist.select("name = '" + testname +"'"))
{
row = rtntable.newrow();
if(temprow["name"].tostring().trim().equals("__maintest__"))
{
row["name"] = "[system default]";
}
else
{
row["name"] = temprow["name"];
}
row["description"] = temprow["description"];
row["time"] = ((timespan)temprow["time"]).totalmilliseconds;
rtntable.rows.add(row);
}
return rtntable;
}
public datatable gettestlistbyname()
{
return gettestlistbyname(this.manager.rows[0]["name"].tostring());
}
public double gettesttimebyname(string testname)
{
return ((timespan)this.manager.rows.find(testname)["totaltime"]).totalmilliseconds;
}
public double gettesttimebyname()
{
return gettesttimebyname(this.manager.rows[0]["name"].tostring());
}
public datatable getalltesttime()
{
datatable rtntable = this.manager.clone();
rtntable.columns["totaltime"].datatype = typeof(system.double);
rtntable.columns.remove("starttime");
datarow row;
foreach(datarow temprow in this.manager.rows)
{
row = rtntable.newrow();
if(temprow["name"].tostring().trim().equals("__maintest__"))
{
row["name"] = "[system default]";
}
else
{
row["name"] = temprow["name"];
}
row["description"] = temprow["description"];
row["totaltime"] = ((timespan)temprow["totaltime"]).totalmilliseconds;
row["testcount"] = temprow["testcount"];
rtntable.rows.add(row);
}
return rtntable;
}
public datatable getalltestlist()
{
datatable rtntable = this.timelist.clone();
rtntable.columns["time"].datatype = typeof(system.double);
datarow row;
foreach(datarow temprow in this.timelist.rows)
{
row = rtntable.newrow();
if(temprow["name"].tostring().trim().equals("__maintest__"))
{
row["name"] = "[system default]";
}
else
{
row["name"] = temprow["name"];
}
row["description"] = temprow["description"];
row["time"] = ((timespan)temprow["time"]).totalmilliseconds;
rtntable.rows.add(row);
}
return rtntable;
}
}
}