A widget that records the running time table

Source: Internet
Author: User
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;
}
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.