Skip to content

Commit 1e0a261

Browse files
authored
Merge pull request #2 from agocke/add-gh-workflow
Implement basic support for MessagePack
2 parents 3496b18 + 793760f commit 1e0a261

24 files changed

+1355
-97
lines changed

.github/workflows/ci.yml

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
name: CI
2+
on:
3+
push:
4+
branches: [ main ]
5+
pull_request:
6+
branches: [ main ]
7+
8+
jobs:
9+
build:
10+
11+
runs-on: ubuntu-latest
12+
13+
steps:
14+
- uses: actions/checkout@v2
15+
- name: Setup .NET
16+
uses: actions/setup-dotnet@v1
17+
with:
18+
dotnet-version: 8.0.*
19+
- name: Restore dependencies
20+
run: dotnet restore
21+
- name: Build
22+
run: dotnet build --no-restore -warnaserror
23+
- name: Test
24+
run: dotnet test --no-build --verbosity normal
25+
- name: Pack
26+
run: dotnet pack -c Release

.github/workflows/pack.yml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
name: Pack
2+
on:
3+
push:
4+
branches: [ main ]
5+
jobs:
6+
pack:
7+
runs-on: ubuntu-latest
8+
9+
steps:
10+
- uses: actions/checkout@v2
11+
- name: Setup .NET
12+
uses: actions/setup-dotnet@v1
13+
with:
14+
dotnet-version: 8.0.*
15+
- name: Pack
16+
run: dotnet pack -c Release
17+
- name: Upload package
18+
uses: actions/upload-artifact@v4
19+
with:
20+
name: NuGet packages
21+
path: artifacts/package/release/*.nupkg

.github/workflows/publish.yml

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
2+
name: publish
3+
on:
4+
push:
5+
branches: [ release ]
6+
release:
7+
types: [ published ]
8+
9+
jobs:
10+
pack:
11+
runs-on: ubuntu-latest
12+
13+
steps:
14+
- uses: actions/checkout@v2
15+
- name: Setup .NET
16+
uses: actions/setup-dotnet@v1
17+
with:
18+
dotnet-version: 8.0.*
19+
- name: Pack
20+
run: dotnet pack -c Release
21+
- name: Publish package
22+
run: dotnet nuget push --skip-duplicate -k ${{ secrets.NUGET_API_KEY }} -s https://api.nuget.org/v3/index.json artifacts/package/release/*.nupkg

bench/DataGenerator.cs

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
2+
using System;
3+
4+
namespace Benchmarks
5+
{
6+
internal static class DataGenerator
7+
{
8+
public static T GenerateSerialize<T>() where T : Serde.ISerializeProvider<T>
9+
{
10+
if (typeof(T) == typeof(LoginViewModel))
11+
return (T)(object)CreateLoginViewModel();
12+
if (typeof(T) == typeof(Location))
13+
return (T)(object)Location.Sample;
14+
15+
throw new InvalidOperationException();
16+
17+
static LoginViewModel CreateLoginViewModel() => new LoginViewModel
18+
{
19+
Email = "[email protected]",
20+
// [SuppressMessage("Microsoft.Security", "CS002:SecretInNextLine", Justification="Dummy credentials for perf testing.")]
21+
Password = "abcdefgh123456!@",
22+
RememberMe = true
23+
};
24+
25+
}
26+
27+
public static Location CreateLocation() => new Location
28+
{
29+
Id = 1234,
30+
Address1 = "The Street Name",
31+
Address2 = "20/11",
32+
City = "The City",
33+
State = "The State",
34+
PostalCode = "abc-12",
35+
Name = "Nonexisting",
36+
PhoneNumber = "+0 11 222 333 44",
37+
Country = "The Greatest"
38+
};
39+
40+
public static byte[] GenerateDeserialize<T>()
41+
{
42+
if (typeof(T) == typeof(LoginViewModel))
43+
return MessagePack.MessagePackSerializer.Serialize(LoginViewSample);
44+
if (typeof(T) == typeof(Location))
45+
return MessagePack.MessagePackSerializer.Serialize(Location.Sample);
46+
47+
throw new InvalidOperationException("Unexpected type");
48+
}
49+
50+
public const string LoginViewSample = """
51+
{
52+
"email": "[email protected]",
53+
"password": "abcdefgh123456!@",
54+
"rememberMe": true
55+
}
56+
""";
57+
58+
}
59+
}

bench/DeserializeFromString.cs

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
// See the LICENSE file in the project root for more information.
4+
5+
using BenchmarkDotNet.Attributes;
6+
using MessagePack;
7+
using Serde;
8+
9+
namespace Benchmarks
10+
{
11+
[GenericTypeArguments(typeof(Location), typeof(LocationWrap))]
12+
public class DeserializeFromString<T, U>
13+
where T : Serde.IDeserializeProvider<T>
14+
where U : Serde.IDeserializeProvider<T>
15+
{
16+
private byte[] value = null!;
17+
18+
private readonly IDeserialize<T> _proxy = T.DeserializeInstance;
19+
private readonly IDeserialize<T> _manualProxy = U.DeserializeInstance;
20+
21+
[GlobalSetup]
22+
public void Setup()
23+
{
24+
value = DataGenerator.GenerateDeserialize<T>();
25+
}
26+
27+
[Benchmark]
28+
public T? MessagePack()
29+
{
30+
return MessagePackSerializer.Deserialize<T>(value);
31+
}
32+
33+
[Benchmark]
34+
public T SerdeMsgPack() => Serde.MsgPack.MsgPackSerializer.Deserialize<T, IDeserialize<T>>(value, _proxy);
35+
36+
[Benchmark]
37+
public T SerdeMsgPackManual() => Serde.MsgPack.MsgPackSerializer.Deserialize<T, IDeserialize<T>>(value, _manualProxy);
38+
39+
// DataContractJsonSerializer does not provide an API to serialize to string
40+
// so it's not included here (apples vs apples thing)
41+
}
42+
}

bench/Program.cs

Lines changed: 26 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,35 @@
1-
// See https://aka.ms/new-console-template for more information
2-
using System;
3-
using System.Security.Cryptography;
4-
using BenchmarkDotNet.Attributes;
1+
using BenchmarkDotNet.Configs;
2+
using BenchmarkDotNet.Diagnosers;
53
using BenchmarkDotNet.Running;
4+
using Benchmarks;
5+
using MessagePack;
66

7-
namespace MsgPack.Benchmarks;
7+
var msg1 = MessagePackSerializer.Serialize(Location.Sample);
8+
var msg2 = Serde.MsgPack.MsgPackSerializer.Serialize(Location.Sample);
89

9-
public class MessagePackUnit
10+
if (!msg1.SequenceEqual(msg2))
1011
{
11-
private const int N = 10000;
12-
private readonly byte[] data;
13-
14-
private readonly SHA256 sha256 = SHA256.Create();
15-
private readonly MD5 md5 = MD5.Create();
16-
17-
public MessagePackUnit()
18-
{
19-
data = new byte[N];
20-
new Random(42).NextBytes(data);
21-
}
22-
23-
[Benchmark]
24-
public byte[] Sha256() => sha256.ComputeHash(data);
25-
26-
[Benchmark]
27-
public byte[] Md5() => md5.ComputeHash(data);
12+
Console.WriteLine(string.Join(", ", msg1));
13+
Console.WriteLine(string.Join(", ", msg2));
14+
throw new InvalidOperationException("bytes do not match");
2815
}
2916

30-
public class Program
17+
var loc1 = MessagePackSerializer.Deserialize<Location>(msg1);
18+
var loc2 = Serde.MsgPack.MsgPackSerializer.Deserialize<Location, LocationWrap>(msg1, LocationWrap.Instance);
19+
20+
Console.WriteLine("Checking correctness of serialization: " + (loc1 == loc2));
21+
if (loc1 != loc2)
3122
{
32-
public static void Main(string[] args)
33-
{
34-
var summary = BenchmarkRunner.Run<MessagePackUnit>();
35-
}
23+
throw new InvalidOperationException($"""
24+
Serialization is not correct
25+
STJ:
26+
{loc1}
27+
28+
Serde:
29+
{loc2}
30+
""");
3631
}
3732

33+
var config = DefaultConfig.Instance.AddDiagnoser(MemoryDiagnoser.Default);
34+
var summary = BenchmarkSwitcher.FromAssembly(typeof(DeserializeFromString<,>).Assembly)
35+
.Run(args, config);

0 commit comments

Comments
 (0)