Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added byte rate calculation functons, closes #296 #314

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions src/Humanizer.Tests/Bytes/ByteRateTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using System;
using Humanizer.Bytes;
using Xunit;
using Xunit.Extensions;

namespace Humanizer.Tests.Bytes
{
public class ByteRateTests : AmbientCulture
{
public ByteRateTests() : base("en") { }

[Theory]
[InlineData(400, 1, "400 B/s")]
[InlineData(4 * 1024, 1, "4 KB/s")]
[InlineData(4 * 1024 * 1024, 1, "4 MB/s")]
[InlineData(4 * 2 * 1024 * 1024, 2, "4 MB/s")]
[InlineData(4 * 1024, 0.1, "40 KB/s")]
[InlineData(15 * 60 * 1024 * 1024, 60, "15 MB/s")]
public void HumanizesRates(long inputBytes, double perSeconds, string expectedValue)
{
var size = new ByteSize(inputBytes);
var interval = TimeSpan.FromSeconds(perSeconds);

var rate = size.Per(interval).ToRatePerSecond();

Assert.Equal(expectedValue, rate);
}
}
}
1 change: 1 addition & 0 deletions src/Humanizer.Tests/Humanizer.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
</Reference>
</ItemGroup>
<ItemGroup>
<Compile Include="Bytes\ByteRateTests.cs" />
<Compile Include="Bytes\ComparingTests.cs" />
<Compile Include="Bytes\CreatingTests.cs" />
<Compile Include="Bytes\ByteSizeExtensionsTests.cs" />
Expand Down
29 changes: 29 additions & 0 deletions src/Humanizer/Bytes/ByteRate.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Humanizer.Bytes
{
public class ByteRate
{
public ByteSize size { get; private set;}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please change the property names to CamelCase.


public TimeSpan interval { get; private set; }

public ByteRate(ByteSize size, TimeSpan interval)
{
this.size = size;
this.interval = interval;
}

/// <summary>
/// Return this rate as a string, EG "2 MB/s"
/// </summary>
/// <returns></returns>
public string ToRatePerSecond()
{
return (new ByteSize(size.Bytes / interval.TotalSeconds)).Humanize() + "/s";
}
}
}
14 changes: 13 additions & 1 deletion src/Humanizer/Bytes/ByteSizeExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Humanizer.Bytes;
using System;
using Humanizer.Bytes;

// ReSharper disable once CheckNamespace
namespace Humanizer
Expand Down Expand Up @@ -438,5 +439,16 @@ public static string Humanize(this ByteSize input, string format = null)
{
return string.IsNullOrWhiteSpace(format) ? input.ToString() : input.ToString(format);
}

/// <summary>
/// Turns a quantity of bytes in a given interval into a rate that can be manipulated
/// </summary>
/// <param name="size">Quantity of bytes</param>
/// <param name="interval">Interval to create rate for</param>
/// <returns></returns>
public static ByteRate Per(this ByteSize size, TimeSpan interval)
{
return new ByteRate(size, interval);
}
}
}
1 change: 1 addition & 0 deletions src/Humanizer/Humanizer.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
<AssemblyOriginatorKeyFile>Humanizer.snk</AssemblyOriginatorKeyFile>
</PropertyGroup>
<ItemGroup>
<Compile Include="Bytes\ByteRate.cs" />
<Compile Include="CollectionHumanizeExtensions.cs" />
<Compile Include="Configuration\CollectionFormatterRegistry.cs" />
<Compile Include="Localisation\CollectionFormatters\DefaultCollectionFormatter.cs" />
Expand Down