Skip to content

Commit ca37d11

Browse files
authored
Added benchmarks project (#6)
***NO_CI***
1 parent c239a0d commit ca37d11

17 files changed

+1793
-13
lines changed

NFUnitTest/NFUnitTest.nfproj

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<?xml version="1.0" encoding="utf-8"?>
1+
<?xml version="1.0" encoding="utf-8"?>
22
<Project ToolsVersion="Current" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
33
<PropertyGroup Label="Globals">
44
<NanoFrameworkProjectSystemPath>$(MSBuildExtensionsPath)\nanoFramework\v1.0\</NanoFrameworkProjectSystemPath>

README.md

+52-12
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
[![Quality Gate Status](https://sonarcloud.io/api/project_badges/measure?project=nanoframework_nanoFramework.MessagePack&metric=alert_status)](https://sonarcloud.io/dashboard?id=nanoframework_nanoFramework.MessagePack) [![Reliability Rating](https://sonarcloud.io/api/project_badges/measure?project=nanoframework_nanoFramework.MessagePack&metric=reliability_rating)](https://sonarcloud.io/dashboard?id=nanoframework_nanoFramework.MessagePack) [![NuGet](https://img.shields.io/nuget/dt/nanoFramework.MessagePack.svg?label=NuGet&style=flat&logo=nuget)](https://www.nuget.org/packages/nanoFramework.MessagePack/) [![#yourfirstpr](https://img.shields.io/badge/first--timers--only-friendly-blue.svg)](https://github.com/nanoframework/Home/blob/main/CONTRIBUTING.md) [![Discord](https://img.shields.io/discord/478725473862549535.svg?logo=discord&logoColor=white&label=Discord&color=7289DA)](https://discord.gg/gCyBu8T)
1+
[![Quality Gate Status](https://sonarcloud.io/api/project_badges/measure?project=nanoframework_nanoFramework.MessagePack&metric=alert_status)](https://sonarcloud.io/dashboard?id=nanoframework_nanoFramework.MessagePack) [![Reliability Rating](https://sonarcloud.io/api/project_badges/measure?project=nanoframework_nanoFramework.MessagePack&metric=reliability_rating)](https://sonarcloud.io/dashboard?id=nanoframework_nanoFramework.MessagePack) [![NuGet](https://img.shields.io/nuget/dt/nanoFramework.MessagePack.svg?label=NuGet&style=flat&logo=nuget)](https://www.nuget.org/packages/nanoFramework.MessagePack/) [![#yourfirstpr](https://img.shields.io/badge/first--timers--only-friendly-blue.svg)](https://github.com/nanoframework/Home/blob/main/CONTRIBUTING.md) [![Discord](https://img.shields.io/discord/478725473862549535.svg?logo=discord&logoColor=white&label=Discord&color=7289DA)](https://discord.gg/gCyBu8T)
22

33
![nanoFramework logo](https://raw.githubusercontent.com/nanoframework/Home/main/resources/logo/nanoFramework-repo-logo.png)
44

@@ -151,7 +151,7 @@ After completing these steps, the serialization/deserialization of the object fo
151151
{
152152
WordDictionary = new ArrayList
153153
{
154-
"MessagePak",
154+
"MessagePack",
155155
"Hello",
156156
"at",
157157
"nanoFramework!",
@@ -178,30 +178,33 @@ After completing these steps, the serialization/deserialization of the object fo
178178
{
179179
StringBuilder sb = new();
180180
var length = reader.ReadArrayLength();
181-
182-
for(int i = 0; i < length; i++)
181+
var intConverter = ConverterContext.GetConverter(typeof(int));
182+
183+
for (int i = 0; i < length; i++)
183184
{
184-
sb.Append(SharedWordDictionary.WordDictionary[i]);
185+
int wordIndex = (int)intConverter.Read(reader)!;
186+
sb.Append(SharedWordDictionary.WordDictionary[wordIndex]);
185187
sb.Append(' ');
186188
}
187189
if (sb.Length > 0)
188190
sb.Remove(sb.Length - 1, 1);
189-
191+
190192
return new SecureMessage(sb.ToString());
191193
}
192-
194+
193195
public void Write(SecureMessage value, [NotNull] IMessagePackWriter writer)
194196
{
195197
var messageWords = value.Message.Split(' ');
196-
197-
uint length = BitConverter.ToUInt32(BitConverter.GetBytes(messageWords.Length), 0);
198+
199+
uint length = (uint)messageWords.Length;
198200
writer.WriteArrayHeader(length);
199-
201+
200202
var intConverter = ConverterContext.GetConverter(typeof(int));
201-
203+
202204
foreach (var word in messageWords)
203205
{
204-
intConverter.Write(SharedWordDictionary.WordDictionary.IndexOf(word), writer);
206+
int wordIndex = SharedWordDictionary.WordDictionary.IndexOf(word);
207+
intConverter.Write(wordIndex, writer);
205208
}
206209
}
207210

@@ -245,6 +248,43 @@ After completing these steps, the serialization/deserialization of the object fo
245248
}
246249
```
247250

251+
## Benchmarks
252+
253+
The measurements were carried out on the developer's local computer in a virtual nanoDevice:
254+
255+
```text
256+
===============================================================
257+
========== Comparative benchmarks data ==========
258+
========== ==========
259+
========== Json string size: 3957 bytes ==========
260+
========== BinaryFormatter array size: 1079 bytes ==========
261+
========== MessagePack array size: 2444 bytes ==========
262+
========== ==========
263+
===============================================================
264+
265+
Console export: ComparativeDeserializationBenchmark benchmark class.
266+
267+
| ------------------------------------------------------------------------------ |
268+
| MethodName | IterationCount | Mean | Min | Max |
269+
| ------------------------------------------------------------------------------ |
270+
| JsonDeserializationBenchmark | 10 | 27.5 ms | 22 ms | 37 ms |
271+
| BinaryDeserializationBenchmark | 10 | 0.1 ms | 0 ms | 1 ms |
272+
| MessagePackDeserializationBenchmark | 10 | 23.7 ms | 19 ms | 34 ms |
273+
| ------------------------------------------------------------------------------ |
274+
275+
Console export: ComparativeSerializationBenchmark benchmark class.
276+
277+
278+
| ---------------------------------------------------------------------------- |
279+
| MethodName | IterationCount | Mean | Min | Max |
280+
| ---------------------------------------------------------------------------- |
281+
| JsonSerializationBenchmark | 10 | 18.9 ms | 16 ms | 25 ms |
282+
| BinarySerializationBenchmark | 10 | 0.1 ms | 0 ms | 1 ms |
283+
| MessagePackSerializationBenchmark | 10 | 9.8 ms | 9 ms | 14 ms |
284+
| ---------------------------------------------------------------------------- |
285+
```
286+
As it can be seen from the benchmark results above, in what concerns speed and compaction, `MessagePack` performs better than the Json serializer. Comming at no surprise, Binary serialization is the most performant one.
287+
248288
## Acknowledgements
249289

250290
The initial version of the MessagePack library was coded by [Spirin Dmitriy](https://github.com/RelaxSpirit), who has kindly handed over the library to the .NET **nanoFramework** project.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
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+
4+
namespace nanoFramework.MessagePack.Benchmark.Base
5+
{
6+
using System;
7+
8+
/// <summary>
9+
/// Base benchmark class.
10+
/// </summary>
11+
public abstract class BaseIterationBenchmark
12+
{
13+
/// <summary>
14+
/// Internal iteration count.
15+
/// </summary>
16+
protected virtual int _iterationCount => 20;
17+
18+
/// <summary>
19+
/// Call iteration benchmark method.
20+
/// </summary>
21+
/// <param name="methodToRun">Iteration benchmark method.</param>
22+
/// <exception cref="ArgumentNullException">iteration benchmark method is <see cref="null"/></exception>
23+
protected void RunInIteration(Action methodToRun)
24+
{
25+
if (methodToRun == null)
26+
{
27+
throw new ArgumentNullException(nameof(methodToRun));
28+
}
29+
else
30+
{
31+
int step = 0;
32+
while (step++ < _iterationCount)
33+
{
34+
methodToRun();
35+
}
36+
}
37+
}
38+
}
39+
}

0 commit comments

Comments
 (0)