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

我有以下对象:


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 => BinaryFormatter

XMLFormatter

我需要详细做的是:


现在获取要序列化的对象 的大小

浆化后 达到现在的大小

序列化的 时间

时间反序列化

我已经尝试过一些东西,但是我有点挣扎。也许已经有一些用于这种测量的简单代码。


狐的传说
浏览 605回答 3
3回答

慕村225694

我已经修改了@Marc的基准测试源代码,并为ServiceStack的JSV和JSON序列化器添加了结果。这是我的3yo iMac上的结果:BinaryFormatterLength: 1313Serialize: 3959Deserialize: 3395XmlSerializerLength: 1049Serialize: 1710Deserialize: 2716DataContractSerializerLength: 911Serialize: 712Deserialize: 2117NetDataContractSerializerLength: 1138Serialize: 1093Deserialize: 4825TypeSerializerLength: 431Serialize: 496Deserialize: 887JsonSerializerLength: 507Serialize: 558Deserialize: 1213这是我添加到上述@Marc基准中的源代码。GC.Collect(GC.MaxGeneration, GCCollectionMode.Forced);GC.WaitForPendingFinalizers();var sbJsv = new StringBuilder(4096);using (var sw = new StringWriter(sbJsv)){&nbsp; &nbsp; Console.WriteLine();&nbsp; &nbsp; Console.WriteLine(typeof(TypeSerializer).Name);&nbsp; &nbsp; TypeSerializer.SerializeToWriter(orig, sw);&nbsp; &nbsp; var jsv = sbJsv.ToString();&nbsp; &nbsp; Console.WriteLine("Length: " + sbJsv.Length);&nbsp; &nbsp; TypeSerializer.DeserializeFromString<Game>(jsv);&nbsp; &nbsp; var watch = Stopwatch.StartNew();&nbsp; &nbsp; for (int i = 0; i < LOOP; i++)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; sbJsv.Length = 0;&nbsp; &nbsp; &nbsp; &nbsp; TypeSerializer.SerializeToWriter(orig, sw);&nbsp; &nbsp; }&nbsp; &nbsp; watch.Stop();&nbsp; &nbsp; Console.WriteLine("Serialize: " + watch.ElapsedMilliseconds);&nbsp; &nbsp; watch = Stopwatch.StartNew();&nbsp; &nbsp; for (int i = 0; i < LOOP; i++)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; TypeSerializer.DeserializeFromString<Game>(jsv);&nbsp; &nbsp; }&nbsp; &nbsp; watch.Stop();&nbsp; &nbsp; Console.WriteLine("Deserialize: " + watch.ElapsedMilliseconds);}GC.Collect(GC.MaxGeneration, GCCollectionMode.Forced);GC.WaitForPendingFinalizers();var sbJson = new StringBuilder(4096);using (var sw = new StringWriter(sbJson)){&nbsp; &nbsp; Console.WriteLine();&nbsp; &nbsp; Console.WriteLine(typeof(JsonSerializer).Name);&nbsp; &nbsp; JsonSerializer.SerializeToWriter(orig, sw);&nbsp; &nbsp; var json = sbJson.ToString();&nbsp; &nbsp; Console.WriteLine("Length: " + sbJson.Length);&nbsp; &nbsp; JsonSerializer.DeserializeFromString<Game>(json);&nbsp; &nbsp; var watch = Stopwatch.StartNew();&nbsp; &nbsp; for (int i = 0; i < LOOP; i++)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; sbJson.Length = 0;&nbsp; &nbsp; &nbsp; &nbsp; JsonSerializer.SerializeToWriter(orig, sw);&nbsp; &nbsp; }&nbsp; &nbsp; watch.Stop();&nbsp; &nbsp; Console.WriteLine("Serialize: " + watch.ElapsedMilliseconds);&nbsp; &nbsp; watch = Stopwatch.StartNew();&nbsp; &nbsp; for (int i = 0; i < LOOP; i++)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; JsonSerializer.DeserializeFromString<Game>(json);&nbsp; &nbsp; }&nbsp; &nbsp; watch.Stop();&nbsp; &nbsp; Console.WriteLine("Deserialize: " + watch.ElapsedMilliseconds);}注意:我无法获得他使用的@Marc的protobuf-net v2 r352 dll,因此我不得不将protobuf-net基准测试注释掉。
打开App,查看更多内容
随时随地看视频慕课网APP