Skip to content

Commit d05b122

Browse files
committed
Added benchmark
1 parent fff3fc0 commit d05b122

File tree

3 files changed

+195
-0
lines changed

3 files changed

+195
-0
lines changed

benchmark/bencode.old.js

Lines changed: 163 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,163 @@
1+
/*
2+
* ----------------------------------------------------------------------------
3+
* node-bencode v0.1.0
4+
* <[email protected]> wrote this file. As long as you retain this notice
5+
* you can do whatever you want with this stuff. If we meet some day, and you
6+
* think this stuff is worth it, you can buy me a beer in return. Mark Schmale
7+
* ----------------------------------------------------------------------------
8+
*/
9+
var sys = require('sys');
10+
11+
/**
12+
* decodes a bencoded string
13+
*/
14+
exports.decode = function decode(str) {
15+
var arr = str.split('');
16+
17+
function integer(start) {
18+
var c = '', n = '', i = 0, mul = 1;
19+
var len = arr.length;
20+
if(arr[start] === '-') {
21+
mul = -1;
22+
start = start+1;
23+
}
24+
for(i=start;i<len;i++) {
25+
c = arr[i];
26+
if(c == parseInt(c)) {
27+
n = n + c;
28+
} else {
29+
break;
30+
}
31+
}
32+
return {next: i+1, ret: mul*parseInt(n)};
33+
}
34+
35+
function text(start) {
36+
var nfo = integer(start),
37+
start = nfo.next,
38+
len = nfo.ret
39+
i = 0
40+
full = '';
41+
42+
for(i=start;i<len+start;i++) {
43+
full = full + arr[i]
44+
}
45+
return {next: i, ret: full};
46+
}
47+
48+
function list(start) {
49+
var len = arr.length,
50+
i = start+1,
51+
list = [],
52+
tmp = {},
53+
lcnt = 0;
54+
while(i<len && arr[i] !== 'e') {
55+
tmp = next(i);
56+
i = tmp.next;
57+
list[lcnt] = tmp.ret;
58+
lcnt++;
59+
}
60+
return {next: i+1, ret: list};
61+
}
62+
63+
function dictionary(start) {
64+
var len = arr.length,
65+
i = start+1,
66+
list = {},
67+
tmp = {},
68+
isKey = true,
69+
key = '';
70+
while(i<len && arr[i] !== 'e') {
71+
tmp = next(i);
72+
i = tmp.next;
73+
if(isKey === true) {
74+
key = tmp.ret;
75+
} else {
76+
list[key] = tmp.ret;
77+
}
78+
isKey = !isKey;
79+
}
80+
return {next: i+1, ret: list};
81+
}
82+
83+
84+
function next(start) {
85+
var data = 0;
86+
start = start || 0;
87+
switch(arr[start]) {
88+
case 'i': // integer
89+
data = integer(start+1);
90+
break;
91+
case 'l': // liste
92+
data = list(start);
93+
break;
94+
case 'd': // dict
95+
data = dictionary(start);
96+
break;
97+
default: // string
98+
data = text(start);
99+
}
100+
return data;
101+
}
102+
103+
return next(0).ret;
104+
}
105+
106+
exports.encode = function encode(data) {
107+
/* sys.puts(sys.inspect(typeof(data)));
108+
sys.puts(sys.inspect(is_array(data)));*/
109+
110+
function encode_string(data) {
111+
return data.length + ":" + data;
112+
}
113+
114+
function encode_integer(data) {
115+
return "i" + data + "e";
116+
}
117+
118+
function encode_list(data) {
119+
var max = data.length;
120+
var i = 0;
121+
var str = "l";
122+
for(i=0;i<max;i++) {
123+
str = str + encode(data[i]);
124+
}
125+
str = str + "e";
126+
return str;
127+
}
128+
129+
function encode_dict(data) {
130+
var str = "d";
131+
for(var key in data) {
132+
str = str + encode_string(key) + encode(data[key]);
133+
}
134+
str = str + "e";
135+
return str;
136+
}
137+
138+
/** helper **/
139+
function is_array(obj) {
140+
return obj.constructor == Array;
141+
}
142+
143+
var str = "";
144+
145+
switch(typeof(data)) {
146+
case 'object':
147+
if(is_array(data) === true) {
148+
str = encode_list(data);
149+
} else {
150+
str = encode_dict(data);
151+
}
152+
break;
153+
154+
case 'number':
155+
str = encode_integer(data);
156+
break;
157+
158+
case 'string':
159+
str = encode_string(data);
160+
break;
161+
}
162+
return str;
163+
}

benchmark/index.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
2+
var bencode = require( '../' )
3+
var old = require( './bencode.old' )
4+
var Benchmark = require( 'benchmark' )
5+
var fs = require( 'fs' )
6+
7+
var suite = new Benchmark.Suite
8+
9+
var buffer = fs.readFileSync( __dirname + '/test.torrent' )
10+
var data = bencode.decode( buffer, true )
11+
var string = bencode.encode( data )
12+
13+
suite
14+
.add( 'new#encode( Object )', function () {
15+
bencode.encode( data )
16+
})
17+
.add( 'old#encode( Object )', function () {
18+
old.encode( data )
19+
})
20+
.add( 'new#decode( Buffer )', function () {
21+
bencode.decode( buffer )
22+
})
23+
.add( 'new#decode( Buffer, true )', function () {
24+
bencode.decode( buffer, true )
25+
})
26+
.add( 'old#decode( String )', function () {
27+
old.decode( string )
28+
})
29+
.on( 'cycle', function ( event, bench ) {
30+
console.log( bench.toString(), bench.error || '' )
31+
})
32+
.run()

benchmark/test.torrent

27.1 KB
Binary file not shown.

0 commit comments

Comments
 (0)