|
using System;
using System.Windows.Forms;
using System.Data;
using System.Data.SqlClient;
using System.Data.SqlTypes;
using DotNetPerformance;
using DotNetPerformance.ResultOutput;
namespace Johns_Perf_Testing
{
/// <summary>
/// Summary description for DataSetTest.
/// </summary>
public class DataSetTest
{
[Benchmark("Test different ways to initialize a class")]
public TestResultGrp RunTest()
{
const int iterations = 20000;
const int runs = 5;
TestRunner tr = new TestRunner(iterations, runs);
TestRunner.TestCase testCases = null;
testCases += new TestRunner.TestCase(this.DataSetToString);
testCases += new TestRunner.TestCase(this.DataSetCast);
return tr.RunTests(testCases);
}
public TestResultGrp DataSet_VS_DataReader_Test()
{
const int iterations = 1000;
const int runs = 5;
TestRunner tr = new TestRunner(iterations, runs);
TestRunner.TestCase testCases = null;
testCases += new TestRunner.TestCase(this.GetDataSet);
testCases += new TestRunner.TestCase(this.GetDataReader);
return tr.RunTests(testCases);
}
public TestResultGrp DBNullTest()
{
const int iterations = 10000;
const int runs = 5;
TestRunner tr = new TestRunner(iterations, runs);
TestRunner.TestCase testCases = null;
testCases += new TestRunner.TestCase(this.GetDataWithDBNull);
testCases += new TestRunner.TestCase(this.GetDataWithOutDBNull);
return tr.RunTests(testCases);
}
public TestResultGrp OrdinalTest()
{
const int iterations = 10000;
const int runs = 5;
TestRunner tr = new TestRunner(iterations, runs);
TestRunner.TestCase testCases = null;
testCases += new TestRunner.TestCase(this.IntOrdinal);
testCases += new TestRunner.TestCase(this.StringOrdinal);
return tr.RunTests(testCases);
}
private DataSet data;
public DataSetTest()
{
try
{
SqlConnection con = new SqlConnection("Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=PerfTesting;Data Source=Rainer");
con.Open();
SqlDataAdapter adp = new SqlDataAdapter("select * from TestTable", con);
data = new DataSet();
adp.Fill(data);
con.Close();
}
catch (SqlException ex)
{
MessageBox.Show(ex.Message + Environment.NewLine + ex.StackTrace);
throw;
}
}
#region Test Cases
private void DataSetToString(int iterations)
{
string one, two;
int three, four;
decimal five, six;
for (int i = 0; i < iterations; i++)
{
foreach (DataRow row in data.Tables[0].Rows)
{
one = (string)row["string1"];
two = (string)row["string2"];
three = (int)row["int1"];
four = (int)row["int2"];
five = (decimal)row["decimal1"];
six = (decimal)row["decimal2"];
}
}
}
private void DataSetCast(int iterations)
{
string one, two;
int three, four;
decimal five, six;
for (int i = 0; i < iterations; i++)
{
foreach (DataRow row in data.Tables[0].Rows)
{
one = row["string1"].ToString();
two = row["string2"].ToString();
three = int.Parse(row["int1"].ToString());
four = int.Parse(row["int2"].ToString());
five = decimal.Parse(row["decimal1"].ToString());
six = decimal.Parse(row["decimal2"].ToString());
}
}
}
#endregion
#region Test Case Dataset vs DataReader
private void GetDataSet(int iterations)
{
for (int i = 0; i < iterations; i++)
{
SqlConnection con = new SqlConnection("Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=PerfTesting;Data Source=Rainer");
SqlDataAdapter adp = new SqlDataAdapter("select * from TestTable", con);
con.Open();
data = new DataSet();
adp.Fill(data);
con.Close();
foreach (DataRow row in data.Tables[0].Rows)
{
string temp;
temp = row[0].ToString();
temp = row[1].ToString();
temp = row[2].ToString();
temp = row[3].ToString();
temp = row[4].ToString();
temp = row[5].ToString();
temp = row[6].ToString();
temp = row[7].ToString();
temp = row[8].ToString();
temp = row[9].ToString();
temp = row[10].ToString();
temp = row[11].ToString();
temp = row[12].ToString();
temp = row[13].ToString();
temp = row[14].ToString();
temp = row[15].ToString();
temp = row[16].ToString();
temp = row[17].ToString();
temp = row[18].ToString();
temp = row[19].ToString();
temp = row[20].ToString();
temp = row[21].ToString();
temp = row[22].ToString();
temp = row[23].ToString();
temp = row[24].ToString();
temp = row[25].ToString();
}
}
}
private void GetDataReader(int iterations)
{
for (int i = 0; i < iterations; i++)
{
SqlConnection con = new SqlConnection("Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=PerfTesting;Data Source=Rainer");
SqlCommand cmd = new SqlCommand("select * from TestTable", con);
con.Open();
SqlDataReader reader = cmd.ExecuteReader(CommandBehavior.CloseConnection);
while (reader.Read())
{
string temp;
temp = reader[0].ToString();
temp = reader[1].ToString();
temp = reader[2].ToString();
temp = reader[3].ToString();
temp = reader[4].ToString();
temp = reader[5].ToString();
temp = reader[6].ToString();
temp = reader[7].ToString();
temp = reader[8].ToString();
temp = reader[9].ToString();
temp = reader[10].ToString();
temp = reader[11].ToString();
temp = reader[12].ToString();
temp = reader[13].ToString();
temp = reader[14].ToString();
temp = reader[15].ToString();
temp = reader[16].ToString();
temp = reader[17].ToString();
temp = reader[18].ToString();
temp = reader[19].ToString();
temp = reader[20].ToString();
temp = reader[21].ToString();
temp = reader[22].ToString();
temp = reader[23].ToString();
temp = reader[24].ToString();
temp = reader[25].ToString();
}
reader.Close();
}
}
#endregion Test Case DataSet vs DataReader
#region Test Case DataReader vs DataReader with db null check
private void GetDataWithDBNull(int iterations)
{
for (int i = 0; i < iterations; i++)
{
SqlConnection con = new SqlConnection("Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=PerfTestDB;Data Source=w2sis-eco928");
SqlCommand cmd = new SqlCommand("select * from TestTable", con);
con.Open();
SqlDataReader reader = cmd.ExecuteReader(CommandBehavior.CloseConnection);
while (reader.Read())
{
string tempS;
int tempI;
decimal tempD;
tempS = reader["string1"] == DBNull.Value ? "" : reader["string1"].ToString();
tempS = reader["string2"] == DBNull.Value ? "" : reader["string2"].ToString();
tempI = reader["int1"] == DBNull.Value ? 0 : (int)reader["int1"];
tempI = reader["int2"] == DBNull.Value ? 0 : (int)reader["int2"];
tempD = reader["decimal1"] == DBNull.Value ? 0 : (decimal)reader["decimal1"];
tempD = reader["decimal2"] == DBNull.Value ? 0 : (decimal)reader["decimal2"];
}
reader.Close();
}
}
private void GetDataWithOutDBNull(int iterations)
{
for (int i = 0; i < iterations; i++)
{
SqlConnection con = new SqlConnection("Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=PerfTestDB;Data Source=w2sis-eco928");
SqlCommand cmd = new SqlCommand("select * from TestTable", con);
con.Open();
SqlDataReader reader = cmd.ExecuteReader(CommandBehavior.CloseConnection);
while (reader.Read())
{
string tempS;
int tempI;
decimal tempD;
tempS = reader["string1"].ToString();
tempS = reader["string2"].ToString();
tempI = (int)reader["int1"];
tempI = (int)reader["int2"];
tempD = (decimal)reader["decimal1"];
tempD = (decimal)reader["decimal2"];
}
reader.Close();
}
}
#endregion Test Case DataSet vs DataReader
#region Test Case String Ordinal vs Int Ordinal with db null check
private void StringOrdinal(int iterations)
{
for (int i = 0; i < iterations; i++)
{
SqlConnection con = new SqlConnection("Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=PerfTestDB;Data Source=w2sis-eco928");
SqlCommand cmd = new SqlCommand("select * from TestTable", con);
con.Open();
SqlDataReader reader = cmd.ExecuteReader(CommandBehavior.CloseConnection);
while (reader.Read())
{
string tempS;
int tempI;
decimal tempD;
tempS = reader["string1"].ToString();
tempS = reader["string2"].ToString();
tempI = (int)reader["int1"];
tempI = (int)reader["int2"];
tempD = (decimal)reader["decimal1"];
tempD = (decimal)reader["decimal2"];
}
reader.Close();
}
}
private void IntOrdinal(int iterations)
{
for (int i = 0; i < iterations; i++)
{
SqlConnection con = new SqlConnection("Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=PerfTestDB;Data Source=w2sis-eco928");
SqlCommand cmd = new SqlCommand("select * from TestTable", con);
con.Open();
SqlDataReader reader = cmd.ExecuteReader(CommandBehavior.CloseConnection);
while (reader.Read())
{
string tempS;
int tempI;
decimal tempD;
tempS = (string)reader[0];
tempS = (string)reader[1];
tempI = (int)reader[2];
tempI = (int)reader[3];
tempD = (decimal)reader[4];
tempD = (decimal)reader[5];
}
reader.Close();
}
}
#endregion Test Case DataSet vs DataReader
}
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
I have been a professional developer since 1996. My experience comes from many different industries; Data Mining Software, Consulting, E-Commerce, Wholesale Operations, Clinical Software, Insurance, Energy.
I started programming in the military, trying to find better ways to analyze database data, eventually automating my entire job. Later, in college, I automated my way out of another job. This gave me the great idea to switch majors to the only thing that seemed natural…Programming!