Caliper压力测试工具(本地测试)

Caliper 压力测试工具 (本地测试)

1. 安装 nodejs,

使用 nvm 或者系统自带的包管理器进行安装（apt 或 yum）

nodejs 版本 => 8

Copy
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.5/install.sh | bash
或
wget -qO- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.5/install.sh | bash
执行
export NVM_DIR="$([ -z "${XDG_CONFIG_HOME-}" ] && printf %s "${HOME}/.nvm" || printf %s "${XDG_CONFIG_HOME}/nvm")"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # 添加环境变量

2. 部署 Docker

docker 版本 >= 18.06.01

安装指南:

执行

Copy
apt install docker.io
或
yum install docker

3. 部署 Caliper

新建工作目录

Copy
mkdir benchmarks && cd benchmarks

初始化 npm 项目

Copy
npm init

安装caliper-cli

Copy
npm install --only=prod @hyperledger/caliper-cli@0.2.0

测试是否安装成功

Copy
npx caliper --version

绑定区块链平台

由于 Caliper 采用了轻量级的部署方式，因此需要显式的绑定步骤指定要测试的平台及适配器版本，caliper-cli会自动进行相应依赖项的安装。使用npx caliper bind命令进行绑定，命令所需的各项参数可以通过如下命令查看：

Copy
user@ubuntu:~/benchmarks$ npx caliper bind --help
Usage:
caliper bind --caliper-bind-sut fabric --caliper-bind-sdk 1.4.1 --caliper-bind-cwd ./ --caliper-bind-args="-g"

Options:
--help Show help [boolean]
-v, --version Show version number [boolean]
--caliper-bind-sut The name of the platform to bind to [string]
--caliper-bind-sdk Version of the platform SDK to bind to [string]
--caliper-bind-cwd The working directory for performing the SDK install [string]
--caliper-bind-args Additional arguments to pass to "npm install". Use the "=" notation when setting this parameter [string]

对于 FISCO BCOS，可以采用如下方式进行绑定：

Copy
npx caliper bind --caliper-bind-sut fisco-bcos --caliper-bind-sdk latest

3. 快速体验 FISCO BCOS 基准测试

在工作目录下下载预定义测试用例

Copy
git clone https://github.com/vita-dounai/caliper-benchmarks.git
或
git clone https://gitee.com/mirrors_hyperledger/caliper-benchmarks.git

CentOS 系统需关闭 selinux

4. 修改配置

修改 /benchmarks/node_modules/\@hyperledger/caliper-fisco-bcos/lib/channelPromise.js 第 49 行为：

Copy
let emitter = emitters.get(seq);
if(!emitter) {
//Stale message receieved
return;
}
emitter = emitter.emitter;

修改 /benchmarks/node_modules/\@hyperledger/caliper-fisco-bcos/lib/fiscoBcos.js 第 25 行为：

Copy
const Color = require('./common').Color;

修改 /benchmarks/node_modules/\@hyperledger/caliper-fisco-bcos/lib/web3lib/web3sync.js 第 27 行，91 行，118 行分别修改为

27 行：

Copy
uuid = '0x' + uuid.replace(/-/g, '');

91 行

Copy
extraData: '0x0'

118 行

Copy
extraData: '0x0'

修改/benchmarks/node_modules/\@hyperledger/caliper-fisco-bcos中的package.json文件，在dependencies字段中增加secp256k1": "^3.8.0, 随后在该目录下执行npm i

修改/benchmarks/caliper-benchmarks/networks/fisco-bcos/4nodes1group/fisco-bcos.json，删除其中的command字段

修改authentication字段中key,cert,ca字段的路径为ficso/nodes/127.0.0.1/sdk/目录下的对应文件位置

修改nodes字段中的rpcport和channelport为本地链端口

修改/benchmarks/caliper-benchmarks/benchmarks/samples/fisco-bcos/helloworld/config.yaml, 在末尾添加：

Copy
monitor:
type:
- process
process:
- command: node0
arguments: fiscoBcosClientWorker.js
multiOutput: avg
- command: node1
arguments: fiscoBcosClientWorker.js
multiOutput: avg
- command: node2
arguments: fiscoBcosClientWorker.js
multiOutput: avg
- command: node3
arguments: fiscoBcosClientWorker.js
multiOutput: avg
interval: 0.5

以下内容仅以 helloworld 为例

修改/benchmarks/caliper-benchmarks/benchmarks/samples/fisco-bcos/helloworld/目录下的get.js和set.js

get.js

Copy
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

'use strict';

module.exports.info = ' querying name';

let bc, contx;

module.exports.init = function (blockchain, context, args) {
// Do nothing
bc = blockchain;
contx = context;
return Promise.resolve();
};

module.exports.run = function () {
return bc.queryState(contx, 'helloworld', 'v0', null, 'get()');
};

module.exports.end = function () {
// Do nothing
return Promise.resolve();
};

set.js

Copy
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

'use strict';

module.exports.info = ' setting name';

let bc, contx;
let txnPerBatch;

module.exports.init = function (blockchain, context, args) {
txnPerBatch = 1;
bc = blockchain;
contx = context;
return Promise.resolve();
};

/**
* Generates simple workload
* @return {Object} array of json objects
*/
function generateWorkload() {
let workload = [];
for (let i = 0; i < txnPerBatch; i++) {
let w = {
'transaction_type': 'set(string)',
'name': 'hello! - from ' + process.pid.toString(),
};
workload.push(w);
}
return workload;
}

module.exports.run = function () {
let args = generateWorkload();
return bc.invokeSmartContract(contx, 'helloworld', 'v0', args, null);
};

module.exports.end = function () {
// Do nothing
return Promise.resolve();
};

5. 执行 helloworld 测试（在 benchmarks 文件夹中执行）

Copy
npx caliper benchmark run --caliper-workspace caliper-benchmarks --caliper-benchconfig benchmarks/samples/fisco-bcos/helloworld/config.yaml --caliper-networkconfig networks/fisco-bcos/4nodes1group/fisco-bcos.json

参数说明：

--caliper-workspace 指定工作目录

--caliper-benchconfig 指定测试配置文件

--caliper-networkconfig指定网络配置文件