Skip to content

Commit

Permalink
Merge pull request #72 from phnx47/feat/type-half
Browse files Browse the repository at this point in the history
feat: support System.Half type
  • Loading branch information
phnx47 authored Oct 22, 2023
2 parents c48b07d + 496c379 commit c91ba55
Show file tree
Hide file tree
Showing 10 changed files with 151 additions and 100 deletions.
34 changes: 34 additions & 0 deletions .github/workflows/benchmarks.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
name: Benchmarks

on:
pull_request:
branches:
- "main"
workflow_dispatch:

jobs:
benchmarks:
name: Benchmarks
if: startsWith(github.ref, 'refs/heads/renovate') == false
runs-on: ubuntu-22.04
permissions:
pull-requests: write
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Restore
run: dotnet restore
- name: Build
run: dotnet build --no-restore -c Release
- name: Run all benchmarks
working-directory: tests/benchmarks
run: dotnet run -c Release -- -f *
- name: Add summary
working-directory: tests/benchmarks/BenchmarkDotNet.Artifacts/results
run: cat FingerprintBuilder.BenchmarkTests.ModelToHex-report-github.md >> $GITHUB_STEP_SUMMARY
- name: Add PR comment
uses: marocchino/sticky-pull-request-comment@v2
if: github.event_name == 'pull_request'
with:
recreate: true
path: tests/benchmarks/BenchmarkDotNet.Artifacts/results/FingerprintBuilder.BenchmarkTests.ModelToHex-report-github.md
86 changes: 41 additions & 45 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: 💿 CI
name: CI

on:
push:
Expand All @@ -13,79 +13,75 @@ on:

jobs:
build:
name: 🏭 Build & Test
name: Build & Test
runs-on: ubuntu-22.04
steps:
- name: 🚚 Checkout
- name: Checkout
uses: actions/checkout@v4
- name: ⚙ Restore
with:
fetch-depth: 0

- name: Setup .NET
uses: actions/setup-dotnet@v3
with:
dotnet-version: |
5.0.x
6.0.x
7.0.x
- name: Restore
run: dotnet restore
- name: 🏭 Build

- name: Build
run: dotnet build --no-restore -c Release -p:ContinuousIntegrationBuild=true
- name: 🧪 Run tests

- name: Run tests
run: dotnet test --no-build -c Release
- name: ☢ Publish to Codecov

- name: Publish to Codecov
uses: codecov/codecov-action@v3
- name: 🗜 Pack Dev NuGet artifacts

- name: Set Dev version
if: github.ref == 'refs/heads/main'
run: dotnet pack --no-build -c Release --version-suffix dev-$(date +%s)-${GITHUB_SHA::7}
- name: 🗜 Pack NuGet artifacts
run: echo "VERSION=$(git describe --long --tags | sed 's/^v//;0,/-/s//./')" >> $GITHUB_ENV

- name: Set Release version
if: startsWith(github.ref, 'refs/tags/v')
run: dotnet pack --no-build -c Release
- name: 📤 Upload artifacts
run: echo "VERSION=${GITHUB_REF_NAME#v}" >> $GITHUB_ENV

- name: Pack NuGet artifacts
if: github.ref == 'refs/heads/main' || startsWith(github.ref, 'refs/tags/v')
run: dotnet pack --no-build -c Release -p:PackageVersion="${{ env.VERSION }}"

- name: Upload artifacts
if: github.ref == 'refs/heads/main' || startsWith(github.ref, 'refs/tags/v')
uses: actions/upload-artifact@v3
with:
name: nupkgs
path: src/**/*nupkg

github:
name: 🚀 Deploy to GitHub
name: Deploy to GitHub
needs: [build]
if: github.ref == 'refs/heads/main' || startsWith(github.ref, 'refs/tags/v')
runs-on: ubuntu-22.04
steps:
- name: 📥 Download artifacts
- name: Download artifacts
uses: actions/download-artifact@v3
with:
name: nupkgs
- name: 📦 Publish to GitHub
run: dotnet nuget push "**/*.nupkg" -s https://nuget.pkg.github.com/phnx47/index.json -k ${{ secrets.GH_PKG_PAT }} --skip-duplicate
- name: Push to GitHub
run: dotnet nuget push "**/*.nupkg" -s https://nuget.pkg.github.com/phnx47/index.json -k ${{ secrets.GH_PKG_PAT }} --skip-duplicate

nuget:
name: 🚀 Deploy to NuGet
name: Deploy to NuGet
needs: [build]
if: startsWith(github.ref, 'refs/tags/v')
runs-on: ubuntu-22.04
steps:
- name: 📥 Download artifacts
- name: Download artifacts
uses: actions/download-artifact@v3
with:
name: nupkgs
- name: 📦 Publish to NuGet
- name: Push to NuGet
run: dotnet nuget push "**/*.nupkg" -k ${{ secrets.NUGET_KEY }} -s https://api.nuget.org/v3/index.json

benchmarks:
name: 🐌 Benchmarks
if: github.event_name != 'push'
runs-on: ubuntu-22.04
permissions:
pull-requests: write
steps:
- name: 🚚 Checkout
uses: actions/checkout@v4
- name: ⚙ Restore
run: dotnet restore
- name: 🏭 Build
run: dotnet build --no-restore -c Release
- name: 🐌 Run all benchmarks
working-directory: tests/benchmarks
run: dotnet run -c Release -- -f *
- name: 📝 Add summary
working-directory: tests/benchmarks/BenchmarkDotNet.Artifacts/results
run: cat FingerprintBuilder.BenchmarkTests.ModelToHex-report-github.md >> $GITHUB_STEP_SUMMARY
- name: 💬 Add PR comment
uses: marocchino/sticky-pull-request-comment@v2
if: github.event_name == 'pull_request'
with:
recreate: true
path: tests/benchmarks/BenchmarkDotNet.Artifacts/results/FingerprintBuilder.BenchmarkTests.ModelToHex-report-github.md
7 changes: 7 additions & 0 deletions Directory.Build.props
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<Project>
<PropertyGroup>
<LangVersion>11.0</LangVersion>
<IsPackable>false</IsPackable>
<NoWarn>CS1591;NETSDK1138</NoWarn>
</PropertyGroup>
</Project>
30 changes: 0 additions & 30 deletions FingerprintBuilder.sln
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,6 @@
Microsoft Visual Studio Solution File, Format Version 12.00
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FingerprintBuilder", "src\FingerprintBuilder.csproj", "{4A63DB87-276C-4FA9-A106-B62DDB222429}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{58083BD6-01D5-4CB8-92A3-06E0CF3D3F90}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "tests", "tests", "{16A1A1CA-A6FB-4C13-AC2A-3545E7D510BD}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "root", "root", "{695542BB-CD7D-4C7E-9209-B283CBE0EE5B}"
ProjectSection(SolutionItems) = preProject
LICENSE = LICENSE
README.md = README.md
.gitattributes = .gitattributes
.gitignore = .gitignore
.editorconfig = .editorconfig
EndProjectSection
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = ".github", ".github", "{F6951B67-481C-45A2-A9A7-6D3D4CC64213}"
ProjectSection(SolutionItems) = preProject
.github\CODEOWNERS = .github\CODEOWNERS
.github\renovate.json = .github\renovate.json
.github\FUNDING.yml = .github\FUNDING.yml
EndProjectSection
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "workflows", "workflows", "{0859450E-9030-4C36-ABF0-5017F31718F5}"
ProjectSection(SolutionItems) = preProject
.github\workflows\ci.yml = .github\workflows\ci.yml
EndProjectSection
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FingerprintBuilder.Tests", "tests\units\FingerprintBuilder.Tests.csproj", "{4D5DE776-DC77-4596-935A-C3833F149F4C}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FingerprintBuilder.BenchmarkTests", "tests\benchmarks\FingerprintBuilder.BenchmarkTests.csproj", "{CE2317B3-4DB5-4CB3-BB17-802F3253EF88}"
Expand All @@ -51,10 +26,5 @@ Global
{CE2317B3-4DB5-4CB3-BB17-802F3253EF88}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{4A63DB87-276C-4FA9-A106-B62DDB222429} = {58083BD6-01D5-4CB8-92A3-06E0CF3D3F90}
{F6951B67-481C-45A2-A9A7-6D3D4CC64213} = {695542BB-CD7D-4C7E-9209-B283CBE0EE5B}
{0859450E-9030-4C36-ABF0-5017F31718F5} = {F6951B67-481C-45A2-A9A7-6D3D4CC64213}
{4D5DE776-DC77-4596-935A-C3833F149F4C} = {16A1A1CA-A6FB-4C13-AC2A-3545E7D510BD}
{CE2317B3-4DB5-4CB3-BB17-802F3253EF88} = {16A1A1CA-A6FB-4C13-AC2A-3545E7D510BD}
EndGlobalSection
EndGlobal
24 changes: 12 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# fingerprint-builder-net
# fingerprint-builder

[![ci](https://img.shields.io/github/actions/workflow/status/phnx47/fingerprint-builder-net/ci.yml?branch=main&label=ci&logo=github&style=flat-square)](https://github.com/phnx47/fingerprint-builder-net/actions/workflows/ci.yml)
[![nuget](https://img.shields.io/nuget/v/FingerprintBuilder?logo=nuget&style=flat-square)](https://www.nuget.org/packages/FingerprintBuilder)
Expand Down Expand Up @@ -44,20 +44,20 @@ var hash = sha256(user).ToLowerHexString(); // 62565a67bf16004038c502eb68907411f
## Benchmarks

```ini

BenchmarkDotNet=v0.13.5, OS=ubuntu 22.04
BenchmarkDotNet v0.13.9+228a464e8be6c580ad9408e98f18813f6407fb5a, Ubuntu 22.04.3 LTS (Jammy Jellyfish)
Intel Xeon Platinum 8272CL CPU 2.60GHz, 1 CPU, 2 logical and 2 physical cores
.NET SDK=7.0.203
[Host] : .NET 7.0.5 (7.0.523.17405), X64 RyuJIT AVX2
DefaultJob : .NET 7.0.5 (7.0.523.17405), X64 RyuJIT AVX2
.NET SDK 7.0.402
[Host] : .NET 7.0.12 (7.0.1223.47720), X64 RyuJIT AVX2
DefaultJob : .NET 7.0.12 (7.0.1223.47720), X64 RyuJIT AVX2
```

| Method | Mean | Error | StdDev | Median | Gen0 | Allocated |
| Method | Mean | Error | StdDev | Median | Gen0 | Allocated |
|-------------------- |---------:|----------:|----------:|---------:|-------:|----------:|
| MD5_Model_To_Hex | 2.508 μs | 0.0217 μs | 0.0203 μs | 2.497 μs | 0.0725 | 1.34 KB |
| SHA1_Model_To_Hex | 2.822 μs | 0.0273 μs | 0.0255 μs | 2.826 μs | 0.0801 | 1.49 KB |
| SHA256_Model_To_Hex | 3.416 μs | 0.0227 μs | 0.0213 μs | 3.417 μs | 0.1030 | 1.93 KB |
| SHA512_Model_To_Hex | 5.133 μs | 0.0305 μs | 0.0286 μs | 5.130 μs | 0.1678 | 3.12 KB |
| MD5_Model_To_Hex | 2.613 μs | 0.0076 μs | 0.0071 μs | 2.614 μs | 0.0725 | 1.34 KB |
| SHA1_Model_To_Hex | 2.910 μs | 0.0172 μs | 0.0161 μs | 2.910 μs | 0.0801 | 1.49 KB |
| SHA256_Model_To_Hex | 3.540 μs | 0.0064 μs | 0.0053 μs | 3.539 μs | 0.1030 | 1.93 KB |
| SHA512_Model_To_Hex | 5.449 μs | 0.0173 μs | 0.0153 μs | 5.449 μs | 0.1678 | 3.12 KB |

<!-- Sticky Pull Request Comment -->

## License

Expand Down
8 changes: 8 additions & 0 deletions src/FingerprintBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ public class FingerprintBuilder<T> : IFingerprintBuilder<T>

private readonly Type[] _supportedTypes =
{
#if HasSystemHalf
typeof(Half),
#endif
typeof(bool),
typeof(byte),
typeof(sbyte),
Expand Down Expand Up @@ -147,6 +150,11 @@ public Func<T, byte[]> Build()
case string typedValue:
binaryWriter.Write(typedValue);
break;
#if HasSystemHalf
case Half typedValue:
binaryWriter.Write(typedValue);
break;
#endif
}
}

Expand Down
12 changes: 7 additions & 5 deletions src/FingerprintBuilder.csproj
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<LangVersion>11.0</LangVersion>
<Description>Calculate fingerprint for an object</Description>
<Description>Compute hash for specified .NET object</Description>
<Copyright>2019 © Serge K</Copyright>
<VersionPrefix>2.0.0</VersionPrefix>
<Authors>Serge K</Authors>
<TargetFramework>netstandard2.0</TargetFramework>
<TargetFrameworks>netstandard2.0;net6.0</TargetFrameworks>
<PackageId>FingerprintBuilder</PackageId>
<PackageTags>fingerprint;hash</PackageTags>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<RepositoryType>git</RepositoryType>
<RepositoryUrl>https://github.com/phnx47/fingerprint-builder-net</RepositoryUrl>
<RepositoryUrl>https://github.com/phnx47/fingerprint-builder</RepositoryUrl>
<PublishRepositoryUrl>true</PublishRepositoryUrl>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<IncludeSymbols>true</IncludeSymbols>
Expand All @@ -19,12 +18,15 @@
<PackageIcon>icon.png</PackageIcon>
<PackageReadmeFile>README.md</PackageReadmeFile>
<PackageOutputPath>nupkgs</PackageOutputPath>
<NoWarn>CS1591</NoWarn>
<IsPackable>true</IsPackable>
</PropertyGroup>
<ItemGroup>
<None Include="../icon.png" Pack="true" Visible="false" PackagePath="" />
<None Include="../README.md" Pack="true" Visible="false" PackagePath="" />
</ItemGroup>
<PropertyGroup Condition=" '$(TargetFramework)' == 'net6.0'">
<DefineConstants>$(DefineConstants);HasSystemHalf</DefineConstants>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.1.1" PrivateAssets="All" />
</ItemGroup>
Expand Down
1 change: 0 additions & 1 deletion tests/benchmarks/FingerprintBuilder.BenchmarkTests.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<IsPackable>false</IsPackable>
<OutputType>Exe</OutputType>
</PropertyGroup>
<ItemGroup>
Expand Down
15 changes: 8 additions & 7 deletions tests/units/FingerprintBuilder.Tests.csproj
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>net6.0;net7.0</TargetFrameworks>
<IsPackable>false</IsPackable>
<LangVersion>11.0</LangVersion>
<TargetFrameworks>net5.0;net6.0;net7.0</TargetFrameworks>
<CollectCoverage>true</CollectCoverage>
<CoverletOutputFormat>opencover</CoverletOutputFormat>
<NoWarn>CS0618</NoWarn>
<NoWarn>$(NoWarn);CS0618</NoWarn>
</PropertyGroup>
<PropertyGroup Condition=" '$(TargetFramework)' != 'net5.0'">
<DefineConstants>$(DefineConstants);HasSystemHalf</DefineConstants>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.7.2" />
<PackageReference Include="xunit" Version="2.5.3" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.3">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Condition="'$(TargetFramework)' != 'net5.0'" Include="xunit.runner.visualstudio" Version="2.5.3" />
<PackageReference Condition="'$(TargetFramework)' == 'net5.0'" Include="xunit.runner.visualstudio" Version="2.4.*" />
<PackageReference Include="coverlet.collector" Version="6.0.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
Expand Down
34 changes: 34 additions & 0 deletions tests/units/TypedTests/HalfTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,38 @@ namespace FingerprintBuilder.Tests.TypedTests;

public class HalfTests
{
#if HasSystemHalf
private readonly Func<ThisUser, byte[]> _sha1;
private readonly ThisUser _user;

public HalfTests()
{
_sha1 = FingerprintBuilder<ThisUser>
.Create(SHA1.Create())
.For(p => p.FirstName)
.For(p => p.Number)
.Build();

_user = new ThisUser { FirstName = "John", Number = (Half)2.1 };
}

[Fact]
public void Sha1()
{
var hash = _sha1(_user).ToLowerHexString();
Assert.Equal("5262b46f62ebac058f25289132af4577f9f2236a", hash);
}

[Fact]
public void UserInfo_Sha1_UpdateBool_ChangeHash()
{
var hash0 = _sha1(_user).ToLowerHexString();
_user.Number = (Half)2.11;
var hash1 = _sha1(_user).ToLowerHexString();

Assert.NotEqual(hash0, hash1);
}
#else
[Fact]
public void Half_ThrowArgumentException() // Half is not avalaivable in 'netstandard2.*'
{
Expand All @@ -18,6 +50,8 @@ public void Half_ThrowArgumentException() // Half is not avalaivable in 'netstan

Assert.Equal(nameof(ThisUser.Number), exception.ParamName);
}
#endif


private class ThisUser : User
{
Expand Down

0 comments on commit c91ba55

Please sign in to comment.