From 58f7f9384af4ef757c93276be941da354cc821d3 Mon Sep 17 00:00:00 2001 From: Ben Date: Tue, 23 Apr 2024 09:10:10 +0200 Subject: [PATCH] Test for disc speeds --- Framework/Utils/Formatter.cs | 6 +- .../ScalabilityTests/ClusterSpeedTests.cs | 83 +++++++++++++++++++ 2 files changed, 87 insertions(+), 2 deletions(-) create mode 100644 Tests/CodexTests/ScalabilityTests/ClusterSpeedTests.cs diff --git a/Framework/Utils/Formatter.cs b/Framework/Utils/Formatter.cs index 1ea1550..7422c84 100644 --- a/Framework/Utils/Formatter.cs +++ b/Framework/Utils/Formatter.cs @@ -1,4 +1,6 @@ -namespace Utils +using System.Globalization; + +namespace Utils { public static class Formatter { @@ -10,7 +12,7 @@ var sizeOrder = Convert.ToInt32(Math.Floor(Math.Log(bytes, 1024))); var digit = Math.Round(bytes / Math.Pow(1024, sizeOrder), 1); - return digit.ToString() + sizeSuffixes[sizeOrder]; + return digit.ToString(CultureInfo.InvariantCulture) + sizeSuffixes[sizeOrder]; } } } diff --git a/Tests/CodexTests/ScalabilityTests/ClusterSpeedTests.cs b/Tests/CodexTests/ScalabilityTests/ClusterSpeedTests.cs new file mode 100644 index 0000000..d8b27b1 --- /dev/null +++ b/Tests/CodexTests/ScalabilityTests/ClusterSpeedTests.cs @@ -0,0 +1,83 @@ +using DistTestCore; +using Logging; +using NUnit.Framework; +using Utils; + +namespace CodexTests.ScalabilityTests +{ + [TestFixture] + public class ClusterDiscSpeedTests : DistTest + { + private readonly Random random = new Random(); + + [Test] + [Combinatorial] + public void DiscSpeedTest( + [Values(1, 10, 100, 1024, 1024 * 10, 1024 * 100, 1024 * 1024)] int bufferSizeKb + ) + { + long targetSize = (long)(1024 * 1024 * 1024) * 2; + long bufferSizeBytes = ((long)bufferSizeKb) * 1024; + + var filename = nameof(DiscSpeedTest); + + Thread.Sleep(1000); + if (File.Exists(filename)) File.Delete(filename); + Thread.Sleep(1000); + var writeSpeed = PerformWrite(targetSize, bufferSizeBytes, filename); + Thread.Sleep(1000); + var readSpeed = PerformRead(targetSize, bufferSizeBytes, filename); + + Log($"Write speed: {writeSpeed} per second."); + Log($"Read speed: {writeSpeed} per second."); + } + + private ByteSize PerformWrite(long targetSize, long bufferSizeBytes, string filename) + { + long bytesWritten = 0; + var buffer = new byte[bufferSizeBytes]; + random.NextBytes(buffer); + + var sw = Stopwatch.Begin(GetTestLog()); + using (var stream = File.OpenWrite(filename)) + { + while (bytesWritten < targetSize) + { + long remaining = targetSize - bytesWritten; + long toWrite = Math.Min(bufferSizeBytes, remaining); + + stream.Write(buffer, 0, Convert.ToInt32(toWrite)); + bytesWritten += toWrite; + } + } + var duration = sw.End("WriteTime"); + double totalSeconds = duration.TotalSeconds; + double totalBytes = bytesWritten; + double bytesPerSecond = totalBytes / totalSeconds; + return new ByteSize(Convert.ToInt64(bytesPerSecond)); + } + + private ByteSize PerformRead(long targetSize, long bufferSizeBytes, string filename) + { + long bytesRead = 0; + var buffer = new byte[bufferSizeBytes]; + var sw = Stopwatch.Begin(GetTestLog()); + using (var stream = File.OpenRead(filename)) + { + while (bytesRead < targetSize) + { + long remaining = targetSize - bytesRead; + long toRead = Math.Min(bufferSizeBytes, remaining); + + var r = stream.Read(buffer, 0, Convert.ToInt32(toRead)); + bytesRead += r; + } + } + var duration = sw.End("ReadTime"); + double totalSeconds = duration.TotalSeconds; + double totalBytes = bytesRead; + double bytesPerSecond = totalBytes / totalSeconds; + return new ByteSize(Convert.ToInt64(bytesPerSecond)); + } + } +}