目前FISCO BCOS适配的Caliper版本为0.2.0,请在部署Caliper运行环境时确保Caliper的版本为0.2.0,如在部署或使用过程中遇到任何问题,请优先参考 https://github.com/FISCO-BCOS/FISCO-BCOS/issues/1248 中的解决方案进行排查。
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的安装方式如下:
# 安装nvm
curl -o- https://gitee.com/mirrors/nvm/raw/v0.33.2/install.sh | bash
# 加载nvm配置
source ~/.$(basename $SHELL)rc
# 打开环境配置文件,追加淘宝镜像
# nvm淘宝镜像
export NVM_NODEJS_ORG_MIRROR=http://npm.taobao.org/mirrors/node
# 安装Node.js 8
nvm install 8
# 使用Node.js 8
nvm use 8
第三步. 部署Docker
bash <(curl -s -L get.docker.com)
第四步. 安装Docker Compose
curl -L "https://github.com/docker/compose/releases/download/1.24.0/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
chmod +x /usr/local/bin/docker-compose
docker-compose --version
2. Caliper部署
第一步. 部署
npm 安装
# 建立工作目录
mkdir caliper-benchmarks && cd caliper-benchmarks
# npm项目初始化
npm init -y
这一步主要是为在工作目录下创建package.json文件以方便后续依赖项的安装,如果不需要填写项目信息的话可以直接执行npm init -y。
# 安装 caliper-cli
npm install --only=prod @hyperledger/caliper-cli@0.2.0
# 验证安装成功
npx caliper --version
拉源码安装
git clone https://github.com/hyperledger/caliper-benchmarks.git
cd caliper-benchmarks
git checkout v0.2.0
npx caliper --version
第二步. 绑定
–caliper-bind-sut :用于指定需要测试的区块链平台,即受测系统(System under Test);
–caliper-bind-sdk:用于指定适配器版本;
–caliper-bind-cwd:用于绑定caliper-cli
的工作目录,caliper-cli
在加载配置文件等场合时均是使用相对于工作目录的相对路径;
caliper-bind-args:用于指定caliper-cli
在安装依赖项时传递给npm
的参数,如用于全局安装的-g
。
ps:chmod 777 caliper-benchmarks
# tmd,我也不知道为什么700,用root执行不行,必须755把,下面才能绑定成功
# 对fisco 进行绑定
npx caliper bind --caliper-bind-sut fisco-bcos --caliper-bind-sdk latest
3.自定义测试用例
Caliper前后端分离的设计原则使得只要后端的区块链系统开放了相关网络端口,Caliper便可以对该系统进行测试。结合Docker提供的性能数据统计服务或本地的ps命令工具,Caliper能够在测试的同时收集节点所在机器上的各种性能数据,包括CPU、内存、网络及磁盘的使用等。尽管Caliper能工作在不使用Docker模式而是使用原生二进制ficos-bcos可执行程序搭建出的链上,但是那样Caliper将无法获知节点所在机器上的资源消耗
。因此,在目前的Caliper版本下(v0.2.0),我们推荐使用Docker模式搭链
。
配置区块链网络
当我们在使用caliper去进行测试的时候,是测试两种东西,一种是性能,一种是网络,所以命令相对较长:
npx caliper benchmark run --caliper-workspace caliper-benchmarks --caliper-benchconfig /root/tools/caliper/config.yaml --caliper-networkconfig /root/tools/caliper/fisco-bcos.json
其中,
–caliper-workspace:用于指定caliper-cli的工作目录
,如果没有绑定工作目录,可以通过该选项动态指定工作目录;
–caliper-benchconfig:用于指定测试配置文件
,测试配置文件中包含测试的具体参数,如交易的发送方式、发送速率控制器类型、性能监视器类型等;
–caliper-networkconfig:用于指定网络配置文件
,网络配置文件中会指定Caliper与受测系统的连接方式及要部署测试的合约等。
相关配置文件解析:
配置fisco-bcos适配器
fisco-bcos.json文件
{
"caliper": {
"blockchain": "fisco-bcos"
},
"fisco-bcos": {
"config": {
"privateKey": "bcec428d5205abe0f0cc8a734083908d9eb8563e31f943d760786edf42ad67dd",
"account": "0x64fa644d2a694681bd6addd6c5e36cccd8dcdde3"
},
"network": {
"nodes": [
{
"ip": "127.0.0.1",
"rpcPort": "8545",
"channelPort": "20200"
},
{
"ip": "127.0.0.1",
"rpcPort": "8546",
"channelPort": "20201"
},
{
"ip": "127.0.0.1",
"rpcPort": "8547",
"channelPort": "20202"
},
{
"ip": "127.0.0.1",
"rpcPort": "8548",
"channelPort": "20203"
}
],
"authentication": {
"key": "./networks/fisco-bcos/fisco/nodes/127.0.0.1/sdk/node.key",
"cert": "./networks/fisco-bcos/fisco/nodes/127.0.0.1/sdk/node.crt",
"ca": "./networks/fisco-bcos/fisco/nodes/127.0.0.1/sdk/ca.crt"
},
"groupID": 1,
"timeout": 100000
},
"smartContracts": [
{
"id": "helloworld",
"path": "src/fisco-bcos/helloworld/HelloWorld.sol",
"language": "solidity",
"version": "v0"
}
]
},
"info": {
"Version": "2.0.0",
"Size": "4 Nodes",
"Distribution": "Single Host"
}
}
caliper.command.start:
启动Caliper时会首先执行的start配置中的命令,主要用于初始化caliper.command.end:
退出Caliper时会执行的end配置中的命令。network.nodes:
一个包含了所有要连接节点的列表,列表中的每一项需要指明被连接节点的IP地址,RPC端口以及Channel端口号,所有端口号需要和节点的配置文件保持一致。network.authentication:
即节点下的sdk文件夹中的所对应的证书文件和配置文件。ps:可以不写。
network.smartContracts:
要测试的合约,目前FISCO BCOS适配器支持通过language字段指定两种类型的合约——Solidity合约和预编译合约,当测试合约为Solidity合约时,language字段需要指定为solidity,当测试合约为预编译合约时,language字段需要指定为precompiled。当测试合约为预编译合约时,需要在address字段中指定预编译合约的地址,否则需要在path字段中指定Solidity合约的路径。
config.yaml文件
---
test:
name: Hello World
description: This is a helloworld benchmark of FISCO BCOS for caliper
clients:
type: local
number: 1
rounds:
- label: get
description: Test performance of getting name
txNumber:
- 10000
rateControl:
- type: fixed-rate
opts:
tps: 1000
callback: benchmarks/samples/fisco-bcos/helloworld/get.js # PS
- label: set
description: Test performance of setting name
txNumber:
- 10000
rateControl:
- type: fixed-rate
opts:
tps: 1000
callback: benchmarks/samples/fisco-bcos/helloworld/set.js # PS
monitor:
type:
# - docker
- process
# docker:
# name:
# - node0
# - node1
# - node2
# - node3
process:
- command: node
# arguments: fiscoBcosClientWorker.js
multiOutput: avg
interval: 0.5
- 测试内容配置
test项负责对测试内容进行配置。配置主要集中在round字段中指定如何对区块链系统进行测试。每一个测试可以包含多轮,每一轮可以向区块链发起不同的测试请求。具体要HelloWorld合约测试,测试中包含两轮,分别对合约的get接口和set接口进行测试
。在每一轮测试中,可以通过txNumber或txDuration字段指定测试的交易发送数量或执行时间
,并通过rateControl字段指定交易发送时的速率控制器
,在本节的示例中,使用了QPS为1000的匀速控制器,更多速率控制器的介绍可以参考官方文档。
- 性能监视器配置
非docker---------
1.monitor.type,需要指定为
process
,只对进程进行监控;2.monitor.process,一个包含所有要监视的进称列表,
其中每个进程的command属性为一个正则表达式,表示进程名称
;每个进程还可以有一个arguments属性(未在上述示例中使用到),表示进程的参数。
Caliper会先使用ps命令搜索commad + arguments,然后匹配以得到目标的进程的进程ID及系统资源的使用情况
。每个进程的multiOutput属性用于指定结果的输出方式,目前支持平均值(avg)及总和(sum)
两种方式;3.monitor.interval,监视器的采样间隔,单位为秒。
docker----------
工作负载文件
工作负载文件即一个js文件,本身实现没有过多的要求,可以在此模块中实现任何逻辑。但有三个函数是必需的:
- init(blockchain,context,args)
init函数是在测试开始时被调用的,需要接收三个参数,第一个是区块链实例
,第二个是区块链的上下文
,第三个是用户提供的设置对象
。 - run() => Promise
run函数是在速率控制器每次启动发送交易时
被调用的,里面必须要有invokeSmartContract
或者queryState函数
。 - end()
end()函数是在每一轮测试结束时被调用
的,工作负载模块可以在这个时刻进行
set.js举例
一个需要增删改合约数据
'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();
};
/**
* 生成调用合约函数的abi
* @param
* @return
*/
function generateWorkload() {
let workload = [];
for (let i = 0; i < txnPerBatch; i++) {
let w = {
'transaction_type': 'set(string)',
'n': '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();
};
get.js举例
'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();
};
负载文件对应的合约文件
pragma solidity ^0.4.2;
contract HelloWorld {
string name;
constructor() public {
name = "Hello, World!";
}
function get() public view returns (string) {
return name;
}
function set(string n) public {
name = n;
}
}