Skip to content

Instantly share code, notes, and snippets.

@szehetner
Created April 13, 2018 21:12
Show Gist options
  • Save szehetner/6c11a0545f0600c1c5ae5cd23527a1d2 to your computer and use it in GitHub Desktop.
Save szehetner/6c11a0545f0600c1c5ae5cd23527a1d2 to your computer and use it in GitHub Desktop.
ProtoWriter.DemandSpace Benchmark
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Running;
using ProtoBuf;
using ProtoBuf.Meta;
namespace PerfTests
{
public class Program
{
static void Main(string[] args)
{
BenchmarkRunner.Run<Program>();
Console.ReadLine();
}
private RuntimeTypeModel _model = CreateModel();
private DummDto _dto = DummDto.Create();
private MemoryStream _memoryStream;
[GlobalSetup]
public void Setup()
{
_memoryStream = new MemoryStream();
_memoryStream.SetLength(0);
_model.Serialize(_memoryStream, _dto);
_memoryStream.Position = 0;
}
[Benchmark]
public void RunSerializer()
{
for (int i = 0; i < 100_000; i++)
{
_memoryStream.Position = 0;
using (ProtoWriter writer = new ProtoWriter(_memoryStream, _model, null))
{
_model.Serialize(writer, _dto);
}
}
}
static RuntimeTypeModel CreateModel()
{
var model = TypeModel.Create();
model.Add(typeof(DummDto), true);
return model;
}
}
[ProtoContract]
public class DummDto
{
[ProtoMember(1)]
public int IntField1 { get; set; }
[ProtoMember(2)]
public int IntField2 { get; set; }
[ProtoMember(3)]
public int IntField3 { get; set; }
[ProtoMember(4)]
public long LongField1 { get; set; }
[ProtoMember(5)]
public long LongField2 { get; set; }
[ProtoMember(6)]
public long LongField3 { get; set; }
[ProtoMember(7)]
public string StringField1 { get; set; }
[ProtoMember(8)]
public string StringField2 { get; set; }
[ProtoMember(9)]
public string StringField3 { get; set; }
public static DummDto Create()
{
return new DummDto
{
IntField1 = 1,
IntField2 = 2,
IntField3 = 323,
LongField1 = 468678,
LongField2 = 52323443,
LongField3 = 61542134234,
StringField1 = "abc",
StringField2 = "adsfasdf",
StringField3 = "qewrwtr"
};
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment