From 5348a23f586f82a3a0f735d33390be9cb729ede3 Mon Sep 17 00:00:00 2001 From: ProfessionalUwU Date: Mon, 3 Jul 2023 23:31:33 +0200 Subject: [PATCH] Add progress bar --- src/Chksum/Program.cs | 3 +++ src/Chksum/chksum.cs | 27 +++++++++++++++++++++++---- 2 files changed, 26 insertions(+), 4 deletions(-) diff --git a/src/Chksum/Program.cs b/src/Chksum/Program.cs index adcaf54..0abc8d0 100644 --- a/src/Chksum/Program.cs +++ b/src/Chksum/Program.cs @@ -26,6 +26,9 @@ public class Program { Console.ResetColor(); try { + if (args[1] == "MD5") { + + } int bufferSize = int.Parse(args[2]); utils.doTheThing(args[1], bufferSize); } diff --git a/src/Chksum/chksum.cs b/src/Chksum/chksum.cs index 7212865..c4163cb 100644 --- a/src/Chksum/chksum.cs +++ b/src/Chksum/chksum.cs @@ -98,9 +98,19 @@ public class ChksumUtils { } } + private void UpdateProgressBar(int current, int total) { + int progress = (int)((double)current / total * 100); + string progressText = $"Progress: {progress}% [{current}/{total}]"; + + Console.Write("\r" + progressText.PadRight(Console.WindowWidth)); + } + private Dictionary CalculateChecksums(string[] filenames) { ConcurrentDictionary checksums = new ConcurrentDictionary(); + int totalFiles = filenames.Length; + int processedFiles = 0; + Parallel.ForEach(filenames, (filename, state) => { using (var md5 = MD5.Create()) { using (var stream = File.OpenRead(filename)) { @@ -111,6 +121,8 @@ public class ChksumUtils { checksums.TryAdd(filename, checksum); } } + Interlocked.Increment(ref processedFiles); + UpdateProgressBar(processedFiles, totalFiles); } }); @@ -120,12 +132,17 @@ public class ChksumUtils { private Dictionary CalculateChecksumsWithMurmur(string[] filenames, int userDefinedBufferSize) { ConcurrentDictionary checksums = new ConcurrentDictionary(); + int totalFiles = filenames.Length; + int processedFiles = 0; + Parallel.ForEach(filenames, (filename, state) => { using (var stream = File.OpenRead(filename)) { var hash = CalculateMurmurHash32(stream, userDefinedBufferSize); lock (checksums) { checksums.TryAdd(filename, hash); } + Interlocked.Increment(ref processedFiles); + UpdateProgressBar(processedFiles, totalFiles); } }); @@ -151,11 +168,16 @@ public class ChksumUtils { private Dictionary CalculateChecksumsWithXxHash3(string[] filenames, int userDefinedBufferSize) { ConcurrentDictionary checksums = new ConcurrentDictionary(); + int totalFiles = filenames.Length; + int processedFiles = 0; + Parallel.ForEach(filenames, (filename, state) => { using (var stream = File.OpenRead(filename)) { var hash = CalculateXxHash3(stream, userDefinedBufferSize); checksums.TryAdd(filename, hash); } + Interlocked.Increment(ref processedFiles); + UpdateProgressBar(processedFiles, totalFiles); }); return new Dictionary(checksums); @@ -177,7 +199,6 @@ public class ChksumUtils { return hash; } - public void doTheThing(string hashalgo, int bufferSize) { ConnectionMultiplexer redis = ConnectionMultiplexer.Connect("localhost"); @@ -372,9 +393,7 @@ public class ChksumUtils { deleteCommand.Parameters.AddWithValue("$pathtofile", pathToFile); deleteCommand.ExecuteNonQuery(); - //Console.WriteLine("File deleted:"); - //Console.WriteLine($"\t{pathToFile}\n"); - logger.Information("File deleted: {pathToFile}", pathToFile); + logger.Information("File deleted:\n\t{pathToFile}", pathToFile); } } logger.Information("All deleted files were successfully removed from the database");