Created
December 22, 2012 05:44
-
-
Save kazuk/4357661 to your computer and use it in GitHub Desktop.
performace test for msgpack-cli TestStringMedium
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Diagnostics; | |
using System.IO; | |
using System.Linq; | |
using System.Text; | |
using MsgPack; | |
namespace PerfTest | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
var sw = Stopwatch.StartNew(); | |
for (int i = 0; i < 10; i++) | |
{ | |
var avg = TestMedium(); | |
} | |
sw.Stop(); | |
} | |
private static double TestMedium() | |
{ | |
Random random = new Random(); | |
var sb = new StringBuilder(1000*1000*200); | |
var avg = 0.0; | |
// medium size string | |
for (int i = 0; i < 100; i++) | |
{ | |
sb.Length = 0; | |
int len = (int) random.Next()%100 + (1 << 15); | |
for (int j = 0; j < len; j++) | |
{ | |
sb.Append('a' + ((int) random.Next()) & 26); | |
} | |
avg = (avg + sb.Length)/2.0; | |
for (int x = 0; x < 10000; x++) | |
{ | |
TestString(sb.ToString()); | |
} | |
} | |
return avg; | |
} | |
private static void TestString(String val) | |
{ | |
var output = new MemoryStream(); | |
Packer.Create(output).PackString(val); | |
MessagePackObject obj = UnpackOne(output); | |
// Assert.AreEqual(val, obj.AsString()); | |
// Assert.IsTrue(obj.IsTypeOf<string>().GetValueOrDefault()); | |
} | |
private static bool _traceUnpackingBytes = false; | |
private static MessagePackObject UnpackOne(MemoryStream output) | |
{ | |
output.Seek(0L, SeekOrigin.Begin); | |
if (_traceUnpackingBytes) | |
{ | |
Console.WriteLine("Unpaking:"); | |
foreach (var b in output.ToArray().Take(100)) | |
{ | |
Console.Write(b.ToString("x2")); | |
} | |
Console.WriteLine(); | |
} | |
return Unpacking.UnpackObject(output); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment