Skip to content

Commit

Permalink
docs(): cleaned up code documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
TvanSchagen committed Jul 3, 2019
1 parent 460dd22 commit 058cb8b
Show file tree
Hide file tree
Showing 9 changed files with 171 additions and 126 deletions.
12 changes: 6 additions & 6 deletions DummyDataGenerator/Connectors/SqlServerConnector.cs
Expand Up @@ -30,16 +30,16 @@ private SqlServerConnector()
if (Connection == null)
{
SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder();
builder.DataSource = "10.0.10.136";
builder.UserID = "SA";
builder.Password = "SQLServer2019";
builder.InitialCatalog = "master";
builder.DataSource = Env.GetString("SQLSERVER_HOST");
builder.UserID = Env.GetString("SQLSERVER_USER");
builder.Password = Env.GetString("SQLSERVER_PW");
builder.InitialCatalog = Env.GetString("SQLSERVER_DB");

try
{
_connection = new SqlConnection(builder.ConnectionString);
_connection.Open();
Logger.Info("Created connection to SQL Server @ " + "10.0.10.136");
Logger.Info("Created connection to SQL Server @ " + Env.GetString("SQLSERVER_HOST"));
Logger.Debug("State: " + _connection.State.ToString());
}
catch (SqlException e)
Expand All @@ -54,7 +54,7 @@ public void Close()
try
{
_connection?.Close();
Logger.Info("Closed connection to SQL Server @ " + "10.0.10.136");
Logger.Info("Closed connection to SQL Server @ " + Env.GetString("SQLSERVER_HOST"));
}
catch (SqlException e)
{
Expand Down
13 changes: 6 additions & 7 deletions DummyDataGenerator/Evaluators/MySqlClosureTableEvaluator .cs
Expand Up @@ -12,17 +12,16 @@ namespace DummyDataGenerator.Evaluators
{
class MySqlClosureTableEvaluator : MySqlEvaluator
{
// add the databases you want to evaluate here
private void AddDatabases()
{
databases.Add("scm_ct_breadth2_depth2");
databases.Add("scm_ct_breadth2_depth4");
databases.Add("scm_ct_breadth2_depth6");
databases.Add("scm_ct_breadth2_depth8");
databases.Add("scm_ct_breadth2_depth10");
databases.Add("scm_ct_breadth2_depth12");
databases.Add("scm_ct_breadth2_depth14");
databases.Add("scm_ct_b2_d10");
}

/// <summary>
/// Reads all .sql files from the directory and adds them to the list
/// </summary>
#warning due to limitations (of query length) of the MySQL performance schema, q<number> must be contained somewhere in the query in order to recognize it as this particular query
private void AddQueries()
{
string path = @"../../../Queries/SQL/Closure Table/";
Expand Down
51 changes: 44 additions & 7 deletions DummyDataGenerator/Evaluators/MySqlEvaluator.cs
Expand Up @@ -22,6 +22,9 @@ class MySqlEvaluator
protected List<string> databases = new List<string>();
protected List<string> queries = new List<string>();

/// <summary>
/// Runs the performance evaluator for all added databases and queries, and outputs a file with the results
/// </summary>
public void Execute()
{
AddDatabases();
Expand All @@ -43,17 +46,16 @@ public void Execute()
WriteOutputToFile("mysql_eval.csv");
}

// add databases you want to evaluate here
private void AddDatabases()
{
databases.Add("v2_scm_al_breadth2_depth2");
databases.Add("v2_scm_al_breadth2_depth4");
databases.Add("v2_scm_al_breadth2_depth6");
databases.Add("v2_scm_al_breadth2_depth8");
databases.Add("v2_scm_al_breadth2_depth10");
databases.Add("v2_scm_al_breadth2_depth12");
databases.Add("v2_scm_al_breadth2_depth14");
databases.Add("scm_al_b2_d10");
}

/// <summary>
/// Reads all .sql files from the directory and adds them to the list
/// </summary>
#warning due to limitations (of query length) of the MySQL performance schema, q<number> must be contained somewhere in the query in order to recognize it as this particular query
private void AddQueries()
{
string path = @"../../../Queries/SQL/Adjacency List/";
Expand All @@ -72,6 +74,10 @@ private void AddQueries()
}
}

/// <summary>
/// Returns the size of the specified database in megabytes
/// </summary>
/// <param name="database">The name of the database</param>
private void CaptureDatabaseSize(string database)
{
string statement = string.Format("USE {0}; SELECT sum(round(((data_length + index_length) / 1024 / 1024), 2)) as 'size_mb' FROM information_schema.TABLES WHERE table_schema = '{0}'", database);
Expand All @@ -80,6 +86,10 @@ private void CaptureDatabaseSize(string database)
output += database + " (" + size + " MB) ";
}

/// <summary>
/// Enabled the performance schema on MySQL in order to capture the query response times
/// </summary>
/// <param name="database">The name of the database</param>
private void EnablePerformanceSchema(string database)
{
Logger.Debug("Enabling performance schema");
Expand All @@ -100,6 +110,13 @@ private void EnablePerformanceSchema(string database)
command.ExecuteNonQuery();
}

/// <summary>
/// Executes a query on the database a specified number of times
/// </summary>
/// <param name="queryNumber">Number of the query</param>
/// <param name="query">The actual SQL code to be executed</param>
/// <param name="repetitions">Number of times the query is executed</param>
/// <param name="database"></param>
private void ExecuteQuery(int queryNumber, string query, int repetitions, string database)
{
Logger.Info("Executing query: " + queryNumber + " " + NUMBER_OF_REPETITIONS + " times on database: " + database);
Expand All @@ -111,6 +128,13 @@ private void ExecuteQuery(int queryNumber, string query, int repetitions, string
}
}

/// <summary>
/// Gets the result from the enabled performance schema for the specified query, using 'q' and the number of the query (example 'q1')
/// </summary>
/// <param name="queryNumber"></param>
/// <param name="query"></param>
/// <param name="repetitions"></param>
#warning due to limitations (of query length) of the MySQL performance schema, q<number> must be contained somewhere in the query in order to recognize it as this particular query
private void RetrieveResult(int queryNumber, string query, int repetitions)
{
Logger.Info("Retrieving results for query: " + (queryNumber));
Expand Down Expand Up @@ -148,6 +172,8 @@ private void RetrieveResult(int queryNumber, string query, int repetitions)

Logger.Info("Average execution time: " + totaExecutionTime / repetitions);

// gets individual response times

/*foreach (int i in eventIds)
{
string statement2 = string.Format("SELECT event_name AS Stage, TRUNCATE(TIMER_WAIT/1000000000000,6) AS Duration FROM performance_schema.events_stages_history_long WHERE NESTING_EVENT_ID = {0}", i);
Expand Down Expand Up @@ -184,6 +210,10 @@ private void CloseConnection()
connector.Close();
}

/// <summary>
/// Disables the performance schema for the specified database
/// </summary>
/// <param name="database">Name of the database</param>
private void DisablePerformanceSchema(string database)
{
Logger.Debug("Disabling performance schema");
Expand All @@ -197,6 +227,9 @@ private void DisablePerformanceSchema(string database)
command.ExecuteNonQuery();
}

/// <summary>
/// Adds headers to the output file based on the no. of repetitions
/// </summary>
private void AddHeadersToOutput()
{
Logger.Debug("Adding headers to file..");
Expand All @@ -208,6 +241,10 @@ private void AddHeadersToOutput()
output += "\n";
}

/// <summary>
/// Writes the results to a file in csv format
/// </summary>
/// <param name="fileName"></param>
private void WriteOutputToFile(string fileName)
{
Logger.Debug("Writing output to file..");
Expand Down
63 changes: 21 additions & 42 deletions DummyDataGenerator/Generators/MySqlClosureTableGenerator.cs
Expand Up @@ -3,8 +3,6 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading;

namespace DummyDataGenerator.Generators
{
Expand All @@ -29,6 +27,9 @@ public override void GenerateData(Configuration config, bool allowMultipleThread
Console.WriteLine("Program done, press enter to continue");
}

/// <summary>
/// Drops and creates the database, as to start off with a clean sheet
/// </summary>
private new void RefreshDatabaseSchema()
{
var watch = System.Diagnostics.Stopwatch.StartNew();
Expand All @@ -55,33 +56,14 @@ private new void RefreshDatabaseSchema()
com.ExecuteNonQuery();
}

struct ProductTreeData
{
public ProductTreeData(int org, int productsPerSupplier, int depth, int breadthPerLevel, int orgIter, int prodIter)
{
this.org = org;
this.productsPerSupplier = productsPerSupplier;
this.depth = depth;
this.breadthPerLevel = breadthPerLevel;
this.orgIter = orgIter;
this.prodIter = prodIter;
}

public int org { get; }
public int productsPerSupplier { get; }
public int depth { get; }
public int breadthPerLevel { get; }
public int orgIter;
public int prodIter;
}

/// <summary>
///
/// Generates all of the product trees necessary (amount of top level suppliers * amount of products)
/// starts with the top level products, and then proceeds with all branches in the tree
/// </summary>
/// <param name="organizationIdentifiers"></param>
/// <param name="productsPerSupplier"></param>
/// <param name="depth"></param>
/// <param name="breadthPerLevel"></param>
/// <param name="organizationIdentifiers">The id's of the top level organizations</param>
/// <param name="productsPerSupplier">The number of products each supplier should get</param>
/// <param name="depth">The depth of each chain (in how many branches does each product split up)</param>
/// <param name="breadthPerLevel">The breadth of each chain per level; the number of products to generate for the product above</param>
private void GenerateProductTrees(int[] organizationIdentifiers, int productsPerSupplier, int depth, int breadthPerLevel)
{
var watchTotal = System.Diagnostics.Stopwatch.StartNew();
Expand Down Expand Up @@ -148,7 +130,6 @@ private void GenerateProductTrees(int[] organizationIdentifiers, int productsPer
Console.WriteLine("Took " + watchTotal.ElapsedMilliseconds + "ms " + "(" + (watchTotal.ElapsedMilliseconds / 1000) + "s)" + "(" + (watchTotal.ElapsedMilliseconds / 1000 / 60) + "m) in total");
}


/// <summary>
/// Adds a single row to a single product hierarchy
/// </summary>
Expand Down Expand Up @@ -190,7 +171,18 @@ private List<int> GenerateProductRowAndRelations(List<int> allParentProductIdent
// first, retrieve the parent products and their path lengths
List<int> ids = new List<int>();
List<int> pathlengths = new List<int>();
string stmt = string.Format("WITH RECURSIVE cte ( ppid, cpid, pl ) AS ( SELECT parent.id, child.id, path_length FROM consists_of JOIN product AS parent ON consists_of.parent_product_id = parent.id JOIN product AS child ON consists_of.child_product_id = child.id WHERE child.id = {0} UNION ALL SELECT parent.id, child.id, path_length FROM consists_of JOIN product AS parent ON consists_of.parent_product_id = parent.id JOIN product AS child ON consists_of.child_product_id = child.id JOIN cte ON cte.ppid = child.id WHERE consists_of.child_product_id <> consists_of.parent_product_id ) SELECT ppid, MAX(pl) AS pl FROM cte GROUP BY ppid ORDER BY ppid DESC", childProductId);
string stmt = string.Format(@"WITH RECURSIVE cte ( ppid, cpid, pl ) AS (
SELECT parent.id, child.id, path_length FROM consists_of
JOIN product AS parent ON consists_of.parent_product_id = parent.id
JOIN product AS child ON consists_of.child_product_id = child.id
WHERE child.id = {0}
UNION ALL
SELECT parent.id, child.id, path_length FROM consists_of
JOIN product AS parent ON consists_of.parent_product_id = parent.id
JOIN product AS child ON consists_of.child_product_id = child.id
JOIN cte ON cte.ppid = child.id WHERE consists_of.child_product_id <> consists_of.parent_product_id )
SELECT ppid, MAX(pl) AS pl FROM cte GROUP BY ppid ORDER BY ppid DESC"
, childProductId);
MySqlCommand cmd = new MySqlCommand(stmt, connector.Connection);
MySqlDataReader reader = cmd.ExecuteReader();
while (reader.Read())
Expand All @@ -202,24 +194,11 @@ private List<int> GenerateProductRowAndRelations(List<int> allParentProductIdent
}
reader.Close();

// select all of our parent products for the created child product
/*string statement4 = string.Format("WITH RECURSIVE cte ( ppid, cpid, pl ) AS ( SELECT parent.id, child.id, path_length FROM consists_of JOIN product AS parent ON consists_of.parent_product_id = parent.id JOIN product AS child ON consists_of.child_product_id = child.id WHERE child.id = {0} UNION ALL SELECT parent.id, child.id, path_length FROM consists_of JOIN product AS parent ON consists_of.parent_product_id = parent.id JOIN product AS child ON consists_of.child_product_id = child.id JOIN cte ON cte.ppid = child.id WHERE consists_of.child_product_id <> consists_of.parent_product_id ) SELECT GROUP_CONCAT(DISTINCT concat(ppid, '; ', pl) ORDER BY ppid DESC SEPARATOR ',') AS ids FROM cte", childProductId);
MySqlCommand com4 = new MySqlCommand(statement4, c);
string list = (string) com4.ExecuteScalar();
List<string> result = list.Split(',').ToList();
foreach (string r in result)
{
ids.Add(Int32.Parse(r.Split(";").ToArray()[0]));
pathlengths.Add(Int32.Parse(r.Split(";").ToArray()[1]));
}*/

// skip the first two id's, because they the first one is referencing themselves, and the second one was already inserted before (initial parent > child relation)
for (int k = 2; k < ids.Count; k++)
{
// inserto into hierarchy
string statement5 = string.Format("INSERT INTO consists_of(parent_product_id, child_product_id, path_length) VALUES(" + ids[k] + "," + childProductId + ", " + (pathlengths[k] + 1) +");");
//Console.WriteLine("Depth: " + k);
// Console.WriteLine("Inserting: " + productIds[k]);
MySqlCommand com5 = new MySqlCommand(statement5, c);
com5.ExecuteNonQuery();
}
Expand Down

0 comments on commit 058cb8b

Please sign in to comment.