Skip to content

Commit

Permalink
Merge pull request #639 from aklenik/round-config-refactor
Browse files Browse the repository at this point in the history
Flatten round configuration and consolidate related components
  • Loading branch information
aklenik authored Nov 11, 2019
2 parents bf55479 + 7b7b899 commit 788f31c
Show file tree
Hide file tree
Showing 50 changed files with 993 additions and 907 deletions.
17 changes: 2 additions & 15 deletions packages/caliper-burrow/lib/burrowClientFactory.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,28 +22,15 @@ const path = require('path');
*/
class BurrowClientFactory {

/**
* Require paths to configuration data used when calling new on fabric.js
* @param {String} absNetworkFile absolute workerPath
* @param {Sting} workspace_root root location
*/
constructor(absNetworkFile, workspace_root){
this.absNetworkFile = absNetworkFile;
this.workspaceRoot = workspace_root;
}


/**
* Spawn the worker and perform required init
* @returns {Object} the child process
*/
async spawnWorker() {
const child = childProcess.fork(path.join(__dirname, './burrowClientWorker.js'), process.argv.slice(1), { env: process.env});
const child = childProcess.fork(path.join(__dirname, './burrowClientWorker.js'), process.argv.slice(2), { env: process.env});

const msg = {
type: 'init',
absNetworkFile: this.absNetworkFile,
networkRoot: this.workspaceRoot
type: 'init'
};
child.send(msg);

Expand Down
48 changes: 17 additions & 31 deletions packages/caliper-burrow/lib/burrowClientWorker.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,41 +14,27 @@

'use strict';

const { CaliperLocalClient, CaliperUtils } = require('@hyperledger/caliper-core');
const { MessageHandler } = require('@hyperledger/caliper-core');
const BurrowClient = require('./burrow');

let caliperClient;
/**
* Message handler
* Handles the init message. Constructs the Burrow adapter.
* @param {object} context The context of the message handler object.
* @param {object} message The message object.
* @return {Promise<BurrowClient>} The initialized adapter instance.
* @async
*/
process.on('message', async (message) => {
async function initHandler(context, message) {
return new BurrowClient(context.networkConfigPath, context.workspacePath);
}

if (!message.hasOwnProperty('type')) {
process.send({ type: 'error', data: 'unknown message type' });
return;
}

try {
switch (message.type) {
case 'init': {
const blockchain = new BurrowClient(message.absNetworkFile, message.networkRoot);
caliperClient = new CaliperLocalClient(blockchain);
process.send({ type: 'ready', data: { pid: process.pid, complete: true } });
break;
}
case 'test': {
let result = await caliperClient.doTest(message);
const handlerContext = new MessageHandler({
init: initHandler
});

await CaliperUtils.sleep(200);
process.send({ type: 'testResult', data: result });
break;
}
default: {
process.send({ type: 'error', data: 'unknown message type [' + message.type + ']' });
}
}
}
catch (err) {
process.send({ type: 'error', data: err.toString() });
}
/**
* Message handler
*/
process.on('message', async (message) => {
await MessageHandler.handle(handlerContext, message);
});
6 changes: 0 additions & 6 deletions packages/caliper-cli/caliper.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,7 @@
'use strict';

process.env.SUPPRESS_NO_CONFIG_WARNING = true;
const cmdUtil = require('./lib/utils/cmdutils');
const yargs = require('yargs');
const chalk = require('chalk');
const version = 'v' + require('./package.json').version;

let results = yargs
Expand All @@ -35,11 +33,7 @@ let results = yargs
.argv;

results.thePromise.then( () => {
if (!results.quiet) {
cmdUtil.log(chalk.green('\nCommand succeeded\n'));
}
process.exit(0);
}).catch((error) => {
cmdUtil.log(error.stack+chalk.red('\nCommand failed\n'));
process.exit(1);
});
69 changes: 44 additions & 25 deletions packages/caliper-cli/lib/benchmark/lib/runBenchmark.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,8 @@

'use strict';

const {CaliperFlow, CaliperUtils, ConfigUtil} = require('@hyperledger/caliper-core');
const chalk = require('chalk');
const cmdUtil = require('../../utils/cmdutils');
const {CaliperEngine, CaliperUtils, ConfigUtil} = require('@hyperledger/caliper-core');
const logger = CaliperUtils.getLogger('CLI');
const path = require('path');
const fs = require('fs');
/**
Expand All @@ -30,46 +29,66 @@ class RunBenchmark {
* @param {string} argv argument list from caliper benchmark command
*/
static async handler(argv) {
let workspace = ConfigUtil.get(ConfigUtil.keys.Workspace, './');
let benchConfigFile = ConfigUtil.get(ConfigUtil.keys.BenchConfig, undefined);
let blockchainConfigFile = ConfigUtil.get(ConfigUtil.keys.NetworkConfig, undefined);
let workspacePath = ConfigUtil.get(ConfigUtil.keys.Workspace);
let benchmarkConfigPath = ConfigUtil.get(ConfigUtil.keys.BenchConfig);
let networkConfigPath = ConfigUtil.get(ConfigUtil.keys.NetworkConfig);

// Workspace is expected to be the root location of working folders
workspace = path.resolve(workspace);
benchConfigFile = path.isAbsolute(benchConfigFile) ? benchConfigFile : path.join(workspace, benchConfigFile);
blockchainConfigFile = path.isAbsolute(blockchainConfigFile) ? blockchainConfigFile : path.join(workspace, blockchainConfigFile);
workspacePath = path.resolve(workspacePath);
benchmarkConfigPath = CaliperUtils.resolvePath(benchmarkConfigPath, workspacePath);
networkConfigPath = CaliperUtils.resolvePath(networkConfigPath, workspacePath);

if(!benchConfigFile || !fs.existsSync(benchConfigFile)) {
throw(new Error(`Benchmark configuration file "${benchConfigFile || 'UNSET'}" does not exist`));
if(!benchmarkConfigPath || !fs.existsSync(benchmarkConfigPath)) {
let msg = `Benchmark configuration file "${benchmarkConfigPath || 'UNSET'}" does not exist`;
logger.error(msg);
throw new Error(msg);
}

if(!blockchainConfigFile || !fs.existsSync(blockchainConfigFile)) {
throw(new Error(`Network configuration file "${blockchainConfigFile || 'UNSET'}" does not exist`));
if(!networkConfigPath || !fs.existsSync(networkConfigPath)) {
let msg = `Network configuration file "${networkConfigPath || 'UNSET'}" does not exist`;
logger.error(msg);
throw new Error(msg);
}

let benchmarkConfig = CaliperUtils.parseYaml(benchmarkConfigPath);
let networkConfig = CaliperUtils.parseYaml(networkConfigPath);

let blockchainType = '';
let networkObject = CaliperUtils.parseYaml(blockchainConfigFile);
if (networkObject.hasOwnProperty('caliper') && networkObject.caliper.hasOwnProperty('blockchain')) {
blockchainType = networkObject.caliper.blockchain;
if (networkConfig.caliper && networkConfig.caliper.blockchain) {
blockchainType = networkConfig.caliper.blockchain;
} else {
throw new Error('The configuration file [' + blockchainConfigFile + '] is missing its "caliper.blockchain" attribute');
let msg = `Network configuration file "${networkConfigPath}" is missing its "caliper.blockchain" attribute`;
logger.error(msg);
throw new Error(msg);
}

let knownError = false;

try {
cmdUtil.log(chalk.blue.bold('Benchmark for target Blockchain type ' + blockchainType + ' about to start'));
const {AdminClient, ClientFactory} = require('@hyperledger/caliper-' + blockchainType);
const adminClient = new AdminClient(blockchainConfigFile, workspace);
const clientFactory = new ClientFactory(blockchainConfigFile, workspace);
logger.info(`Set workspace path: ${workspacePath}`);
logger.info(`Set benchmark configuration path: ${benchmarkConfigPath}`);
logger.info(`Set network configuration path: ${networkConfigPath}`);
logger.info(`Detected SUT type: ${blockchainType}`);

const response = await CaliperFlow.run(benchConfigFile, blockchainConfigFile, adminClient, clientFactory, workspace);
const {AdminClient, ClientFactory} = require(`@hyperledger/caliper-${blockchainType}`);
const blockchainAdapter = new AdminClient(networkConfigPath, workspacePath);
const workerFactory = new ClientFactory();

const engine = new CaliperEngine(benchmarkConfig, networkConfig, blockchainAdapter, workerFactory);
const response = await engine.run();

if (response === 0) {
cmdUtil.log(chalk.blue.bold('Benchmark run successful'));
logger.info('Benchmark successfully finished');
} else {
cmdUtil.log(chalk.red.bold('Benchmark failure'));
throw new Error('Benchmark failure');
knownError = true;
let msg = `Benchmark failed with error code ${response}`;
logger.error(msg);
throw new Error(msg);
}
} catch (err) {
if (!knownError) {
logger.error(`Unexpected error during benchmark execution: ${err.stack || err}`);
}
throw err;
}
}
Expand Down
3 changes: 1 addition & 2 deletions packages/caliper-cli/lib/bind/bind.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
'use strict';

const { CaliperUtils, ConfigUtil } = require('@hyperledger/caliper-core');
const chalk = require('chalk');
const cmdUtil = require('../utils/cmdutils');
const path = require('path');

Expand Down Expand Up @@ -107,7 +106,7 @@ class Bind {
settings = {};
}

cmdUtil.log(chalk.blue.bold(`Binding for ${sut}@${sdk}. This might take some time...`));
logger.info(`Binding for ${sut}@${sdk}. This might take some time...`);
try {
// combine, then convert the arguments to an array
let npmArgs;
Expand Down
12 changes: 0 additions & 12 deletions packages/caliper-cli/lib/utils/cmdutils.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,6 @@ const { spawn }= require('child_process');
*/
class CmdUtil {


/** Simple log method to output to the console
* Used to put a single console.log() here, so eslinting is easier.
* And if this needs to written to a file at some point it is also eaiser
*/
static log(){
Array.from(arguments).forEach((s)=>{
// eslint-disable-next-line no-console
console.log(s);
});
}

/**
* Invokes a given command in a spawned child process and attaches all standard IO.
* @param {string} cmd The command to be run.
Expand Down
1 change: 0 additions & 1 deletion packages/caliper-cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
"@hyperledger/caliper-sawtooth": "0.2.0",
"@hyperledger/caliper-ethereum": "0.2.0",
"@hyperledger/caliper-fisco-bcos": "0.2.0",
"chalk": "1.1.3",
"yargs": "10.0.3"
},
"devDependencies": {
Expand Down
17 changes: 2 additions & 15 deletions packages/caliper-composer/lib/composerClientFactory.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,28 +22,15 @@ const path = require('path');
*/
class ComposerClientFactory {

/**
* Require paths to configuration data used when calling new on fabric.js
* @param {String} absNetworkFile absolute workerPath
* @param {Sting} workspace_root root location
*/
constructor(absNetworkFile, workspace_root){
this.absNetworkFile = absNetworkFile;
this.workspaceRoot = workspace_root;
}


/**
* Spawn the worker and perform required init
* @returns {Object} the child process
*/
spawnWorker() {
const child = childProcess.fork(path.join(__dirname, './composerClientWorker.js'), process.argv.slice(1), { env: process.env});
const child = childProcess.fork(path.join(__dirname, './composerClientWorker.js'), process.argv.slice(2), { env: process.env});

const msg = {
type: 'init',
absNetworkFile: this.absNetworkFile,
networkRoot: this.workspaceRoot
type: 'init'
};
child.send(msg);

Expand Down
48 changes: 17 additions & 31 deletions packages/caliper-composer/lib/composerClientWorker.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,41 +14,27 @@

'use strict';

const {CaliperLocalClient, CaliperUtils} = require('@hyperledger/caliper-core');
const { MessageHandler } = require('@hyperledger/caliper-core');
const ComposerClient = require('./composer');

let caliperClient;
/**
* Message handler
* Handles the init message. Constructs the Composer adapter.
* @param {object} context The context of the message handler object.
* @param {object} message The message object.
* @return {Promise<ComposerClient>} The initialized adapter instance.
* @async
*/
process.on('message', async (message) => {
async function initHandler(context, message) {
return new ComposerClient(context.networkConfigPath, context.workspacePath);
}

if (!message.hasOwnProperty('type')) {
process.send({type: 'error', data: 'unknown message type'});
return;
}

try {
switch (message.type) {
case 'init': {
const blockchain = new ComposerClient(message.absNetworkFile, message.networkRoot);
caliperClient = new CaliperLocalClient(blockchain);
process.send({type: 'ready', data: {pid: process.pid, complete: true}});
break;
}
case 'test': {
let result = await caliperClient.doTest(message);
const handlerContext = new MessageHandler({
init: initHandler
});

await CaliperUtils.sleep(200);
process.send({type: 'testResult', data: result});
break;
}
default: {
process.send({type: 'error', data: 'unknown message type [' + message.type + ']'});
}
}
}
catch (err) {
process.send({type: 'error', data: err.toString()});
}
/**
* Message handler
*/
process.on('message', async (message) => {
await MessageHandler.handle(handlerContext, message);
});
3 changes: 2 additions & 1 deletion packages/caliper-core/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,5 @@ module.exports.TxStatus = require('./lib/common/core/transaction-status');
module.exports.CaliperUtils = require('./lib/common/utils/caliper-utils');
module.exports.Version = require('./lib/common/utils/version');
module.exports.ConfigUtil = require('./lib/common/config/config-util');
module.exports.CaliperFlow = require('./lib/master/caliper-flow');
module.exports.MessageHandler = require('./lib/worker/client/message-handler');
module.exports.CaliperEngine = require('./lib/master/caliper-engine');
4 changes: 2 additions & 2 deletions packages/caliper-core/lib/common/utils/caliper-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ class CaliperUtils {
* @param {String} root_path root path to use
* @return {String} The resolved absolute path.
*/
static resolvePath(relOrAbsPath, root_path) {
static resolvePath(relOrAbsPath, root_path = undefined) {
if (!relOrAbsPath) {
throw new Error('Util.resolvePath: Parameter is undefined');
}
Expand All @@ -73,7 +73,7 @@ class CaliperUtils {
return relOrAbsPath;
}

return path.resolve(root_path, relOrAbsPath);
return path.resolve(root_path || Config.get(Config.keys.Workspace), relOrAbsPath);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion packages/caliper-core/lib/common/utils/logging-util.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ function _messageFormat() {
output = output.replace(labelRegex, info.label || '');
output = output.replace(moduleRegex, info.module || '');
output = output.replace(messageRegex, info.message || '');
return output.replace(metadataRegex, info.meta || '');
return output.replace(metadataRegex, info.metadata || '');
});
}

Expand Down
Loading

0 comments on commit 788f31c

Please sign in to comment.