Advertisement
Guest User

Untitled

a guest
Feb 21st, 2020
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.95 KB | None | 0 0
  1. using System;
  2. using System.IO;
  3. using System.Linq;
  4. using System.Collections.Generic;
  5.  
  6. namespace Speedometer {
  7. class Program {
  8. static string DirPath = @"testFolder";
  9. static int CycleNum = 10;
  10.  
  11. static void Main(string[] args) {
  12. for (int i = 0, len = args.Length; i < len; i++) {
  13. var arg = args[i];
  14. var argLen = arg.Length;
  15. if (arg == "--") {
  16. break;
  17. }
  18.  
  19. arg = arg.TrimStart('-');
  20. if (argLen == arg.Length) {
  21. break;
  22. }
  23.  
  24. var flag = arg;
  25. switch (flag) {
  26. case "help":
  27. case "h":
  28. Console.WriteLine("speedometer [-h] [-d dir] [-cycles num]");
  29. return;
  30. case "dir":
  31. case "d":
  32. i++;
  33. if (i >= len) {
  34. Console.Error.WriteLine($"Expected -{flag} argument.");
  35. Environment.Exit(1);
  36. return;
  37. }
  38. DirPath = args[i];
  39. continue;
  40. case "cycles":
  41. case "c":
  42. i++;
  43. if (i >= len) {
  44. Console.Error.WriteLine($"Expected -{flag} argument.");
  45. Environment.Exit(1);
  46. return;
  47. }
  48. if (!Int32.TryParse(args[i], out CycleNum)) {
  49. Console.Error.WriteLine($"Cannot parse -{flag} argument.");
  50. Environment.Exit(1);
  51. return;
  52. }
  53. continue;
  54. default:
  55. Console.Error.WriteLine($"Unknown option {flag}");
  56. Environment.Exit(1);
  57. return;
  58. }
  59. }
  60. double creationTime = 0;
  61. // Creating and filling the directory.
  62. if (!Directory.Exists(DirPath)) {
  63. try {
  64. Directory.CreateDirectory(DirPath);
  65. }
  66. catch(DirectoryNotFoundException dirEx) {
  67. Console.WriteLine("An error occured: " + dirEx.Message);
  68. throw new DirectoryNotFoundException("Wrong folder path");
  69. }
  70. catch(IOException IOEx) {
  71. Console.WriteLine("An error occured: " + IOEx.Message);
  72. throw new IOException("Specified path is a file");
  73. }
  74. finally {
  75. Environment.Exit(1);
  76. }
  77. }
  78. Console.WriteLine($"Directory was made sucessefully!");
  79.  
  80. if (IsDirectoryEmpty(DirPath)) {
  81. creationTime = fillFolder(DirPath);
  82. }
  83.  
  84. DirectoryInfo di = new DirectoryInfo(DirPath);
  85. long bytesNum = 0;
  86. double overallTime = 0;
  87. var time1 = DateTime.Now;
  88. FileInfo[] df = di.GetFiles();
  89. Random rnd = new Random();
  90. // int[] seen;
  91. for (int i = 0; i < CycleNum; i++) {
  92. //getting the info about the random file
  93. string randomFile = GetRandomFile(ref df, ref rnd);
  94. FileInfo fi = new FileInfo(randomFile);
  95. bytesNum += fi.Length;
  96.  
  97. //calculating the time required to read this file
  98. var t1 = DateTime.Now;
  99. File.ReadAllText(randomFile);
  100. var t2 = DateTime.Now;
  101. TimeSpan sp = t2 - t1;
  102. overallTime += sp.TotalSeconds;
  103. }
  104. var time2 = DateTime.Now;
  105. TimeSpan span = time2 - time1;
  106. double cycleTime = span.TotalSeconds;
  107.  
  108. ShowResults(overallTime, cycleTime, bytesNum, DirPath, creationTime);
  109. }
  110. static void ShowResults(double overallTime, double cycleTime, long bytesNum, string fp, double creationTime) {
  111. Console.WriteLine("Sys name: " + Environment.MachineName);
  112. Console.WriteLine("Folder path: " + fp);
  113. if (creationTime != 0)
  114. Console.WriteLine("Time taken to create the directory: " + creationTime);
  115. else
  116. Console.WriteLine("The directory has already existed");
  117. Console.WriteLine("Number of bytes in read files: " + bytesNum);
  118. Console.WriteLine("Time spent to process the cycle: " + cycleTime);
  119. Console.WriteLine("Speed of reading files in byte/sec: " + (bytesNum/overallTime));
  120. }
  121. static string GetRandomFile(ref FileInfo[] df, ref Random rnd) {
  122. int i = rnd.Next(0, df.Length);
  123. return df[i].ToString();
  124. }
  125. static bool IsDirectoryEmpty(string fp) {
  126. return !Directory.EnumerateFileSystemEntries(fp).Any();
  127. }
  128. static double fillFolder(string fp) {
  129. var orig = fp;
  130. var data = new byte[5 * 1024 * 1024];
  131. var t1 = DateTime.Now;
  132. for (var i = 0; i < 1000; i++) {
  133. fp = orig + "/File" + i;
  134. File.WriteAllBytes(fp, data);
  135. }
  136. var t2 = DateTime.Now;
  137. TimeSpan sp = t2 - t1;
  138. return sp.TotalSeconds;
  139. }
  140. }
  141. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement