From cea415740238fa7dbce7fd1cf84ad904754a61ff Mon Sep 17 00:00:00 2001 From: Julien Fontanet Date: Tue, 26 May 2015 16:35:05 +0200 Subject: [PATCH] Perf traces in the CLI. --- packages/xen-api/package.json | 1 + packages/xen-api/src/cli.js | 29 +++++++++++++++++++++++++++-- packages/xen-api/src/index.js | 18 ++++++++++++------ 3 files changed, 40 insertions(+), 8 deletions(-) diff --git a/packages/xen-api/package.json b/packages/xen-api/package.json index 935aeeb85..f0c7f567b 100644 --- a/packages/xen-api/package.json +++ b/packages/xen-api/package.json @@ -30,6 +30,7 @@ ], "dependencies": { "babel-runtime": "^5", + "blocked": "^1.1.0", "bluebird": "^2.9.21", "debug": "^2.1.3", "event-to-promise": "^0.3.2", diff --git a/packages/xen-api/src/cli.js b/packages/xen-api/src/cli.js index 4c308d776..c5ae44441 100755 --- a/packages/xen-api/src/cli.js +++ b/packages/xen-api/src/cli.js @@ -1,5 +1,6 @@ #!/usr/bin/env node +import blocked from 'blocked' import Bluebird, {coroutine} from 'bluebird' import eventToPromise from 'event-to-promise' import execPromise from 'exec-promise' @@ -9,17 +10,27 @@ import {start as createRepl} from 'repl' import {createClient} from './' +// =================================================================== + +Bluebird.longStackTraces() + +import createDebug from 'debug' +const debug = createDebug('xen-api:cli') + import sourceMapSupport from 'source-map-support' sourceMapSupport.install() +// =================================================================== + const usage = `Usage: xen-api ` const main = coroutine(function * (args) { const opts = minimist(args, { - boolean: ['help'], + boolean: ['help', 'verbose'], alias: { - help: 'h' + help: 'h', + verbose: 'v' } }) @@ -27,6 +38,13 @@ const main = coroutine(function * (args) { return usage } + if (opts.verbose) { + // Does not work perfectly. + // + // https://github.com/visionmedia/debug/pull/156 + createDebug.enable('xen-api,xen-api:*') + } + const [url, user] = opts._ if (!url || !user) { throw new Error('missing arguments') @@ -37,6 +55,13 @@ const main = coroutine(function * (args) { pw(resolve) }) + { + const debug = createDebug('xen-api:perf') + blocked(ms => { + debug('blocked for %sms', ms | 0) + }) + } + const xapi = createClient({url, auth: {user, password}}) yield xapi.connect() diff --git a/packages/xen-api/src/index.js b/packages/xen-api/src/index.js index 02f995548..a6ebed355 100644 --- a/packages/xen-api/src/index.js +++ b/packages/xen-api/src/index.js @@ -107,10 +107,16 @@ const noop = () => {} // ------------------------------------------------------------------- -const notConnectedPromise = Bluebird.reject(new Error('not connected')) +let getNotConnectedPromise = function () { + const promise = Bluebird.reject(new Error('not connected')) -// Does nothing but avoid a Bluebird message error. -notConnectedPromise.catch(noop) + // Does nothing but avoid a Bluebird message error. + promise.catch(noop) + + getNotConnectedPromise = () => promise + + return promise +} // =================================================================== @@ -155,7 +161,7 @@ export class Xapi extends EventEmitter { this._url = parseUrl(opts.url) this._auth = opts.auth - this._sessionId = notConnectedPromise + this._sessionId = getNotConnectedPromise() this._init() @@ -237,7 +243,7 @@ export class Xapi extends EventEmitter { }) } - this._sessionId = notConnectedPromise + this._sessionId = getNotConnectedPromise() return Bluebird.resolve().then(() => { debug('%s: disconnected', this._humanId) @@ -316,7 +322,7 @@ export class Xapi extends EventEmitter { // Try to login again. debug('%s: the session has been reinitialized', this._humanId) - this._sessionId = notConnectedPromise + this._sessionId = getNotConnectedPromise() return this._sessionCall(method, args) })