AddColumn() public method

public AddColumn ( string name ) : ColumnName
name string
return ColumnName
Example #1
0
        public static DataTable ReadFromCSV(string fullFilePath)
        {
            DataTable table = new DataTable();

            using (CSVReader reader = new CSVReader(fullFilePath))
            {
                // Create columns.
                ColumnName[] columns = new ColumnName[reader.TitlePositions.Count];
                int columnIndex = 0;

                foreach (KeyValuePair<string, int> titleInfo in reader.TitlePositions.OrderBy(p => p.Value))
                {
                    columns[columnIndex++] = table.AddColumn(titleInfo.Key);
                }

                // Iterate through each row in the CSV file and add the data to the table.
                for (int rowIndex = 0; rowIndex < reader.Length(); rowIndex++)
                {
                    // Create a new row.
                    Row row = table.AppendRow();
                    for(columnIndex = 0; columnIndex < columns.Length; columnIndex++)
                    {
                        ColumnName columnName = columns[columnIndex];
                        row[columnName] = reader.GetValue(rowIndex, columnName.Name);
                    }
                }
            }

            return table;
        }
        /// <summary>
        /// Generate CSV data (Probably not the most efficient way, e.g. How big can this become).
        /// </summary>
        /// <param name="reader"></param>
        private void WriteStatisticsToFile(CSVMetricReader reader)
        {
            var statisticsFilePath = $"{Configuration.RunId}-Statistics.csv";
            var statisticsTable = new DataTable();
            var col0_testName = statisticsTable.AddColumn("Test Name");
            var col1_metric = statisticsTable.AddColumn("Metric");
            var col2_iterations = statisticsTable.AddColumn("Iterations");
            var col3_average = statisticsTable.AddColumn("Average");
            var col4_stdevs = statisticsTable.AddColumn("Sample standard deviation");
            var col5_min = statisticsTable.AddColumn("Minimum");
            var col6_max = statisticsTable.AddColumn("Maximum");

            foreach (var testCaseName in reader.TestCases)
            {
                List<Dictionary<string, double>> iterations = reader.GetValues(testCaseName);

                var measurements = new Dictionary<string, List<double>>();
                foreach (var dict in iterations)
                {
                    foreach (var pair in dict)
                    {
                        if (!measurements.ContainsKey(pair.Key))
                            measurements[pair.Key] = new List<double>();
                        measurements[pair.Key].Add(pair.Value);
                    }
                }

                foreach (var measurement in measurements)
                {
                    var metric = measurement.Key;
                    var count = measurement.Value.Count;
                    var avg = measurement.Value.Average();
                    var stdev_s = Math.Sqrt(measurement.Value.Sum(x => Math.Pow(x - avg, 2)) / (measurement.Value.Count - 1));
                    var max = measurement.Value.Max();
                    var min = measurement.Value.Min();

                    var r = statisticsTable.AppendRow();
                    r[col0_testName] = testCaseName;
                    r[col1_metric] = metric;
                    r[col2_iterations] = count.ToString();
                    r[col3_average] = avg.ToString();
                    r[col4_stdevs] = stdev_s.ToString();
                    r[col5_min] = min.ToString();
                    r[col6_max] = max.ToString();
                }
            }

            statisticsTable.WriteToCSV(statisticsFilePath);
            Console.WriteLine($"\nStatistics written to \"{statisticsFilePath}\"\n");
        }