通过Caliper进行压力测试程序
1.环境配置
第一步. 配置基本环境
- 部署Caliper的计算机需要有外网权限;
- 操作系统版本需要满足以下要求:Ubuntu >= 16.04、CentOS >= 7或MacOS >= 10.14;
- 部署Caliper的计算机需要安装有以下软件:python 2.7、make、g++、gcc及git。
第二步. 安装NodeJS
版本要求:
- NodeJS 8 (LTS), 9, 或 10 (LTS),Caliper尚未在更高的NodeJS版本中进行过验证。
安装指南:
- 建议使用nvm(Node Version Manager)安装,nvm的安装方式如下:
首先重新装一个虚拟机(Ubuntu 20.04 安装教程_ubuntu20.04安装教程-CSDN博客)
然后在建立root用户

下载curl命令 apt install curl

安装nvm
curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.33.2/install.sh | bash
# 若出现因网络问题导致长时间下载失败,可尝试以下命令
curl -o- https://gitee.com/mirrors/nvm/raw/v0.33.2/install.sh | bash

# 加载nvm配置
source ~/.$(basename $SHELL)rc

# 安装Node.js 8
nvm install 8

# 使用Node.js 8
nvm use 8

第三步. 部署Docker
# 更新包索引
sudo apt-get update

# 安装基础依赖库
sudo apt-get install \
apt-transport-https \
ca-certificates \
curl \
gnupg-agent \
software-properties-common

# 添加Docker官方GPG key
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -

# 添加docker仓库
sudo add-apt-repository \
"deb [arch=amd64] https://download.docker.com/linux/ubuntu \
$(lsb_release -cs) \
stable"

# 更新包索引
sudo apt-get update

# 安装Docker
sudo apt-get install docker-ce docker-ce-cli containerd.io

第四步. 安装Docker Compose
sudo curl -L "https://github.com/docker/compose/releases/download/1.24.0/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
sudo chmod +x /usr/local/bin/docker-compose


2. Caliper部署
第一步. 部署
Caliper提供了方便易用的命令行界面工具caliper-cli
,推荐在本地进行局部安装:
建立一个工作目录
mkdir benchmarks && cd benchmarks

对NPM项目进行初始化
npm init

这一步主要是为在工作目录下创建package.json文件以方便后续依赖项的安装,如果不需要填写项目信息的话可以直接执行npm init -y
。
安装caliper-cli
npm install --only=prod @hyperledger/caliper-cli@0.2.0

由于Caliper所有依赖项的安装较为耗时,因此使用--only=prod
选项用于指定NPM只安装Caliper的核心组件,而不安装其他的依赖项(如各个区块链平台针对Caliper的适配器)。在部署完成后,可以通过caliper-cli
显式绑定需要测试的区块链平台及相应的适配器。
验证caliper-cli安装成功
npx caliper --version
若安装成功,则会打印相应的版本信息,如:

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

--caliper-bind-sut :用于指定需要测试的区块链平台,即受测系统(***S***ystem ***u***nder ***T***est);
--caliper-bind-sdk:用于指定适配器版本;
--caliper-bind-cwd :用于绑定caliper-cli
的工作目录,caliper-cli
在加载配置文件等场合时均是使用相对于工作目录的相对路径;
--caliper-bind-args :用于指定caliper-cli
在安装依赖项时传递给npm
的参数,如用于全局安装的-g
。
对于FISCO BCOS,可以采用如下方式进行绑定:
npx caliper bind --caliper-bind-sut fisco-bcos --caliper-bind-sdk latest

遇到以上问题第一种可以直接在命令行前面加上sudo
第二种:重新下载nodejs以及npm
sudo apt-get install nodejs npm

然后在重新运行
sudo npx caliper bind --caliper-bind-sut fisco-bcos --caliper-bind-sdk latest

第三步. 快速体验FISCO BCOS基准测试
为方便测试人员快速上手,FISCO BCOS已经为Caliper提供了一组预定义的测试样例,测试对象涵盖HelloWorld合约、Solidity版转账合约及预编译版转账合约。同时在测试样例中,Caliper测试脚本会使用docker在本地自动部署及运行4个互连的节点组成的链,因此测试人员无需手工搭链及编写测试用例便可直接运行这些测试样例。
在工作目录下下载预定义测试用例
git clone https://github.com/vita-dounai/caliper-benchmarks.git
注意 若出现网络问题导致的长时间拉取代码失败,则尝试以下方式:
# 拉取gitee代码
git clone https://gitee.com/vita-dounai/caliper-benchmarks.git

执行HelloWorld合约测试
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
遇到以下错误:

由于FISCO BCOS对于caliper 0.2.0版本的适配存在部分不兼容情况,需要手动按照(https://github.com/FISCO-BCOS/FISCO-BCOS/issues/1248)中的步骤修改代码后方可正常运行。
此报错需要修改源码才能解决:
更改1:
绿色为新增的代码,红色为源代码,需要删除。
文件位置:/node_modules/@hyperledger/caliper-fisco-bcos/lib/fiscoBcos.js


绿色信息增加第一段:
if (this.fiscoBcosSettings.network && this.fiscoBcosSettings.network.authentication) {
for (let k in this.fiscoBcosSettings.network.authentication) {
this.fiscoBcosSettings.network.authentication[k] = CaliperUtils.resolvePath(this.fiscoBcosSettings.network.authentication[k], workspace_root);
}
}
绿色信息增加第二段:
const fiscoBcosSettings = this.fiscoBcosSettings;

更改二:
变动文件一:绿色为新增的代码,红色为源代码,需要删除。
文件位置:/node_modules/@hyperledger/caliper-fisco-bcos/lib/channelPromise.js


绿色增加代码:
let emitter = emitters.get(seq); if(!emitter) {
//Stale message receieved
return;
} emitter = emitter.emitter;

变动文件二:绿色为新增的代码,红色为源代码,需要删除。 文件位置:packages/caliper-fisco-bcos/lib/fiscoBcos.js
绿色增加代码:
const Color = require('./common').Color;


变动文件三:绿色为新增的代码,红色为源代码,需要删除。

文件位置:/node_modules/@hyperledger/caliper-fisco-bcos/lib/web3lib/web3sync.js

27行绿色增加代码:
uuid = '0x' + uuid.replace(/-/g, '');

91行绿色增加代码:
extraData: '0x0'

118行绿色增加代码:
extraData: '0x0'

错误2:
指定secp256k1依赖包时版本限制没写对,导致在绑定时自动安装了4.0版本的secp256k1包,但是最新的4.0的包API全部变了,导致执行出错。
有一个临时的解决方案,进入node_modules/@hyperledger/caliper-fisco-bcos目录,编辑该目录下的package.json文件,在"dependencies"中添加一项"secp256k1": "^3.8.0",随后在该目录下执行npm i,更新完成后测试程序就能启动了。


使用npm i 的时候会出现node和npm版本号兼容的问题

可以升级node的版本号,使用nvm安装node14
nvm install v14.19.0

然后在重新运行npm i

重新执行HelloWorld合约测试
cd 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


执行Solidity版转账合约测试
npx caliper benchmark run --caliper-workspace caliper-benchmarks --caliper-benchconfig benchmarks/samples/fisco-bcos/transfer/solidity/config.yaml --caliper-networkconfig networks/fisco-bcos/4nodes1group/fisco-bcos.json

