Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit f91556c

Browse files
Aymen TROUDIAymen TROUDI
Aymen TROUDI
authored and
Aymen TROUDI
committed
First commit !
1 parent af5ba60 commit f91556c

22 files changed

+555
-1
lines changed

‎App/App.csproj‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net5.0</TargetFramework>
6+
</PropertyGroup>
7+
8+
<ItemGroup>
9+
<PackageReference Include="BenchmarkDotNet" Version="0.13.1" />
10+
</ItemGroup>
11+
12+
<ItemGroup>
13+
<ProjectReference Include="..\LibOne\LibOne.csproj" />
14+
<ProjectReference Include="..\LibTwo\LibTwo.csproj" />
15+
</ItemGroup>
16+
17+
</Project>

‎App/Benchmarks/BenchCategory.cs‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
namespace App.Benchmarks
2+
{
3+
public enum BenchCategory
4+
{
5+
BinaryFormatter,
6+
BinarySerializer,
7+
}
8+
}

‎App/Benchmarks/BenchConfig.cs‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
using BenchmarkDotNet.Columns;
2+
using BenchmarkDotNet.Configs;
3+
using BenchmarkDotNet.Diagnosers;
4+
using BenchmarkDotNet.Exporters;
5+
using BenchmarkDotNet.Loggers;
6+
using BenchmarkDotNet.Order;
7+
8+
namespace App.Benchmarks
9+
{
10+
public class BenchConfig : ManualConfig
11+
{
12+
public BenchConfig()
13+
{
14+
AddColumn(RankColumn.Arabic);
15+
AddColumn(StatisticColumn.Min);
16+
AddColumn(StatisticColumn.Max);
17+
AddLogger(ConsoleLogger.Default);
18+
AddColumn(CategoriesColumn.Default);
19+
AddExporter(HtmlExporter.Default);
20+
AddExporter(MarkdownExporter.GitHub);
21+
AddExporter(MarkdownExporter.Default);
22+
AddDiagnoser(MemoryDiagnoser.Default);
23+
AddLogicalGroupRules(BenchmarkLogicalGroupRule.ByParams);
24+
WithOrderer(new DefaultOrderer(SummaryOrderPolicy.FastestToSlowest));
25+
}
26+
}
27+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
using App.Helpers;
2+
using BenchmarkDotNet.Attributes;
3+
using LibOne;
4+
using LibTwo;
5+
using EmployeeOne = LibOne.Models.Employee;
6+
using EmployeeTwo = LibTwo.Models.Employee;
7+
8+
namespace App.Benchmarks
9+
{
10+
[Config(typeof(BenchConfig))]
11+
public class BinarySerializationBench
12+
{
13+
private static readonly LibOneBinarySerializer SerializerOne = new ();
14+
private static readonly LibTwoBinarySerializer SerializerTwo = new ();
15+
16+
public EmployeeOne EmployeeOne { get; private set; }
17+
public EmployeeTwo EmployeeTwo { get; private set; }
18+
19+
[Params(16, 32, 64, 128, 256)]
20+
public int Length { get; set; }
21+
22+
[GlobalSetup]
23+
public void Setup()
24+
{
25+
EmployeeOne = RandomHelper.RandomEmployeeOne(Length);
26+
EmployeeTwo = RandomHelper.RandomEmployeeTwo(Length);
27+
}
28+
29+
[Benchmark]
30+
[BenchmarkCategory(nameof(BenchCategory.BinaryFormatter))]
31+
public EmployeeOne SerializeAndDeserializeOne()
32+
{
33+
using var stream = SerializerOne.Serialize(EmployeeOne);
34+
return SerializerOne.Deserialize<EmployeeOne>(stream);
35+
}
36+
37+
[Benchmark]
38+
[BenchmarkCategory(nameof(BenchCategory.BinarySerializer))]
39+
public EmployeeTwo SerializeAndDeserializeTwo()
40+
{
41+
using var stream = SerializerTwo.Serialize(EmployeeTwo);
42+
return SerializerTwo.Deserialize<EmployeeTwo>(stream);
43+
}
44+
}
45+
}

‎App/Helpers/RandomHelper.cs‎

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
using System;
2+
using System.Linq;
3+
using EmployeeOne = LibOne.Models.Employee;
4+
using EmployeeTwo = LibTwo.Models.Employee;
5+
6+
namespace App.Helpers
7+
{
8+
public static class RandomHelper
9+
{
10+
private static readonly Random Random = new(Guid.NewGuid().GetHashCode());
11+
12+
public static EmployeeOne RandomEmployeeOne(int length)
13+
{
14+
return new()
15+
{
16+
FirstName = RandomString(length),
17+
LastName = RandomString(length),
18+
Title = RandomString(length),
19+
Address = new LibOne.Models.Address
20+
{
21+
City = RandomString(length),
22+
Country = RandomString(length)
23+
}
24+
};
25+
}
26+
27+
public static LibTwo.Models.Employee RandomEmployeeTwo(int length)
28+
{
29+
return new()
30+
{
31+
FirstName = RandomString(length),
32+
LastName = RandomString(length),
33+
Title = RandomString(length),
34+
Address = new LibTwo.Models.Address
35+
{
36+
City = RandomString(length),
37+
Country = RandomString(length)
38+
}
39+
};
40+
}
41+
42+
private static string RandomString(int length)
43+
{
44+
const string chars = @"ABCDEFGHIJKLMNOPQRSTUVWXYZ";
45+
return new string(Enumerable.Repeat(chars, length)
46+
.Select(s => s[Random.Next(s.Length)]).ToArray());
47+
}
48+
}
49+
}

‎App/Program.cs‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
using App.Benchmarks;
2+
using BenchmarkDotNet.Running;
3+
4+
namespace App
5+
{
6+
public static class Program
7+
{
8+
private static void Main()
9+
{
10+
BenchmarkRunner.Run<BinarySerializationBench>();
11+
}
12+
}
13+
}

‎BinarySerializationBenchDemo.sln‎

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio Version 16
4+
VisualStudioVersion = 16.0.31515.178
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "App", "App\App.csproj", "{4C899376-9F35-402F-A883-4D40CA9924E9}"
7+
EndProject
8+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "LibOne", "LibOne\LibOne.csproj", "{3C865B64-575D-4595-AD5D-FC2ABEDCF6E9}"
9+
EndProject
10+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "LibTwo", "LibTwo\LibTwo.csproj", "{BA3F38B1-1A27-4313-8F96-67067AA0FCCB}"
11+
EndProject
12+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "LibOneTests", "LibOneTests\LibOneTests.csproj", "{1F56F913-C950-4B10-987C-80FF63DDF80F}"
13+
EndProject
14+
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Tests", "Tests", "{B8CC92F3-BD6F-4488-8F21-0E5EB082E86F}"
15+
EndProject
16+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "LibTwoTests", "LibTwoTests\LibTwoTests.csproj", "{AAA9CD12-64BB-4BB1-B6BC-BC32FD067148}"
17+
EndProject
18+
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{007DEE4C-2CC4-43D4-B519-0493D99EFC5A}"
19+
ProjectSection(SolutionItems) = preProject
20+
README.md = README.md
21+
EndProjectSection
22+
EndProject
23+
Global
24+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
25+
Debug|Any CPU = Debug|Any CPU
26+
Release|Any CPU = Release|Any CPU
27+
EndGlobalSection
28+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
29+
{4C899376-9F35-402F-A883-4D40CA9924E9}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
30+
{4C899376-9F35-402F-A883-4D40CA9924E9}.Debug|Any CPU.Build.0 = Debug|Any CPU
31+
{4C899376-9F35-402F-A883-4D40CA9924E9}.Release|Any CPU.ActiveCfg = Release|Any CPU
32+
{4C899376-9F35-402F-A883-4D40CA9924E9}.Release|Any CPU.Build.0 = Release|Any CPU
33+
{3C865B64-575D-4595-AD5D-FC2ABEDCF6E9}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
34+
{3C865B64-575D-4595-AD5D-FC2ABEDCF6E9}.Debug|Any CPU.Build.0 = Debug|Any CPU
35+
{3C865B64-575D-4595-AD5D-FC2ABEDCF6E9}.Release|Any CPU.ActiveCfg = Release|Any CPU
36+
{3C865B64-575D-4595-AD5D-FC2ABEDCF6E9}.Release|Any CPU.Build.0 = Release|Any CPU
37+
{BA3F38B1-1A27-4313-8F96-67067AA0FCCB}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
38+
{BA3F38B1-1A27-4313-8F96-67067AA0FCCB}.Debug|Any CPU.Build.0 = Debug|Any CPU
39+
{BA3F38B1-1A27-4313-8F96-67067AA0FCCB}.Release|Any CPU.ActiveCfg = Release|Any CPU
40+
{BA3F38B1-1A27-4313-8F96-67067AA0FCCB}.Release|Any CPU.Build.0 = Release|Any CPU
41+
{1F56F913-C950-4B10-987C-80FF63DDF80F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
42+
{1F56F913-C950-4B10-987C-80FF63DDF80F}.Debug|Any CPU.Build.0 = Debug|Any CPU
43+
{1F56F913-C950-4B10-987C-80FF63DDF80F}.Release|Any CPU.ActiveCfg = Release|Any CPU
44+
{1F56F913-C950-4B10-987C-80FF63DDF80F}.Release|Any CPU.Build.0 = Release|Any CPU
45+
{AAA9CD12-64BB-4BB1-B6BC-BC32FD067148}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
46+
{AAA9CD12-64BB-4BB1-B6BC-BC32FD067148}.Debug|Any CPU.Build.0 = Debug|Any CPU
47+
{AAA9CD12-64BB-4BB1-B6BC-BC32FD067148}.Release|Any CPU.ActiveCfg = Release|Any CPU
48+
{AAA9CD12-64BB-4BB1-B6BC-BC32FD067148}.Release|Any CPU.Build.0 = Release|Any CPU
49+
EndGlobalSection
50+
GlobalSection(SolutionProperties) = preSolution
51+
HideSolutionNode = FALSE
52+
EndGlobalSection
53+
GlobalSection(NestedProjects) = preSolution
54+
{1F56F913-C950-4B10-987C-80FF63DDF80F} = {B8CC92F3-BD6F-4488-8F21-0E5EB082E86F}
55+
{AAA9CD12-64BB-4BB1-B6BC-BC32FD067148} = {B8CC92F3-BD6F-4488-8F21-0E5EB082E86F}
56+
EndGlobalSection
57+
GlobalSection(ExtensibilityGlobals) = postSolution
58+
SolutionGuid = {2FF41457-09E2-4EF0-9B00-4C765FF973B6}
59+
EndGlobalSection
60+
EndGlobal

‎LibOne/IBinarySerializer.cs‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
using System.IO;
2+
3+
namespace LibOne
4+
{
5+
public interface IBinarySerializer
6+
{
7+
string Description { get; }
8+
9+
Stream Serialize<T>(T obj);
10+
11+
T Deserialize<T>(Stream stream);
12+
}
13+
}

‎LibOne/LibOne.csproj‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>netstandard2.1</TargetFramework>
5+
</PropertyGroup>
6+
7+
</Project>

‎LibOne/LibOneBinarySerializer.cs‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
using System.IO;
2+
using System.Runtime.Serialization;
3+
using System.Runtime.Serialization.Formatters.Binary;
4+
5+
namespace LibOne
6+
{
7+
public class LibOneBinarySerializer : IBinarySerializer
8+
{
9+
private readonly IFormatter _formatter;
10+
11+
public LibOneBinarySerializer()
12+
{
13+
_formatter = new BinaryFormatter();
14+
}
15+
16+
public string Description => "Binary serialization based on BinaryFormatter";
17+
18+
public Stream Serialize<T>(T obj)
19+
{
20+
var stream = new MemoryStream();
21+
_formatter.Serialize(stream, obj);
22+
stream.Seek(0, SeekOrigin.Begin);
23+
return stream;
24+
}
25+
26+
public T Deserialize<T>(Stream stream)
27+
{
28+
var obj = (T)_formatter.Deserialize(stream);
29+
return obj;
30+
}
31+
}
32+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /