Advertisement
sheefoo

JMeterAverageAggregator.cs

Jun 25th, 2020
1,109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.69 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Dynamic;
  4. using System.Globalization;
  5. using System.IO;
  6. using System.Linq;
  7. using System.Text.RegularExpressions;
  8. using CsvHelper;
  9.  
  10. namespace AverageCsv
  11. {
  12.     class Program
  13.     {
  14.         static void Main(string[] args)
  15.         {
  16.             var csvDir = args[0];
  17.  
  18.             var csvFiles = new DirectoryInfo(csvDir)
  19.                             .GetFiles()
  20.                             .Where(f => f.Extension.EndsWith("csv"))
  21.                             .Where(f => !f.Name.EndsWith("result.csv"));
  22.  
  23.             var dict = new Dictionary<string, dynamic>();
  24.  
  25.             var countRegex = new Regex(@"\w\w-(\d+).+");
  26.  
  27.             foreach (var csvFile in csvFiles)
  28.             {
  29.                 Console.WriteLine("Full path: " + csvFile.FullName);
  30.  
  31.                 var threadCount = countRegex.Match(csvFile.Name)
  32.                                             .Groups[1].Value;
  33.  
  34.                 using (var reader = new StreamReader(csvFile.FullName))
  35.                 using (var csv = new CsvReader(reader, CultureInfo.InvariantCulture))
  36.                 {
  37.                     var records = csv.GetRecords<Record>().ToArray();
  38.                     var recordsFiltered = records;
  39.  
  40.                     var a = recordsFiltered.Select(x => x.Label + ": " + x.Average).ToArray();
  41.                     Console.WriteLine(string.Join("\n", a));
  42.  
  43.                     var firstAverage = records.First();
  44.  
  45.                     foreach (var rec in recordsFiltered)
  46.                     {
  47.                         if (!dict.ContainsKey(rec.Label))
  48.                         {
  49.                             dynamic exp = new ExpandoObject();
  50.                             exp.Label = rec.Label;
  51.                             exp.u10  = 0;
  52.                             exp.u50  = 0;
  53.                             exp.u100 = 0;
  54.                             exp.u150 = 0;
  55.                             exp.u200 = 0;
  56.                             exp.u300 = 0;
  57.  
  58.                             dict.Add(rec.Label, exp);
  59.                         }
  60.  
  61.                         var e = dict[rec.Label] as IDictionary<String, Object>;
  62.                         e["u" + threadCount] = rec.Average;
  63.                     }
  64.                 }
  65.             }
  66.  
  67.             using (var writer = new StreamWriter(csvDir + "/result.csv"))
  68.             using (var csv = new CsvWriter(writer, CultureInfo.InvariantCulture))
  69.             {
  70.                 var finalRecords = dict.Values.ToList();
  71.                 csv.WriteRecords(finalRecords);
  72.             }
  73.         }
  74.     }
  75.  
  76.     class Record
  77.     {
  78.         public string Label { get; set; }
  79.         public int Average { get; set; }
  80.     }
  81. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement