Advertisement
Blizzardo1

Loop Test

Aug 10th, 2021
1,321
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.29 KB | None | 0 0
  1. using System;
  2. using System.Diagnostics;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Security.Cryptography;
  6. using System.Text;
  7.  
  8. namespace LoopScopeTest
  9. {
  10.     internal static class Program
  11.     {
  12.         private static readonly Encoding encoding = Encoding.UTF8;
  13.         public static string GetString(this byte[] b) => encoding.GetString(b);
  14.         public static byte[] ToBytes(this string s) => encoding.GetBytes(s);
  15.         public static byte[] GetFromHex(this string s) => Convert.FromHexString(s);
  16.  
  17.         public static long Average(this long[] l) => l.Aggregate((x, y) => x + y) / l.Length;
  18.  
  19.         private record Data(string[] DataLine)
  20.         {
  21.             static byte[] key = { }; // Add your key here!
  22.             private SHA256 hmac = SHA256.Create();
  23.             private byte[] data;
  24.  
  25.             public long Overall;
  26.             public long InlineTest;
  27.             public long OutOfScopeTest;
  28.             public long Difference;
  29.             public string Hash;
  30.             public Data GetData()
  31.             {
  32.                 _ = long.TryParse(DataLine[0], out Overall);
  33.                 _ = long.TryParse(DataLine[1], out InlineTest);
  34.                 _ = long.TryParse(DataLine[2], out OutOfScopeTest);
  35.                 _ = long.TryParse(DataLine[3], out Difference);
  36.                 Hash = DataLine[4];
  37.                 data = ToString().ToBytes();
  38.                 return this;
  39.             }
  40.  
  41.             public bool IsDataValid()
  42.             {
  43.                 byte[] left = Hash.GetFromHex();
  44.                 byte[] right = hmac.ComputeHash(data);
  45.                 string a = Convert.ToHexString(left);
  46.                 string b = Convert.ToHexString(right);
  47.  
  48.                 return a == b;
  49.             }
  50.  
  51.             public string CompileHash() => BitConverter.ToString(hmac.ComputeHash(data)).Replace("-", "").ToLower();
  52.  
  53.             public override string ToString()
  54.             {
  55.                 return $"{Overall};{InlineTest};{OutOfScopeTest};{Difference}";
  56.             }
  57.         }
  58.  
  59.         private static long InlineForloop(ulong iterations)
  60.         {
  61.             Stopwatch sw = new();
  62.             var sb = new StringBuilder();
  63.             sb.AppendLine($"Testing inline forloop initializer with {iterations} iterations");
  64.             sb.AppendLine("for (ulong u = 0; u < iterations; u++) ; ");
  65.             Console.WriteLine(sb.ToString());
  66.             if (sw == null) sw = new();
  67.             sw.Start();
  68.             for (ulong u = 0; u < iterations; u++) ;
  69.             sw.Stop();
  70.             return sw.ElapsedMilliseconds;
  71.         }
  72.  
  73.         private static long OutOfScopeForloop(ulong iterations)
  74.         {
  75.             Stopwatch sw = new();
  76.             var sb = new StringBuilder();
  77.             sb.AppendLine($"Testing out of scope forloop initializer with {iterations} iterations");
  78.             sb.AppendLine("ulong u;");
  79.             sb.AppendLine("for (u = 0; u < iterations; u++) ; ");
  80.             Console.WriteLine(sb.ToString());
  81.  
  82.             if (sw == null) sw = new();
  83.             sw.Start();
  84.             ulong u;
  85.             for (u = 0; u < iterations; u++) ;
  86.             sw.Stop();
  87.             return sw.ElapsedMilliseconds;
  88.         }
  89.  
  90.         private static long RunTest(string testName, ulong iterations, Func<ulong, long> test)
  91.         {
  92.             long result = test(iterations);
  93.             Console.WriteLine($"{testName}: {result}ms");
  94.             Console.WriteLine("\n\n");
  95.             return result;
  96.         }
  97.  
  98.         private static void Main(string[] args)
  99.         {
  100.             Stopwatch sw = new();
  101.             sw.Start();
  102.  
  103.             const string log = "tests.log";
  104.             if(!File.Exists(log))
  105.             {
  106.                 File.WriteAllText(log, "Overall,Inline,Out of Scope,Difference,Hash\n");
  107.             }
  108.  
  109.             string[] history = File.ReadAllLines(log);
  110.             long[] overallAverage= new long[history.Length];
  111.             long[] testAAverage = new long[history.Length];
  112.             long[] testBAverage = new long[history.Length];
  113.             long[] differenceAverage = new long[history.Length];
  114.             Console.WriteLine("Retrieving history\n");
  115.  
  116.  
  117.             if (history.Length > 1)
  118.             {
  119.                 for (int i = 1; i < history.Length; i++)
  120.                 {
  121.                     Data data = new Data(history[i].Split(',')).GetData();
  122.                    
  123.                     if (!data.IsDataValid())
  124.                     {
  125.                         Console.WriteLine($"Invalid Data, Line {i + 1}");
  126.                         continue;
  127.                     }
  128.                     overallAverage[i] = data.Overall;
  129.                     testAAverage[i] = data.InlineTest;
  130.                     testBAverage[i] = data.OutOfScopeTest;
  131.                     differenceAverage[i] = data.Difference;
  132.                 }
  133.             }
  134.             if (history.Length > 1)
  135.             {
  136.                 Console.WriteLine($"Overall average {overallAverage.Average()}ms");
  137.                 Console.WriteLine($"Inline Test average {testAAverage.Average()}ms");
  138.                 Console.WriteLine($"Out of Initialization average {testBAverage.Average()}ms");
  139.                 Console.WriteLine($"Difference average {differenceAverage.Average()}ms");
  140.                 Console.WriteLine();
  141.             }
  142.  
  143.             var sb = new StringBuilder();
  144.             ulong iterations = int.MaxValue;
  145.             long testA = RunTest("Inline Test", iterations, InlineForloop);
  146.             long testB = RunTest("Out of Scope Test", iterations, OutOfScopeForloop);
  147.             long overall = testA + testB;
  148.             long difference = Math.Abs(testA - testB);
  149.             Console.WriteLine($"Difference of {difference}ms");
  150.             Console.WriteLine($"Total test time {overall}ms");
  151.             Console.WriteLine("\nTest complete!\n\n");
  152.             sw.Stop();
  153.             Console.WriteLine($"Program Runtime: {sw.ElapsedMilliseconds}ms\n\n");
  154.             Console.WriteLine($"That's an additional {sw.ElapsedMilliseconds - overall}ms of runtime.");
  155.             sb.AppendLine($"{overall},{testA},{testB},{difference},{new Data(new string[] { overall.ToString(), testA.ToString(), testB.ToString(), difference.ToString(), "" }).GetData().CompileHash()}");
  156.             File.AppendAllText(log, sb.ToString());
  157.  
  158.         }
  159.     }
  160. }
  161.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement