现在的位置: 首页 > 综合 > 正文

MsgPack MessagePack 测试

2012年07月08日 ⁄ 综合 ⁄ 共 12290字 ⁄ 字号 评论关闭


using System.Runtime.CompilerServices;
[assembly: InternalsVisibleTo(MsgPack.CompiledPacker.MethodBuilderPacker.AssemblyName)]
//========================================================================================================
//
// Copyright 2011 Kazuki Oikawa
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
//using NUnit.Framework;
namespace Test
{
using System;
using System.Collections.Generic;
using System.Linq;
using Test.Share;
using MsgPack;
using Microshaoft;
//using System.Diagnostics;
//using Microsoft.VisualStudio.TestTools.UnitTesting;
//[TestFixture]
public class Class1
{
static void Main(string[] args)
{
Class1 c = new Class1();
int iterations = 1000;
CodeTimer.ParallelTime
(
"MsgPack ObjectPacker 并行测试",
iterations,
() =>
{
c.Test_ObjectPacker();
}
);
CodeTimer.Time
(
"MsgPack ObjectPacker 串行测试",
iterations,
() =>
{
c.Test_ObjectPacker();
}
);
CodeTimer.ParallelTime
(
"MsgPack CompiledPacker 并行测试",
iterations,
() =>
{
c.Test_CompiledPacker();
}
);
CodeTimer.Time
(
"MsgPack CompiledPacker 串行测试",
iterations,
() =>
{
c.Test_CompiledPacker();
}
);
//CodeTimer.ParallelTime
// (
// "MsgPack BoxingPacker 并行测试",
// iterations,
// () =>
// {
// c.Test_BoxingPacker();
// }
// );
//CodeTimer.Time
// (
// "MsgPack BoxingPacker 串行测试",
// iterations,
// () =>
// {
// c.Test_BoxingPacker();
// }
// );
CodeTimer.ParallelTime
(
"MsgPack ObjectPacker/CompiledPacker 并行测试",
iterations,
() =>
{
c.Test_HybridPacker();
}
);
CodeTimer.Time
(
"MsgPack ObjectPacker/CompiledPacker 串行测试",
iterations,
() =>
{
c.Test_HybridPacker();
}
);
Console.WriteLine("Hello World");
Console.WriteLine(Environment.Version.ToString());
Console.ReadLine();
}
public void Test_HybridPacker()
{
ObjectMsgPackHeader header = new ObjectMsgPackHeader();
header.From = "Microshaoft";
header.ToList = new string[] { "m1", "m2", "m3" };
//header.Instance = header;
ObjectMsgPackBody body = new ObjectMsgPackBody();
body.Parameter1 = "asdasdas";
body.Parameter2 = 1000;
body.ParameterX = new List<string>();
body.ParameterX.Add("aaaaa");
body.ParameterX.Add("bbbbb");
//body.Instance = body;
byte[] buffer;
ObjectMsgPack message = new ObjectMsgPack();
message.Header = new CompiledPacker().Pack(header);
message.Body = new ObjectPacker().Pack(body);
message.SenderID = "asdasd";
message.Signature = new byte[10];
message.TimeStamp = "2012-03-22";
buffer = new ObjectPacker().Pack(message);
//Console.WriteLine("HybridPacker Buffer.Length {0}", buffer.Length);
ObjectMsgPack x = new ObjectPacker().Unpack<ObjectMsgPack>(buffer);
ObjectMsgPackHeader y = new CompiledPacker().Unpack<ObjectMsgPackHeader>(x.Header);
ObjectMsgPackBody z = new ObjectPacker().Unpack<ObjectMsgPackBody>(x.Body);
Print(x, y, z);
}
public void Test_BoxingPacker()
{
ObjectMsgPackHeader header = new ObjectMsgPackHeader();
header.From = "Microshaoft";
header.ToList = new string[] { "m1", "m2", "m3" };
//header.Instance = header;
ObjectMsgPackBody body = new ObjectMsgPackBody();
body.Parameter1 = "asdasdas";
body.Parameter2 = 1000;
body.ParameterX = new List<string>();
body.ParameterX.Add("aaaaa");
body.ParameterX.Add("bbbbb");
//body.Instance = body;
byte[] buffer;
ObjectMsgPack message = new ObjectMsgPack();
message.Header = new BoxingPacker().Pack(header);
message.Body = new BoxingPacker().Pack(body);
message.SenderID = "asdasd";
message.Signature = new byte[10];
message.TimeStamp = "2012-03-22";
buffer = new BoxingPacker().Pack(message);
//Console.WriteLine("BoxingPacker Buffer.Length {0}", buffer.Length);
ObjectMsgPack x = (ObjectMsgPack) new BoxingPacker().Unpack(buffer);
ObjectMsgPackHeader y = (ObjectMsgPackHeader) new BoxingPacker().Unpack(x.Header);
ObjectMsgPackBody z = (ObjectMsgPackBody) new BoxingPacker().Unpack(x.Body);
Print(x, y, z);
}
public void Test_CompiledPacker()
{
ObjectMsgPackHeader header = new ObjectMsgPackHeader();
header.From = "Microshaoft";
header.ToList = new string[] { "m1", "m2", "m3" };
//header.Instance = header;
ObjectMsgPackBody body = new ObjectMsgPackBody();
body.Parameter1 = "asdasdas";
body.Parameter2 = 1000;
body.ParameterX = new List<string>();
body.ParameterX.Add("aaaaa");
body.ParameterX.Add("bbbbb");
//body.Instance = body;
byte[] buffer;
ObjectMsgPack message = new ObjectMsgPack();
message.Header = new CompiledPacker().Pack(header);
message.Body = new CompiledPacker().Pack(body);
message.SenderID = "asdasd";
message.Signature = new byte[10];
message.TimeStamp = "2012-03-22";
buffer = new CompiledPacker().Pack(message);
//Console.WriteLine("CompiledPacker Buffer.Length {0}", buffer.Length);
ObjectMsgPack x = new CompiledPacker().Unpack<ObjectMsgPack>(buffer);
ObjectMsgPackHeader y = new CompiledPacker().Unpack<ObjectMsgPackHeader>(x.Header);
ObjectMsgPackBody z = new CompiledPacker().Unpack<ObjectMsgPackBody>(x.Body);
Print(x, y, z);
}
public void Test_ObjectPacker()
{
ObjectMsgPackHeader header = new ObjectMsgPackHeader();
header.From = "Microshaoft";
header.ToList = new string[] { "m1", "m2", "m3" };
//header.Instance = header;
ObjectMsgPackBody body = new ObjectMsgPackBody();
body.Parameter1 = "asdasdas";
body.Parameter2 = 1000;
body.ParameterX = new List<string>();
body.ParameterX.Add("aaaaa");
body.ParameterX.Add("bbbbb");
//body.Instance = body;
byte[] buffer;
ObjectMsgPack message = new ObjectMsgPack();
message.Header = new ObjectPacker().Pack(header);
message.Body = new ObjectPacker().Pack(body);
message.SenderID = "asdasd";
message.Signature = new byte[10];
message.TimeStamp = "2012-03-22";
buffer = new ObjectPacker().Pack(message);
//Console.WriteLine("ObjectPacker Buffer.Length {0}", buffer.Length);
ObjectMsgPack x = new ObjectPacker().Unpack<ObjectMsgPack>(buffer);
ObjectMsgPackHeader y = new ObjectPacker().Unpack<ObjectMsgPackHeader>(x.Header);
ObjectMsgPackBody z = new ObjectPacker().Unpack<ObjectMsgPackBody>(x.Body);
Print(x, y, z);
}
public void Print(ObjectMsgPack x, ObjectMsgPackHeader y, ObjectMsgPackBody z)
{
return;
Console.WriteLine(x.SenderID);
Console.WriteLine(x.Signature.Length);
Console.WriteLine(x.TimeStamp);
Console.WriteLine(y.From);
y.ToList.ToList().ForEach
(
s =>
{
Console.WriteLine(s);
}
);
Console.WriteLine(z.Parameter1);
Console.WriteLine(z.Parameter12);
Console.WriteLine(z.FF.F1);
Console.WriteLine(z.FF.F2);
z.Parameter3.ToList().ForEach
(
s =>
{
Console.WriteLine(s);
}
);
}
}
}
namespace Microshaoft
{
using System;
using System.Diagnostics;
using System.Threading;
using System.Threading.Tasks;
using System.Runtime.InteropServices;
public static class CodeTimer
{
public static void Initialize()
{
Process.GetCurrentProcess().PriorityClass = ProcessPriorityClass.High;
Thread.CurrentThread.Priority = ThreadPriority.Highest;
Time("", 1, () => { });
}
public static void ParallelTime(string name, int iteration, Action action)
{
if (string.IsNullOrEmpty(name))
{
return;
}
// 1.
ConsoleColor currentForeColor = Console.ForegroundColor;
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine(name);
// 2.
GC.Collect(GC.MaxGeneration, GCCollectionMode.Forced);
int[] gcCounts = new int[GC.MaxGeneration + 1];
for (int i = 0; i <= GC.MaxGeneration; i++)
{
gcCounts[i] = GC.CollectionCount(i);
}
// 3.
Stopwatch watch = new Stopwatch();
watch.Start();
ulong cycleCount = GetCycleCount();
Parallel.For
(
0
, iteration
, i =>
{
action();
}
);
ulong cpuCycles = GetCycleCount() - cycleCount;
watch.Stop();
// 4.
Console.ForegroundColor = currentForeColor;
Console.WriteLine("\tTime Elapsed:\t" + watch.ElapsedMilliseconds.ToString("N0") + "ms");
Console.WriteLine("\tCPU Cycles:\t" + cpuCycles.ToString("N0"));
// 5.
for (int i = 0; i <= GC.MaxGeneration; i++)
{
int count = GC.CollectionCount(i) - gcCounts[i];
Console.WriteLine("\tGen " + i + ": \t\t" + count);
}
Console.WriteLine();
}
public static void Time(string name, int iteration, Action action)
{
if (string.IsNullOrEmpty(name))
{
return;
}
// 1.
ConsoleColor currentForeColor = Console.ForegroundColor;
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine(name);
// 2.
GC.Collect(GC.MaxGeneration, GCCollectionMode.Forced);
int[] gcCounts = new int[GC.MaxGeneration + 1];
for (int i = 0; i <= GC.MaxGeneration; i++)
{
gcCounts[i] = GC.CollectionCount(i);
}
// 3.
Stopwatch watch = new Stopwatch();
watch.Start();
ulong cycleCount = GetCycleCount();
for (int i = 0; i < iteration; i++)
{
action();
}
ulong cpuCycles = GetCycleCount() - cycleCount;
watch.Stop();
// 4.
Console.ForegroundColor = currentForeColor;
Console.WriteLine("\tTime Elapsed:\t" + watch.ElapsedMilliseconds.ToString("N0") + "ms");
Console.WriteLine("\tCPU Cycles:\t" + cpuCycles.ToString("N0"));
// 5.
for (int i = 0; i <= GC.MaxGeneration; i++)
{
int count = GC.CollectionCount(i) - gcCounts[i];
Console.WriteLine("\tGen " + i + ": \t\t" + count);
}
Console.WriteLine();
}
private static ulong GetCycleCount()
{
ulong cycleCount = 0;
QueryThreadCycleTime(GetCurrentThread(), ref cycleCount);
return cycleCount;
}
[DllImport("kernel32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool QueryThreadCycleTime(IntPtr threadHandle, ref ulong cycleTime);
[DllImport("kernel32.dll")]
static extern IntPtr GetCurrentThread();
}
}
//===============================================================================================================
namespace Test.Share
{
using System;
using System.Collections.Generic;
public class ObjectMsgPack
{
public string SenderID;
public string TimeStamp;
public byte[] Signature;
public byte[] Header;
public byte[] Body;
}
public class ObjectMsgPackHeader
{
//public ObjectMsgPack Container;
//public ObjectMsgPackHeader Instance;
public string From;
public string[] ToList;
}
public class ObjectMsgPackBody
{
//public ObjectMsgPack Container;
//public ObjectMsgPackBody Instance;
public string Parameter1 = "aaaa";
public int Parameter2 = 100;
public string Parameter3 = "aaaa";
public int Parameter4 = 100;
public string Parameter5 = "aaaa";
public int Parameter6 = 100;
public string Parameter7 = "aaaa";
public int Parameter8 = 100;
public string Parameter9 = "aaaa";
public int Parameter10 = 100;
public string Parameter11 = "aaaa";
public int Parameter12 = -101;
public List<string> ParameterX;
public ComplexType FF = new ComplexType();
}
public class ComplexType
{
public string F1 = "asdasd";
public DateTime F2 = DateTime.Parse("2012-03-30 00:00:00.00000");
}
}
//================================================================================================
namespace MsgPack
{
//
// Copyright 2011 Kazuki Oikawa
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
public enum TypePrefixes : byte
{
PositiveFixNum = 0x00, // 0x00 - 0x7f
NegativeFixNum = 0xe0, // 0xe0 - 0xff
Nil = 0xc0,
False = 0xc2,
True = 0xc3,
Float = 0xca,
Double = 0xcb,
UInt8 = 0xcc,
UInt16 = 0xcd,
UInt32 = 0xce,
UInt64 = 0xcf,
Int8 = 0xd0,
Int16 = 0xd1,
Int32 = 0xd2,
Int64 = 0xd3,
Raw16 = 0xda,
Raw32 = 0xdb,
Array16 = 0xdc,
Array32 = 0xdd,
Map16 = 0xde,
Map32 = 0xdf,
FixRaw = 0xa0, // 0xa0 - 0xbf
FixArray = 0x90, // 0x90 - 0x9f
FixMap = 0x80, // 0x80 - 0x8f
}
}
namespace MsgPack
{
//
// Copyright 2011 Kazuki Oikawa
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
using System;
using System.Collections.Generic;
using System.Reflection;
public class ReflectionCacheEntry
{
const BindingFlags FieldBindingFlags = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.GetField | BindingFlags.SetField;
public ReflectionCacheEntry(Type t)
{
FieldInfo[] fields = t.GetFields(FieldBindingFlags);
IDictionary<string, FieldInfo> map = new Dictionary<string, FieldInfo>(fields.Length);
for (int i = 0; i < fields.Length; i++)
{
FieldInfo f = fields[i];
string name = f.Name;
int pos;
if (name[0] == '<' && (pos = name.IndexOf('>')) > 1)
{
name = name.Substring(1, pos - 1); // Auto-Property (\<.+\>) <ab>
}
map[name] = f;
}
FieldMap = map;
}
public IDictionary<string, FieldInfo> FieldMap { get; private set; }
}
}
namespace MsgPack
{
//
// Copyright 2011 Kazuki Oikawa
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing perm

抱歉!评论已关闭.