Skip to content
This repository has been archived by the owner on Sep 25, 2020. It is now read-only.

tcurl benchmark mode: sending a request many times #71

Merged
merged 5 commits into from
Oct 19, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 71 additions & 0 deletions benchmark-logger.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
#!/usr/bin/env node

// Copyright (c) 2015 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
'use strict';

module.exports = BenchmarkLogger;

function BenchmarkLogger() {
var self = this;
self.errors = [];
self.responses = [];
}

BenchmarkLogger.prototype.log = function log(message) {
var self = this;
if (self.logger) {
self.logger.log(message);
}
};

BenchmarkLogger.prototype.error = function error(err) {
var self = this;
if (self.logger) {
self.logger.error(err);
}
};

BenchmarkLogger.prototype.response = function response(res, opts) {
var self = this;
if (self.logger) {
self.logger.response(res, opts);
}
};

BenchmarkLogger.prototype.handleReponse = function handleReponse(err, res) {
var self = this;
if (err) {
self.errors.push(err);
}

if (res) {
self.responses.push(res);
}

if (self.count === self.errors.length + self.responses.length) {
self.collect(self.errors, self.responses);
self.errors = [];
self.responses = [];
}
};

BenchmarkLogger.prototype.exit = function exit() {
};
146 changes: 146 additions & 0 deletions benchmark.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
// Copyright (c) 2015 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

'use strict';

var process = require('process');
var setTimeout = require('timers').setTimeout;
var BenchmarkLogger = require('./benchmark-logger');

function Benchmark(options) {
if (!(this instanceof Benchmark)) {
return new Benchmark(options);
}

var self = this;

self.tcurl = options.tcurl;
self.logger = options.logger;
self.delegate = new BenchmarkLogger();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It does not make sense to have both a logger and a benchmark logger. I suggest consolidating these into one delegate and instantiate the benchmark delegate instead of the logging delegate.

if (self.logger.always) {
self.delegate.logger = self.logger;
}

self.cmdOptions = options.cmdOptions;
self.requests = options.cmdOptions.requests || 0;
self.requestsLimit = self.requests !== 0;
self.time = options.cmdOptions.time;

if (self.time === undefined) {
self.time = 0;
if (self.requests === 0) {
self.time = 30 * 1000;
}
}

self.timeLimit = self.time !== 0;
self.delay = options.cmdOptions.delay || 100;
self.rate = options.cmdOptions.rate;
if (typeof self.rate !== 'number' || self.rate <= 0) {
self.logger.error('tcurl requires --rate to be a positive integer');
self.logger.exit();
}

self.loop = 0;
}

Benchmark.prototype.run = function run(callback) {
var self = this;
self.tcurl.prepare(self.cmdOptions, self.delegate, function send() {
self.stopTime = Date.now() + self.time;
self.sendLoop(onComplete);
});

function onComplete() {
process.nextTick(callback);
}
};

Benchmark.prototype.sendLoop = function sendLoop(callback) {
var self = this;

if (self.timeLimit && self.stopTime <= Date.now()) {
return callback();
}

self.send(callback);
};

Benchmark.prototype.logResults = function logResults(errors, responses) {
var self = this;

var collection = {};
var i;
var type;
var count;
collection.succeeded = responses.length;
for (i = 0; i < errors.length; i++) {
type = errors[i].type || 'error.unkown';
count = collection[type] || 0;
count++;
collection[type] = count;
}

self.logger.log('Loop ' + self.loop + ': ');
self.logger.log(' succeeded: ' + (collection.succeeded || '0'));
var keys = Object.keys(collection);
for (i = 0; i < keys.length; i++) {
if (keys[i] === 'succeeded') {
continue;
}

self.logger.log(' ' + keys[i] + ': ' + collection[keys[i]]);
}
};

Benchmark.prototype.collect = function collect(errors, responses, callback) {
var self = this;

self.loop++;

self.logResults(errors, responses);

// schedule next round
self.requests -= self.rate;
if (self.requestsLimit && self.rate > self.requests) {
self.rate = self.requests;
}

if (!self.requestsLimit || self.requests > 0) {
setTimeout(self.sendLoop.bind(self, callback), self.delay || 1);
} else {
callback();
}
};

Benchmark.prototype.send = function send(callback) {
var self = this;
self.delegate.count = self.rate;
self.delegate.collect = function collect(errors, responses) {
self.collect(errors, responses, callback);
};

for (var i = 0; i < self.rate; i++) {
var req = self.tcurl.createRequest(self.cmdOptions);
self.tcurl.send(self.cmdOptions, req, self.delegate);
}
};

module.exports = Benchmark;
99 changes: 75 additions & 24 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ var assert = require('assert');
var safeJsonParse = require('safe-json-parse/tuple');

var Logger = require('./logger');
var Benchmark = require('./benchmark');
var HealthLogger = require('./health-logger');
var TCurlAsHttp = require('./as-http');

Expand Down Expand Up @@ -110,6 +111,18 @@ function main(argv, delegate) {
delegate = delegate || new Logger();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here, instantiate the benchmark delegate instead of the logger if opts.rate.

}

if (opts.rate) {
var benchmark = new Benchmark({
tcurl: tcurl,
cmdOptions: opts,
logger: delegate
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here: pass in the benchmark delegate instead of the logger delegate.

});
return benchmark.run(function done() {
tcurl.client.close();
delegate.exit();
});
}

return tcurl.request(opts, delegate);
}

Expand All @@ -133,6 +146,7 @@ function help() {
console.log(' [-2 | --arg2 | --head] [-3 | --arg3 | --body]');
console.log(' [--shardKey] [--no-strict] [--timeout]');
console.log(' [--http] [--raw] [--health]');
console.log(' [--rate] [--requests] [--time] [--delay]');
}

function printFullHelp() {
Expand Down Expand Up @@ -201,11 +215,22 @@ function parseArgs(argv) {
http: argv.http,
raw: argv.raw,
timeout: argv.timeout,
health: health
health: health,
time: argv.time,
requests: argv.requests,
delay: argv.delay,
rate: argv.rate
};
}

function TCurl(opts) {
if (!(this instanceof TCurl)) {
return new TCurl(opts);
}

var self = this;
self.subChannel = null;
self.client = null;
}

TCurl.prototype.parseJsonArgs = function parseJsonArgs(opts, delegate) {
Expand Down Expand Up @@ -272,7 +297,7 @@ TCurl.prototype.readThriftDir = function readThriftDir(opts, delegate) {
return sources[opts.service];
};

TCurl.prototype.request = function tcurlRequest(opts, delegate) {
TCurl.prototype.prepare = function prepare(opts, delegate, callback) {
var self = this;

// May report errors for arg2, arg3, or both
Expand All @@ -281,11 +306,11 @@ TCurl.prototype.request = function tcurlRequest(opts, delegate) {
return delegate.exit();
}

var client = TChannel({
self.client = TChannel({
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add client as a nulled default property in the constructor.

logger: DebugLogtron('tcurl')
});

var subChan = client.makeSubChannel({
self.subChannel = self.client.makeSubChannel({
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add subChannel as a nulled default property in the constructor.

serviceName: opts.service,
peers: opts.peers,
requestDefaults: {
Expand All @@ -296,39 +321,61 @@ TCurl.prototype.request = function tcurlRequest(opts, delegate) {
}
});

var peer = subChan.peers.choosePeer();
var peer = self.subChannel.peers.choosePeer();
// TODO: the host option should be called peer, hostPort, or address
client.waitForIdentified({host: peer.hostPort}, onIdentified);
self.client.waitForIdentified({host: peer.hostPort}, onIdentified);

function onIdentified(err) {
if (err) {
delegate.error(err);
return delegate.exit();
}

var headers = opts.shardKey ? {sk: opts.shardKey} : {};
callback();
}
};

var request = subChan.request({
timeout: opts.timeout || 100,
hasNoParent: true,
serviceName: opts.service,
headers: headers
});
TCurl.prototype.createRequest = function createRequest(opts) {
var self = this;
var headers = opts.shardKey ? {sk: opts.shardKey} : {};
return self.subChannel.request({
timeout: opts.timeout || 100,
hasNoParent: true,
serviceName: opts.service,
headers: headers
});
};

if (opts.argScheme === 'thrift') {
self.asThrift(opts, request, delegate, done);
} else if (opts.argScheme === 'http') {
self.asHTTP(opts, client, subChan, delegate, done);
} else if (opts.argScheme === 'json') {
self.asJSON(opts, request, delegate, done);
// TODO fix argument order for each of these
} else {
self.asRaw(opts, request, delegate, done);
}
TCurl.prototype.send = function send(opts, request, delegate) {
var self = this;

if (opts.argScheme === 'thrift') {
self.asThrift(opts, request, delegate, done);
} else if (opts.argScheme === 'http') {
self.asHTTP(opts, self.client, self.subChannel, delegate, done);
} else if (opts.argScheme === 'json') {
self.asJSON(opts, request, delegate, done);
// TODO fix argument order for each of these
} else {
self.asRaw(opts, request, delegate, done);
}

function done() {
client.quit();
if (!opts.rate) {
self.client.close();
}
}
};

TCurl.prototype.request = function tcurlRequest(opts, delegate) {
var self = this;
self.prepare(opts, delegate, onReady);

function onReady() {
self.send(opts,
self.createRequest(opts),
delegate
);
}
};

Expand Down Expand Up @@ -420,6 +467,10 @@ TCurl.prototype.asJSON = function asJSON(opts, request, delegate, done) {
};

TCurl.prototype.onResponse = function onResponse(err, res, arg2, arg3, opts, delegate) {
if (typeof delegate.handleReponse === 'function') {
delegate.handleReponse(err, res, arg2, arg3, opts);
}

if (err) {
delegate.error(err);
return delegate.exit();
Expand Down
Loading