Moves ByteSize to Utils assembly.

This commit is contained in:
benbierens 2023-09-08 09:39:56 +02:00
parent 075d9e6ae2
commit df6da29a69
No known key found for this signature in database
GPG Key ID: FE44815D96D0A1AA
6 changed files with 61 additions and 32 deletions

View File

@ -1,5 +1,6 @@
using DistTestCore.Marketplace; using DistTestCore.Marketplace;
using KubernetesWorkflow; using KubernetesWorkflow;
using Utils;
namespace DistTestCore.Codex namespace DistTestCore.Codex
{ {
@ -94,11 +95,11 @@ namespace DistTestCore.Codex
} }
} }
private long GetVolumeCapacity(CodexStartupConfig config) private ByteSize GetVolumeCapacity(CodexStartupConfig config)
{ {
if (config.StorageQuota != null) return config.StorageQuota.SizeInBytes; if (config.StorageQuota != null) return config.StorageQuota;
// Default Codex quota: 8 Gb, using 9 to be safe. // Default Codex quota: 8 Gb, using +20% to be safe.
return 9.GB().SizeInBytes; return 8.GB().Multiply(1.2);
} }
private int GetAccountIndex(MarketplaceInitialConfig marketplaceConfig) private int GetAccountIndex(MarketplaceInitialConfig marketplaceConfig)

View File

@ -1,6 +1,7 @@
using DistTestCore.Marketplace; using DistTestCore.Marketplace;
using DistTestCore.Metrics; using DistTestCore.Metrics;
using KubernetesWorkflow; using KubernetesWorkflow;
using Utils;
namespace DistTestCore.Codex namespace DistTestCore.Codex
{ {

View File

@ -0,0 +1,41 @@
using Utils;
namespace KubernetesWorkflow
{
public static class ByteSizeExtensions
{
public static string ToSuffixNotation(this ByteSize b)
{
long x = 1024;
var map = new Dictionary<long, string>
{
{ Pow(x, 4), "Ti" },
{ Pow(x, 3), "Gi" },
{ Pow(x, 2), "Mi" },
{ (x), "Ki" },
};
var bytes = b.SizeInBytes;
foreach (var pair in map)
{
if (bytes > pair.Key)
{
double bytesD = bytes;
double divD = pair.Key;
double numD = Math.Ceiling(bytesD / divD);
var v = Convert.ToInt64(numD);
return $"{v}{pair.Value}";
}
}
return $"{bytes}";
}
private static long Pow(long x, int v)
{
long result = 1;
for (var i = 0; i < v; i++) result *= x;
return result;
}
}
}

View File

@ -1,4 +1,6 @@
namespace KubernetesWorkflow using Utils;
namespace KubernetesWorkflow
{ {
public abstract class ContainerRecipeFactory public abstract class ContainerRecipeFactory
{ {
@ -97,38 +99,17 @@
podAnnotations.Add(name, value); podAnnotations.Add(name, value);
} }
protected void AddVolume(string mountPath, long capacityBytes) protected void AddVolume(string mountPath, ByteSize volumeSize)
{ {
volumeMounts.Add(new VolumeMount( volumeMounts.Add(new VolumeMount(
$"autovolume-{Guid.NewGuid().ToString().ToLowerInvariant()}", $"autovolume-{Guid.NewGuid().ToString().ToLowerInvariant()}",
mountPath, mountPath,
FormatBytesQuantity(capacityBytes))); volumeSize.ToSuffixNotation()));
} }
protected void Additional(object userData) protected void Additional(object userData)
{ {
additionals.Add(userData); additionals.Add(userData);
} }
private static string FormatBytesQuantity(long capacityBytes)
{
var map = new Dictionary<long, string>
{
{ (1024*1024*1024), "Gi" },
{ (1024*1024), "Mi" },
{ (1024), "Ki" },
};
foreach (var pair in map)
{
if (capacityBytes > pair.Key)
{
var v = (capacityBytes / pair.Key) + 1;
return $"{v}{pair.Value}";
}
}
return $"{capacityBytes * 2}Ki";
}
} }
} }

View File

@ -1,5 +1,6 @@
using DistTestCore; using DistTestCore;
using NUnit.Framework; using NUnit.Framework;
using Utils;
namespace Tests.DownloadConnectivityTests namespace Tests.DownloadConnectivityTests
{ {

View File

@ -1,10 +1,7 @@
using Utils; namespace Utils
namespace DistTestCore
{ {
public class ByteSize public class ByteSize
{ {
public ByteSize(long sizeInBytes) public ByteSize(long sizeInBytes)
{ {
if (sizeInBytes < 0) throw new ArgumentException("Cannot create ByteSize object with size less than 0. Was: " + sizeInBytes); if (sizeInBytes < 0) throw new ArgumentException("Cannot create ByteSize object with size less than 0. Was: " + sizeInBytes);
@ -18,6 +15,13 @@ namespace DistTestCore
return SizeInBytes / (1024 * 1024); return SizeInBytes / (1024 * 1024);
} }
public ByteSize Multiply(double factor)
{
double bytes = SizeInBytes;
double result = Math.Round(bytes * factor);
return new ByteSize(Convert.ToInt64(result));
}
public override bool Equals(object? obj) public override bool Equals(object? obj)
{ {
return obj is ByteSize size && SizeInBytes == size.SizeInBytes; return obj is ByteSize size && SizeInBytes == size.SizeInBytes;