Caliper 壓力測試工具 (本地測試)#
1. 安裝 nodejs,#
-
使用 nvm 或者系統自帶的包管理器進行安裝(apt 或 yum)
-
nodejs 版本 => 8
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
-
安裝指南:
執行
apt install docker.io 或 yum install docker
3. 部署 Caliper#
- 新建工作目錄
mkdir benchmarks && cd benchmarks
-
初始化 npm 項目
npm init
-
安裝
caliper-cli
npm install --only=prod @hyperledger/[email protected]
-
測試是否安裝成功
npx caliper --version
-
綁定區塊鏈平台
-
由於 Caliper 採用了輕量級的部署方式,因此需要顯式的綁定步驟指定要測試的平台及適配器版本,
caliper-cli
會自動進行相應依賴項的安裝。使用npx caliper bind
命令進行綁定,命令所需的各項參數可以通過如下命令查看: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,可以採用如下方式進行綁定:
npx caliper bind --caliper-bind-sut fisco-bcos --caliper-bind-sdk latest
3. 快速體驗 FISCO BCOS 基準測試#
-
在工作目錄下下載預定義測試用例
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 行為: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 行為:const Color = require('./common').Color;
-
修改
/benchmarks/node_modules/\@hyperledger/caliper-fisco-bcos/lib/web3lib/web3sync.js
第 27 行,91 行,118 行分別修改為27 行:
uuid = '0x' + uuid.replace(/-/g, '');
91 行
extraData: '0x0'
118 行
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
, 在末尾添加: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
/* * 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
/* * 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 文件夾中執行)#
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
指定網絡配置文件
本文參照:
https://fisco-bcos-documentation.readthedocs.io/zh_CN/dev/docs/tutorial/caliper.html