Skip to content

Commit

Permalink
Merge a2d353d into 6dd5991
Browse files Browse the repository at this point in the history
  • Loading branch information
Braydon Fuller committed Jun 30, 2015
2 parents 6dd5991 + a2d353d commit 057effa
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 11 deletions.
59 changes: 59 additions & 0 deletions benchmark/serialization.js
Expand Up @@ -14,6 +14,65 @@ console.log('Benchmarking Block/Transaction Serialization');
console.log('---------------------------------------');

async.series([
function(next) {

var buffers = [];
var hashBuffers = [];
console.log('Generating Random Test Data...');
for (var i = 0; i < 100; i++) {

// uint64le
var br = new bitcore.encoding.BufferWriter();
var num = Math.round(Math.random() * 10000000000000);
br.writeUInt64LEBN(new bitcore.crypto.BN(num));
buffers.push(br.toBuffer());

// hashes
var data = bitcore.crypto.Hash.sha256sha256(new Buffer(32));
hashBuffers.push(data);
}

var c = 0;
var bn;

function readUInt64LEBN() {
if (c >= buffers.length) {
c = 0;
}
var buf = buffers[c];
var br = new bitcore.encoding.BufferReader(buf);
bn = br.readUInt64LEBN();
c++;
}

var reversed;

function readReverse() {
if (c >= hashBuffers.length) {
c = 0;
}
var buf = hashBuffers[c];
var br = new bitcore.encoding.BufferReader(buf);
reversed = br.readReverse();
c++;
}

console.log('Starting benchmark...');

var suite = new benchmark.Suite();
suite.add('bufferReader.readUInt64LEBN()', readUInt64LEBN, {maxTime: maxTime});
suite.add('bufferReader.readReverse()', readReverse, {maxTime: maxTime});
suite
.on('cycle', function(event) {
console.log(String(event.target));
})
.on('complete', function() {
console.log('Done');
console.log('----------------------------------------------------------------------');
next();
})
.run();
},
function(next) {

var block1;
Expand Down
7 changes: 2 additions & 5 deletions lib/encoding/bufferreader.js
Expand Up @@ -91,11 +91,8 @@ BufferReader.prototype.readUInt64BEBN = function() {
};

BufferReader.prototype.readUInt64LEBN = function() {
var buf = this.buf.slice(this.pos, this.pos + 8);
var reversebuf = BufferReader({
buf: buf
}).readReverse();
var bn = BN.fromBuffer(reversebuf);
var data = Array.prototype.slice.call(this.buf, this.pos, this.pos + 8);
var bn = new BN(data, 10, 'le');
this.pos = this.pos + 8;
return bn;
};
Expand Down
1 change: 0 additions & 1 deletion lib/util/buffer.js
Expand Up @@ -152,7 +152,6 @@ module.exports = {
* @return {Buffer}
*/
reverse: function reverse(param) {
$.checkArgumentType(param, 'Buffer', 'param');
var ret = new buffer.Buffer(param.length);
for (var i = 0; i < param.length; i++) {
ret[i] = param[param.length - i - 1];
Expand Down
5 changes: 0 additions & 5 deletions test/util/buffer.js
Expand Up @@ -152,10 +152,5 @@ describe('buffer utils', function() {
original[1].should.equal(reversed[1]);
original[2].should.equal(reversed[0]);
});
it('checks the argument type', function() {
expect(function() {
BufferUtil.reverse('invalid');
}).to.throw(errors.InvalidArgumentType);
});
});
});

0 comments on commit 057effa

Please sign in to comment.