为了账号安全,请及时绑定邮箱和手机立即绑定

WCF绑定使用的序列化性能测试

WCF绑定使用的序列化性能测试

狐的传说 2019-11-30 14:13:18
我有以下对象:public partial class Game{    public bool Finished { get; set; }    public Guid GameGUID { get; set; }    public long GameID { get; set; }    public bool GameSetup { get; set; }    public Nullable<int> MaximumCardsInDeck { get; set; }    public Player Player { get; set; }    public Player Player1 { get; set; }    public bool Player1Connected { get; set; }    public bool Player1EnvironmentSetup { get; set; }    public long Player1ID { get; set; }    public int Player1Won { get; set; }    public bool Player2Connected { get; set; }    public bool Player2EnvironmentSetup { get; set; }    public long Player2ID { get; set; }    public int Player2Won { get; set; }    public int Round { get; set; }    public Nullable<int> RoundsToWin { get; set; }    public bool Started { get; set; }    public string StateXML { get; set; }    public Nullable<DateTime> TimeEnded { get; set; }    public Nullable<int> TimeLimitPerTurn { get; set; }    public byte[] TimeStamp { get; set; }    public Nullable<DateTime> TimeStarted { get; set; }    }本课程将包含一些测试数据。我需要比较WCF服务的不同形式的绑定所使用的不同序列化程序的性能:basicHttpBinding => SoapFormatter(TextFormatter?)binaryBinding => BinaryFormatterXMLFormatter我需要详细做的是:现在获取要序列化的对象 的大小浆化后 达到现在的大小序列化的 时间时间反序列化我已经尝试过一些东西,但是我有点挣扎。也许已经有一些用于这种测量的简单代码。
查看完整描述

3 回答

?
慕村225694

TA贡献1880条经验 获得超4个赞

我已经修改了@Marc的基准测试源代码,并为ServiceStack的JSV和JSON序列化器添加了结果。这是我的3yo iMac上的结果:


BinaryFormatter

Length: 1313

Serialize: 3959

Deserialize: 3395


XmlSerializer

Length: 1049

Serialize: 1710

Deserialize: 2716


DataContractSerializer

Length: 911

Serialize: 712

Deserialize: 2117


NetDataContractSerializer

Length: 1138

Serialize: 1093

Deserialize: 4825


TypeSerializer

Length: 431

Serialize: 496

Deserialize: 887


JsonSerializer

Length: 507

Serialize: 558

Deserialize: 1213

这是我添加到上述@Marc基准中的源代码。


GC.Collect(GC.MaxGeneration, GCCollectionMode.Forced);

GC.WaitForPendingFinalizers();

var sbJsv = new StringBuilder(4096);

using (var sw = new StringWriter(sbJsv))

{

    Console.WriteLine();

    Console.WriteLine(typeof(TypeSerializer).Name);

    TypeSerializer.SerializeToWriter(orig, sw);

    var jsv = sbJsv.ToString();

    Console.WriteLine("Length: " + sbJsv.Length);

    TypeSerializer.DeserializeFromString<Game>(jsv);


    var watch = Stopwatch.StartNew();

    for (int i = 0; i < LOOP; i++)

    {

        sbJsv.Length = 0;

        TypeSerializer.SerializeToWriter(orig, sw);

    }

    watch.Stop();

    Console.WriteLine("Serialize: " + watch.ElapsedMilliseconds);


    watch = Stopwatch.StartNew();

    for (int i = 0; i < LOOP; i++)

    {

        TypeSerializer.DeserializeFromString<Game>(jsv);

    }

    watch.Stop();

    Console.WriteLine("Deserialize: " + watch.ElapsedMilliseconds);

}


GC.Collect(GC.MaxGeneration, GCCollectionMode.Forced);

GC.WaitForPendingFinalizers();

var sbJson = new StringBuilder(4096);

using (var sw = new StringWriter(sbJson))

{

    Console.WriteLine();

    Console.WriteLine(typeof(JsonSerializer).Name);

    JsonSerializer.SerializeToWriter(orig, sw);

    var json = sbJson.ToString();

    Console.WriteLine("Length: " + sbJson.Length);

    JsonSerializer.DeserializeFromString<Game>(json);


    var watch = Stopwatch.StartNew();

    for (int i = 0; i < LOOP; i++)

    {

        sbJson.Length = 0;

        JsonSerializer.SerializeToWriter(orig, sw);

    }

    watch.Stop();

    Console.WriteLine("Serialize: " + watch.ElapsedMilliseconds);


    watch = Stopwatch.StartNew();

    for (int i = 0; i < LOOP; i++)

    {

        JsonSerializer.DeserializeFromString<Game>(json);

    }

    watch.Stop();

    Console.WriteLine("Deserialize: " + watch.ElapsedMilliseconds);

}

注意:我无法获得他使用的@Marc的protobuf-net v2 r352 dll,因此我不得不将protobuf-net基准测试注释掉。


查看完整回答
反对 回复 2019-11-30
  • 3 回答
  • 0 关注
  • 628 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信