-
Notifications
You must be signed in to change notification settings - Fork 2
/
MsgpackTestCase.cs
337 lines (264 loc) · 10.9 KB
/
MsgpackTestCase.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
using SharpUnit;
using UnityEngine;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using Debug = UnityEngine.Debug;
namespace chomechomo
{
public class MsgpackTestCase : UnityTestCase {
public override void SetUp() {
Application.targetFrameRate = 1000;
}
public override void TearDown() {
}
[UnitTest]
public IEnumerator JsonBodyTest()
{
JSON json = gameObject.AddComponent("JSON") as JSON;
json.Type = Types.BODY;
yield return StartCoroutine(json.Request( GetParam("jtest1", 1) ));
Assert.False(json.IsError);
Assert.Equal((long)json.LastResult["int"], 14142);
Assert.Equal((string)json.LastResult["name"], "jtest1");
Assert.Equal(json.LastResult["nil"], null);
Assert.Equal((bool)json.LastResult["bool"], true);
IList<object> arr = json.LastResult["arr"] as IList<object>;
Assert.True( arr != null );
Assert.Equal((long)arr[2], 8);
IDictionary<string,object> map = json.LastResult["map"] as IDictionary<string, object>;
Assert.True( map != null );
Assert.Equal((string)map["foo"], "bar");
DoneTesting();
}
[UnitTest]
public IEnumerator MsgpackBodyTest()
{
Msgpack msgpack = gameObject.AddComponent("Msgpack") as Msgpack;
msgpack.Type = Types.BODY;
yield return StartCoroutine(msgpack.Request( GetParam("mtest1", 2) ));
Assert.False(msgpack.IsError);
Assert.Equal((uint)msgpack.LastResult["int"], 14142);
Assert.Equal((string)msgpack.LastResult["name"], "mtest1");
Assert.Equal(msgpack.LastResult["nil"], null);
Assert.Equal((bool)msgpack.LastResult["bool"], true);
IList<object> arr = msgpack.LastResult["arr"] as IList<object>;
Assert.True( arr != null );
Assert.Equal((int)arr[2], 8);
IDictionary<string,object> map = msgpack.LastResult["map"] as IDictionary<string, object>;
Assert.True( map != null );
Assert.Equal((string)map["foo"], "bar");
DoneTesting();
}
const int FUNC_RUNTIME = 3000;
const int HTTP_RUNTIME = 200;
const int DATA_LIST_COUNT = 100;
public float persec( int runtime, long millisec ) {
return (float)(((double)runtime / (double)millisec) * 1000d);
}
[UnitTest]
public IEnumerator JsonSerializeBenchmark()
{
JSON json = gameObject.AddComponent("JSON") as JSON;
Dictionary<string, object> data = GetBigData(DATA_LIST_COUNT);
byte[] bytes = json.Serialize( data );
Stopwatch stopWatch = new Stopwatch();
stopWatch.Start();
for (int i=0; i < FUNC_RUNTIME; i++) {
bytes = json.Serialize( data );
}
stopWatch.Stop();
Debug.Log("Json Serialize: n=" + FUNC_RUNTIME
+ "\r\n Time: " + (stopWatch.ElapsedMilliseconds / 1000f).ToString()
+ "\r\n Length: " + bytes.Length
+ "\r\n Rate: " + persec( FUNC_RUNTIME, stopWatch.ElapsedMilliseconds) + "/s");
Assert.True(true);
DoneTesting();
yield break;
}
[UnitTest]
public IEnumerator MsgpackSerializeBenchmark()
{
Msgpack msgpack = gameObject.AddComponent("Msgpack") as Msgpack;
Dictionary<string, object> data = GetBigData(DATA_LIST_COUNT);
byte[] bytes = msgpack.Serialize( data );;
Stopwatch stopWatch = new Stopwatch();
stopWatch.Start();
for (int i=0; i < FUNC_RUNTIME; i++) {
bytes = msgpack.Serialize( data );
}
stopWatch.Stop();
Debug.Log("Msgpack Serialize: n=" + FUNC_RUNTIME
+ "\r\n Time: " + (stopWatch.ElapsedMilliseconds / 1000f).ToString()
+ "\r\n Length: " + bytes.Length
+ "\r\n Rate: " + persec( FUNC_RUNTIME, stopWatch.ElapsedMilliseconds) + "/s");
Assert.True(true);
DoneTesting();
yield break;
}
[UnitTest]
public IEnumerator JsonDeserializeBenchmark()
{
JSON json = gameObject.AddComponent("JSON") as JSON;
Dictionary<string, object> data = GetBigData(DATA_LIST_COUNT);
byte[] bytes = json.Serialize( data );
Stopwatch stopWatch = new Stopwatch();
stopWatch.Start();
for (int i=0; i < FUNC_RUNTIME; i++) {
IDictionary<string, object> ret = json.Deserialize( bytes );
}
stopWatch.Stop();
Debug.Log("JSON Deserialize: n=" + FUNC_RUNTIME
+ "\r\n Time: " + (stopWatch.ElapsedMilliseconds / 1000f).ToString()
+ "\r\n Length: " + bytes.Length
+ "\r\n Rate: " + persec( FUNC_RUNTIME, stopWatch.ElapsedMilliseconds) + "/s");
Assert.True(true);
DoneTesting();
yield break;
}
[UnitTest]
public IEnumerator MsgpackDeserializeBenchmark()
{
Msgpack msgpack = gameObject.AddComponent("Msgpack") as Msgpack;
Dictionary<string, object> data = GetBigData(DATA_LIST_COUNT);
byte[] bytes = msgpack.Serialize( data );;
Stopwatch stopWatch = new Stopwatch();
stopWatch.Start();
for (int i=0; i < FUNC_RUNTIME; i++) {
IDictionary<string, object> ret = msgpack.Deserialize( bytes );
}
stopWatch.Stop();
Debug.Log("Msgpack Deserialize: n=" + FUNC_RUNTIME
+ "\r\n Time: " + (stopWatch.ElapsedMilliseconds / 1000f).ToString()
+ "\r\n Length: " + bytes.Length
+ "\r\n Rate: " + persec( FUNC_RUNTIME, stopWatch.ElapsedMilliseconds) + "/s");
Assert.True(true);
DoneTesting();
yield break;
}
[UnitTest]
public IEnumerator JsonBenchmark()
{
JSON json = gameObject.AddComponent("JSON") as JSON;
json.Type = Types.BODY;
bool success = true;
Debug.Log("Start JSON Bench");
Stopwatch stopWatch = new Stopwatch();
stopWatch.Start();
for (int i=0; i < HTTP_RUNTIME; i++) {
yield return StartCoroutine(json.Request( GetBigData( i % 100 ) ));
if ( json.IsError ) {
Assert.True(false, "Fail");
success = false;
break;
}
}
stopWatch.Stop();
Debug.Log("JSON HTTP: n=" + HTTP_RUNTIME
+ "\r\n Time: " + (stopWatch.ElapsedMilliseconds / 1000f).ToString()
+ "\r\n Rate: " + persec( HTTP_RUNTIME, stopWatch.ElapsedMilliseconds) + "/s");
Assert.True(success);
DoneTesting();
}
[UnitTest]
public IEnumerator MsgpackBenchmark()
{
Msgpack msgpack = gameObject.AddComponent("Msgpack") as Msgpack;
msgpack.Type = Types.BODY;
bool success = true;
Debug.Log("Start Msgpack Bench");
Stopwatch stopWatch = new Stopwatch();
stopWatch.Start();
for (int i=0; i < HTTP_RUNTIME; i++) {
yield return StartCoroutine(msgpack.Request( GetBigData(i % 100) ));
if ( msgpack.IsError ) {
Assert.True(false, "Fail");
success = false;
break;
}
}
stopWatch.Stop();
Debug.Log("Msgpack HTTP: n=" + HTTP_RUNTIME
+ "\r\n Time: " + (stopWatch.ElapsedMilliseconds / 1000f).ToString()
+ "\r\n Rate: " + persec( HTTP_RUNTIME, stopWatch.ElapsedMilliseconds) + "/s");
Assert.True(success);
DoneTesting();
}
[UnitTest]
public IEnumerator BigData() {
Stopwatch stopWatch = new Stopwatch();
stopWatch.Start();
for (int i=0; i < 100; i++) {
IDictionary<string, object> ret = GetBigData(i);
}
stopWatch.Stop();
Debug.Log("CreateData: n=" +100
+ "\r\n Time: " + (stopWatch.ElapsedMilliseconds / 1000f).ToString()
+ "\r\n Rate: " + persec( FUNC_RUNTIME, stopWatch.ElapsedMilliseconds) + "/s");
DoneTesting();
yield break;
}
public Dictionary<string, object> GetParam (string name, int id) {
Dictionary<string, object> param = new Dictionary<string, object>();
param.Add("id", id);
param.Add("name", name);
param.Add("int", 14142);
param.Add("float", 3.141565f);
param.Add("bool", true);
param.Add("nil", null);
param.Add("str", "test");
List<int> arr = new List<int> {1, 2, 8, 9};
param.Add("arr", arr);
Dictionary<string, object> map = new Dictionary<string, object>();
map.Add("foo", "bar");
map.Add("buzz", 551);
map.Add("piyo", 2.14);
param.Add("map", map);
return param;
}
public Dictionary<string, object> GetBigData ( int count ) {
Dictionary<string, object> param = GetParam("test", 1);
List<int> intArr = new List<int> ();
for (int i = 0; i < count; i++) intArr.Add(i);
param.Add("intarr", intArr);
List<string> strArr = new List<string> ();
for (int i = 0; i < count; i++) strArr.Add("text_" + i.ToString());
param.Add("strarr", strArr);
List<bool> boolArr = new List<bool> ();
for (int i = 0; i < count; i++) boolArr.Add(i % 2 == 1 ? true : false);
param.Add("boolarr", boolArr);
List<Dictionary<string, object>> mapArr = new List<Dictionary<string, object>> ();
for (int i = 0; i < count; i++) mapArr.Add(GetParam( "map"+i, i + 10000));
param.Add("maparr", mapArr);
return param;
}
}
}
/*
Json Serialize: n=3000
Time: 9.993
Length: 17211
Rate: 300.2101/s
Msgpack Serialize: n=3000
Time: 7.701
Length: 11118
Rate: 389.5598/s
JSON Deserialize: n=3000
Time: 19.12
Length: 17211
Rate: 156.9038/s
Msgpack Deserialize: n=3000
Time: 7.83
Length: 11118
Rate: 383.1418/s
JSON HTTP: n=200
Time: 306.695
Rate: 0.6521137/s
Msgpack HTTP: n=200
Time: 287.642
Rate: 0.6953087/s
CreateData: n=100
Time: 0.044
Rate: 68181.82/s
*/