Skip to content

Commit d415441

Browse files
committed
add files
1 parent 73ea935 commit d415441

17 files changed

+28811
-0
lines changed

msgpack.js

Lines changed: 596 additions & 0 deletions
Large diffs are not rendered by default.

test/10000.js

Lines changed: 17426 additions & 0 deletions
Large diffs are not rendered by default.

test/1byte.bin

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+

test/1byte.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
$fileName = "1byte.bin";
3+
4+
header('Content-Type: application/octet-stream');
5+
//header('Content-Disposition: attachment; filename=dl.zip');
6+
header('Content-Length: ' . filesize($fileName));
7+
readfile($fileName);
8+
?>

test/1mb.bin

996 KB
Binary file not shown.

test/1mb.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
$fileName = "1mb.bin";
3+
header('Content-Type: application/octet-stream');
4+
//header('Content-Disposition: attachment; filename=dl.zip');
5+
header('Content-Length: ' . filesize($fileName));
6+
readfile($fileName);
7+
?>

test/48byte.bin

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
��a��b�?�|�hr�˿�\(��---DUMMY-CHUNK-DATAS---

test/48byte.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
$fileName = "48byte.bin";
3+
4+
header('Content-Type: application/octet-stream');
5+
//header('Content-Disposition: attachment; filename=dl.zip');
6+
header('Content-Length: ' . filesize($fileName));
7+
readfile($fileName);
8+
?>

test/ajax.htm

Lines changed: 179 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,179 @@
1+
<!DOCTYPE html><html lang="ja">
2+
<head>
3+
<meta charset="utf-8" />
4+
<title>msgpack ajax demo</title>
5+
<style>
6+
/*
7+
body {
8+
overflow: hidden;
9+
}
10+
*/
11+
.box {
12+
position: absolute; top: 50%; left: 50%; background: lightpink;
13+
width: 100px; height: 100px;
14+
-webkit-border-radius: 60px;
15+
-moz-border-radius: 60px;
16+
border-radius: 60px;
17+
}
18+
</style>
19+
<script src="../msgpack.js"></script>
20+
<script src="uupaa.js"></script>
21+
<script>
22+
23+
// --- set WebWorker script url ---
24+
msgpack.worker = "../" + msgpack.worker;
25+
26+
var downloadURLs = ["1mb.php", "48byte.php", "1byte.php", "last.php"],
27+
uploadURL = "upload.php",
28+
lastData = null;
29+
30+
// msgpack.download + msgpack.unpack
31+
function download(url, useWorker) {
32+
33+
var perf = +new Date;
34+
uu.log("downloading...");
35+
36+
function callback(data, option, response) {
37+
if (response.ok) {
38+
uu.log("download elapse @ms", (+new Date) - perf);
39+
if (uu.id("show").checked) {
40+
uu.log("--- msgpack.unpack() ---");
41+
uu.log(data);
42+
}
43+
lastData = data;
44+
} else {
45+
uu.log("error status = @", response.status);
46+
}
47+
}
48+
49+
msgpack.download(downloadURLs[url] + "?" + (+new Date), { worker: useWorker }, callback);
50+
}
51+
52+
// msgpack.pack + msgpack.upload
53+
function upload(useWorker) {
54+
55+
var perf = +new Date;
56+
uu.log("uploading...");
57+
58+
function callback(data, option, response) {
59+
if (response.ok) {
60+
uu.log("upload elapse @ms", (+new Date) - perf);
61+
if (uu.id("show").checked) {
62+
uu.log("--- xhr.responseText ---");
63+
uu.log(data);
64+
}
65+
} else {
66+
uu.log("error status = @", response.status);
67+
}
68+
}
69+
var data = lastData ? lastData : "";
70+
71+
msgpack.upload(uploadURL, { data: data, worker: useWorker }, callback);
72+
}
73+
74+
// flier
75+
uu.ready("window", function() {
76+
var viewport = uu.viewport, // { innerWidth, innerHeight }
77+
iw = viewport.innerWidth,
78+
ih = viewport.innerHeight;
79+
80+
function rand() {
81+
var rv = { x: [iw / 2, "outback"],
82+
y: [ih / 2, "outback"], chain: 1 };
83+
84+
switch ((Math.random() * 10 % 4) | 0) {
85+
case 0: rv.x = Math.random() * (iw - 100 - 30) + 30; break;
86+
case 1: rv.x = [Math.random() * (iw - 100 - 30) + 30, "outback"]; break;
87+
case 2: rv.y = Math.random() * (ih - 100 - 30) + 30; break;
88+
case 3: rv.y = [Math.random() * (ih - 100 - 30) + 30, "outback"]; break;
89+
}
90+
return rv;
91+
}
92+
93+
function motion(node, duration) {
94+
uu.fx(node, duration, { x: [iw - 200, "outback"], y: 30, o: 0.5, chain: 1 });
95+
uu.fx(node, duration, { y: [ih - 200, "outback"], o: 1, chain: 1 });
96+
uu.fx(node, duration, { x: [30, "outback"], o: 0.5, chain: 1 });
97+
uu.fx(node, duration, rand());
98+
uu.fx(node, duration, rand());
99+
uu.fx(node, duration, { x: [iw / 2, "outback"],
100+
y: [ih / 2, "outback"],
101+
reverse: 1,
102+
after: function(node) { motion(node, duration); }});
103+
}
104+
motion(uu.klass("box")[0], 1000);
105+
});
106+
107+
</script>
108+
</head><body>
109+
<h1>msgpack.download(), msgpack.upload()</h1>
110+
<div class="box"></div>
111+
<div>
112+
Last downloaded data length: <span id="lastlength">0</span> bytes
113+
</div>
114+
<div>
115+
<input id="show" type="checkbox" value="1" />add decode log
116+
<input type="button" value="clear log" onclick="uu.log.clear()" />
117+
</div>
118+
119+
120+
<hr />
121+
<table>
122+
<caption>Test1 (without WebWorkers)</caption>
123+
<tr>
124+
<td>Choose Download data</td>
125+
<td></td>
126+
<td>Upload Last data</td>
127+
<td></td>
128+
<td>Download Last data</td>
129+
</tr>
130+
<tr>
131+
<td>
132+
<input type="button" value="1MB data x 3" onclick="download(0, 0);download(0, 0);download(0, 0)" />
133+
<input type="button" value="1MB data" onclick="download(0)" />
134+
<input type="button" value="48byte data" onclick="download(1)" />
135+
<input type="button" value="1byte data" onclick="download(2)" />
136+
</td>
137+
<td> -&gt; </td>
138+
<td>
139+
<input type="button" value="pack + upload" onclick="upload(0)" />
140+
</td>
141+
<td> -&gt; </td>
142+
<td>
143+
<input type="button" value="download + unpack" onclick="download(3)" />
144+
</td>
145+
</tr>
146+
</table>
147+
148+
149+
<hr />
150+
<table>
151+
<caption>Test2 (with WebWorkers)</caption>
152+
<tr>
153+
<td>Choose Download data</td>
154+
<td></td>
155+
<td>Upload Last data</td>
156+
<td></td>
157+
<td>Download Last data</td>
158+
</tr>
159+
<tr>
160+
<td>
161+
<input type="button" value="1MB data x 3" onclick="download(0, 1);download(0, 1);download(0, 1)" />
162+
<input type="button" value="1MB data" onclick="download(0, 1)" />
163+
<input type="button" value="48byte data" onclick="download(1, 1)" />
164+
<input type="button" value="1byte data" onclick="download(2, 1)" />
165+
</td>
166+
<td> -&gt; </td>
167+
<td>
168+
<input type="button" value="pack + upload" onclick="upload(1)" />
169+
</td>
170+
<td> -&gt; </td>
171+
<td>
172+
<input type="button" value="download + unpack" onclick="download(3, 1)" />
173+
</td>
174+
</tr>
175+
</table>
176+
177+
178+
179+
</body></html>

test/bench.htm

Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
<!DOCTYPE html><html lang="ja"><head>
2+
<meta charset="utf-8" />
3+
<title>benchmark msgpack vs json</title>
4+
<script src="../msgpack.js"></script>
5+
<script src="uupaa.js"></script>
6+
<script src="10000.js"></script>
7+
<script>
8+
9+
10+
uu.ready(function() {
11+
if (uu.ie678) {
12+
alert(1);
13+
uu.id("Biggest").disabled = true;
14+
}
15+
});
16+
17+
var mix = null;
18+
19+
// msgpack.pack + unpack
20+
function test(json) {
21+
22+
var encoded, decoded;
23+
24+
function times() {
25+
var perf = +new Date;
26+
if (json) {
27+
if (window.JSON) {
28+
encoded = JSON.stringify(mix); // NativeJSON
29+
decoded = JSON.parse(encoded); // NativeJSON
30+
} else {
31+
encoded = uu.json(mix);
32+
decoded = uu.json.decode(encoded);
33+
}
34+
} else {
35+
encoded = msgpack.pack(mix);
36+
decoded = msgpack.unpack(encoded);
37+
}
38+
return (+new Date) - perf;
39+
}
40+
41+
// try 3 times
42+
// var rv = [times(), times(), times()];
43+
var rv = [times()];
44+
45+
// rv.sort().pop();
46+
// rv.shift();
47+
48+
if (json) {
49+
uu.log("json = @ms", rv[0]);
50+
} else {
51+
uu.log("msgpack = @ms", rv[0]);
52+
}
53+
54+
// verify
55+
if (1) {
56+
if (!uu.like(mix, decoded)) {
57+
uu.log("verify error !");
58+
}
59+
}
60+
}
61+
62+
// create random data
63+
function create(size) { // @param Number:
64+
function addItem() {
65+
switch ((Math.random() * 12) | 0) {
66+
case 0: return null; break;
67+
case 1: return false; break;
68+
case 2: return true; break;
69+
case 3: return i.toString(16) + String.fromCharCode(i & 0xffff, i & 0xffff, i & 0xffff, i & 0xffff); break;
70+
case 4: return i; break;
71+
case 5: return -i; break;
72+
case 6: return i / 123.456789; break;
73+
case 7: return -(i / 123.456789); break;
74+
case 8: return [addItem(), addItem(), addItem()];
75+
}
76+
var r = {};
77+
r[i] = addItem();
78+
return r;
79+
}
80+
var obj = {}, i = 0, v, type, json;
81+
82+
for (i = 0; i < size; ++i) {
83+
obj[i] = addItem();
84+
}
85+
// uu.id("json").value = uu.json(obj, true);
86+
uu.id("json").value = "(*snip*)";
87+
uu.log("size = " + size);
88+
89+
mix = obj;
90+
}
91+
92+
function solid(solidData) {
93+
mix = solidData;
94+
uu.id("json").value = "(*snip*)";
95+
uu.log("size = 10000");
96+
}
97+
</script>
98+
</head><body>
99+
<h1>Benchmark</h1>
100+
<div>
101+
<input type="button" value="clear log" onclick="uu.log.clear()" />
102+
</div>
103+
104+
105+
<hr />
106+
<table>
107+
<tr>
108+
<td>Prepare Data</td>
109+
<td></td>
110+
<td>Bench !</td>
111+
</tr>
112+
<tr>
113+
<td>
114+
<input type="button" disabled="disabled" value="Load 100000" onclick="solid(solid100000)" />
115+
<input type="button" value="Load 10000" onclick="solid(solid10000)" />
116+
<br />
117+
<input type="button" id="Biggest" value="Create 100000" onclick="create(100000)" />
118+
<input type="button" value="Create 10000" onclick="create(10000)" />
119+
<input type="button" value="Create 1000" onclick="create(1000)" />
120+
<input type="button" value="Create 10" onclick="create(10)" />
121+
</td>
122+
<td> -&gt; </td>
123+
<td>
124+
<input type="button" value="JSON.stringify + JSON.parse" onclick="test(1)" />
125+
</td>
126+
</tr>
127+
<tr>
128+
<td>
129+
</td>
130+
<td> -&gt; </td>
131+
<td>
132+
<input type="button" value="msgpack.pack + msgpack.unpack" onclick="test(0)" />
133+
</td>
134+
</tr>
135+
</table>
136+
<textarea id="json" cols="120" rows="5"></textarea>
137+
138+
139+
140+
141+
142+
</body></html>

0 commit comments

Comments
 (0)