Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.IO;
- using System.Linq;
- using System.Collections.Generic;
- namespace Speedometer {
- class Program {
- static string DirPath = @"testFolder";
- static int CycleNum = 10;
- static void Main(string[] args) {
- for (int i = 0, len = args.Length; i < len; i++) {
- var arg = args[i];
- var argLen = arg.Length;
- if (arg == "--") {
- break;
- }
- arg = arg.TrimStart('-');
- if (argLen == arg.Length) {
- break;
- }
- var flag = arg;
- switch (flag) {
- case "help":
- case "h":
- Console.WriteLine("speedometer [-h] [-d dir] [-cycles num]");
- return;
- case "dir":
- case "d":
- i++;
- if (i >= len) {
- Console.Error.WriteLine($"Expected -{flag} argument.");
- Environment.Exit(1);
- return;
- }
- DirPath = args[i];
- continue;
- case "cycles":
- case "c":
- i++;
- if (i >= len) {
- Console.Error.WriteLine($"Expected -{flag} argument.");
- Environment.Exit(1);
- return;
- }
- if (!Int32.TryParse(args[i], out CycleNum)) {
- Console.Error.WriteLine($"Cannot parse -{flag} argument.");
- Environment.Exit(1);
- return;
- }
- continue;
- default:
- Console.Error.WriteLine($"Unknown option {flag}");
- Environment.Exit(1);
- return;
- }
- }
- double creationTime = 0;
- // Creating and filling the directory.
- if (!Directory.Exists(DirPath)) {
- try {
- Directory.CreateDirectory(DirPath);
- }
- catch(DirectoryNotFoundException dirEx) {
- Console.WriteLine("An error occured: " + dirEx.Message);
- throw new DirectoryNotFoundException("Wrong folder path");
- }
- catch(IOException IOEx) {
- Console.WriteLine("An error occured: " + IOEx.Message);
- throw new IOException("Specified path is a file");
- }
- finally {
- Environment.Exit(1);
- }
- }
- Console.WriteLine($"Directory was made sucessefully!");
- if (IsDirectoryEmpty(DirPath)) {
- creationTime = fillFolder(DirPath);
- }
- DirectoryInfo di = new DirectoryInfo(DirPath);
- long bytesNum = 0;
- double overallTime = 0;
- var time1 = DateTime.Now;
- FileInfo[] df = di.GetFiles();
- Random rnd = new Random();
- // int[] seen;
- for (int i = 0; i < CycleNum; i++) {
- //getting the info about the random file
- string randomFile = GetRandomFile(ref df, ref rnd);
- FileInfo fi = new FileInfo(randomFile);
- bytesNum += fi.Length;
- //calculating the time required to read this file
- var t1 = DateTime.Now;
- File.ReadAllText(randomFile);
- var t2 = DateTime.Now;
- TimeSpan sp = t2 - t1;
- overallTime += sp.TotalSeconds;
- }
- var time2 = DateTime.Now;
- TimeSpan span = time2 - time1;
- double cycleTime = span.TotalSeconds;
- ShowResults(overallTime, cycleTime, bytesNum, DirPath, creationTime);
- }
- static void ShowResults(double overallTime, double cycleTime, long bytesNum, string fp, double creationTime) {
- Console.WriteLine("Sys name: " + Environment.MachineName);
- Console.WriteLine("Folder path: " + fp);
- if (creationTime != 0)
- Console.WriteLine("Time taken to create the directory: " + creationTime);
- else
- Console.WriteLine("The directory has already existed");
- Console.WriteLine("Number of bytes in read files: " + bytesNum);
- Console.WriteLine("Time spent to process the cycle: " + cycleTime);
- Console.WriteLine("Speed of reading files in byte/sec: " + (bytesNum/overallTime));
- }
- static string GetRandomFile(ref FileInfo[] df, ref Random rnd) {
- int i = rnd.Next(0, df.Length);
- return df[i].ToString();
- }
- static bool IsDirectoryEmpty(string fp) {
- return !Directory.EnumerateFileSystemEntries(fp).Any();
- }
- static double fillFolder(string fp) {
- var orig = fp;
- var data = new byte[5 * 1024 * 1024];
- var t1 = DateTime.Now;
- for (var i = 0; i < 1000; i++) {
- fp = orig + "/File" + i;
- File.WriteAllBytes(fp, data);
- }
- var t2 = DateTime.Now;
- TimeSpan sp = t2 - t1;
- return sp.TotalSeconds;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement