Skip to content

Commit

Permalink
perf: ⚡️ add Benchmark.js
Browse files Browse the repository at this point in the history
  • Loading branch information
streamich committed Oct 4, 2020
1 parent 42d00b3 commit 0a6e221
Show file tree
Hide file tree
Showing 9 changed files with 155 additions and 1 deletion.
31 changes: 31 additions & 0 deletions benchmarks/connect.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
const Benchmark = require('benchmark');
const {connectShort, connectWithProperties} = require('./packets');
const {MqttDecoder} = require('../es6/MqttDecoder');
const mqtt = require('mqtt-packet');

const decoder = new MqttDecoder();
const parser = mqtt.parser();

const suite = new Benchmark.Suite;
suite
.add('mqtt-codec (CONNECT short)', function() {
decoder.push(Buffer.from(connectShort));
packet = decoder.parse();
})
.add('mqtt-codec (CONNECT long with properties)', function() {
decoder.push(Buffer.from(connectWithProperties));
packet = decoder.parse();
})
.add('mqtt-packet (CONNECT short)', function() {
parser.parse(Buffer.from(connectShort));
})
.add('mqtt-packet (CONNECT long with properties)', function() {
parser.parse(Buffer.from(connectWithProperties));
})
.on('cycle', function(event) {
console.log(String(event.target));
})
.on('complete', function() {
console.log('Fastest is ' + this.filter('fastest').map('name'));
})
.run();
File renamed without changes.
File renamed without changes.
72 changes: 72 additions & 0 deletions benchmarks/packets.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
const packets = {
connectWithProperties: [
16, 125, // Header
0, 4, // Protocol ID length
77, 81, 84, 84, // Protocol ID
5, // Protocol version
54, // Connect flags
0, 30, // Keepalive
47, // properties length
17, 0, 0, 4, 210, // sessionExpiryInterval
33, 1, 176, // receiveMaximum
39, 0, 0, 0, 100, // maximumPacketSize
34, 1, 200, // topicAliasMaximum
25, 1, // requestResponseInformation
23, 1, // requestProblemInformation,
38, 0, 4, 116, 101, 115, 116, 0, 4, 116, 101, 115, 116, // userProperties,
21, 0, 4, 116, 101, 115, 116, // authenticationMethod
22, 0, 4, 1, 2, 3, 4, // authenticationData
0, 4, // Client ID length
116, 101, 115, 116, // Client ID
47, // will properties
24, 0, 0, 4, 210, // will delay interval
1, 0, // payload format indicator
2, 0, 0, 16, 225, // message expiry interval
3, 0, 4, 116, 101, 115, 116, // content type
8, 0, 5, 116, 111, 112, 105, 99, // response topic
9, 0, 4, 1, 2, 3, 4, // corelation data
38, 0, 4, 116, 101, 115, 116, 0, 4, 116, 101, 115, 116, // user properties
0, 5, // Will topic length
116, 111, 112, 105, 99, // Will topic
0, 4, // Will payload length
4, 3, 2, 1// Will payload
],

connectShort: [
16, 16,
0, 4, 77, 81, 84, 84, // Protocol name
4, // Version
2, // Flags
0, 0, // Keep-alive
0, 4, 116, 101, 115, 116 // Client ID "test"
],

connackShort: [
32, 2, 0, 0
],

connackWithProperties: [
32, 100, 0, 0,
97, // properties length
17, 0, 0, 4, 210, // sessionExpiryInterval
33, 1, 176, // receiveMaximum
36, 2, // Maximum qos
37, 1, // retainAvailable
39, 0, 0, 0, 100, // maximumPacketSize
18, 0, 4, 116, 101, 115, 116, // assignedClientIdentifier
34, 1, 200, // topicAliasMaximum
31, 0, 4, 116, 101, 115, 116, // reasonString
38, 0, 4, 116, 101, 115, 116, 0, 4, 116, 101, 115, 116,
38, 0, 4, 116, 101, 115, 116, 0, 4, 116, 101, 115, 116, // userProperties
40, 1, // wildcardSubscriptionAvailable
41, 1, // subscriptionIdentifiersAvailable
42, 0, // sharedSubscriptionAvailable
19, 4, 210, // serverKeepAlive
26, 0, 4, 116, 101, 115, 116, // responseInformation
28, 0, 4, 116, 101, 115, 116, // serverReference
21, 0, 4, 116, 101, 115, 116, // authenticationMethod
22, 0, 4, 1, 2, 3, 4 // authenticationData
],
};

exports.packets = packets;
28 changes: 28 additions & 0 deletions benchmarks/single-packet.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
const Benchmark = require('benchmark');
const {packets} = require('./packets');
const {MqttDecoder} = require('../es6/MqttDecoder');
const mqtt = require('mqtt-packet');

const suite = new Benchmark.Suite;

for (const name of Object.keys(packets)) {
suite
.add(`mqtt-codec (${name})`, function() {
const decoder = new MqttDecoder();
decoder.push(Buffer.from(packets[name]));
decoder.parse();
})
.add(`mqtt-packet (${name})`, function() {
const parser = mqtt.parser();
parser.parse(Buffer.from(packets[name]));
});
}

suite
.on('cycle', function(event) {
console.log(String(event.target));
})
.on('complete', function() {
console.log('Fastest is ' + this.filter('fastest').map('name'));
})
.run();
10 changes: 10 additions & 0 deletions benchmarks/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
const Benchmark = require('benchmark');
const {connectShort, connectWithProperties} = require('./packets');
const {MqttDecoder} = require('../es6/MqttDecoder');
const {PacketConnect} = require('../es6/packets/connect');
const mqtt = require('mqtt-packet');
const parser = mqtt.parser();
let packet;
parser.on('packet', p => packet = p);
parser.parse(Buffer.from(connectShort));
console.log(packet);
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
"@semantic-release/npm": "^7.0.6",
"@types/bl": "^2.1.0",
"@types/jest": "^26.0.14",
"benchmark": "^2.1.4",
"husky": "^4.3.0",
"jest": "^26.4.2",
"mqtt-packet": "^6.6.0",
Expand Down
1 change: 0 additions & 1 deletion src/packet.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import BufferList from 'bl';
import {PACKET_TYPE} from './enums';
import {QoS} from './types';

Expand Down
13 changes: 13 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1228,6 +1228,14 @@ before-after-hook@^2.1.0:
resolved "https://registry.yarnpkg.com/before-after-hook/-/before-after-hook-2.1.0.tgz#b6c03487f44e24200dd30ca5e6a1979c5d2fb635"
integrity sha512-IWIbu7pMqyw3EAJHzzHbWa85b6oud/yfKYg5rqB5hNE8CeMi3nX+2C2sj0HswfblST86hpVEOAb9x34NZd6P7A==

benchmark@^2.1.4:
version "2.1.4"
resolved "https://registry.yarnpkg.com/benchmark/-/benchmark-2.1.4.tgz#09f3de31c916425d498cc2ee565a0ebf3c2a5629"
integrity sha1-CfPeMckWQl1JjMLuVloOvzwqVik=
dependencies:
lodash "^4.17.4"
platform "^1.3.3"

bin-links@^1.1.2, bin-links@^1.1.8:
version "1.1.8"
resolved "https://registry.yarnpkg.com/bin-links/-/bin-links-1.1.8.tgz#bd39aadab5dc4bdac222a07df5baf1af745b2228"
Expand Down Expand Up @@ -5439,6 +5447,11 @@ pkg-dir@^4.2.0:
dependencies:
find-up "^4.0.0"

platform@^1.3.3:
version "1.3.6"
resolved "https://registry.yarnpkg.com/platform/-/platform-1.3.6.tgz#48b4ce983164b209c2d45a107adb31f473a6e7a7"
integrity sha512-fnWVljUchTro6RiCFvCXBbNhJc2NijN7oIQxbwsyL0buWJPG85v81ehlHI9fXrJsMNgTofEoWIQeClKpgxFLrg==

please-upgrade-node@^3.2.0:
version "3.2.0"
resolved "https://registry.yarnpkg.com/please-upgrade-node/-/please-upgrade-node-3.2.0.tgz#aeddd3f994c933e4ad98b99d9a556efa0e2fe942"
Expand Down

0 comments on commit 0a6e221

Please sign in to comment.