文章目录
简要分析caliper 的代码实现,以
benchmark/simple
为例。
根据 caliper架构图:

Application layer
应用程序层包含针对典型区块链方案实施的测试。 每个测试都有一个配置文件,用于定义后端区块链网络和测试参数。 这些测试可以直接用于测试区块链系统的性能。
开发人员可以直接使用NBI来实现自己的测试而不需要框架。
Adaptation layer
适配层主要作用是将现有的区块链系统集成到caliper框架中。每个适配器使用相应的区块链原生(native)SDK或RESTful API实现 ‘Caliper Blockchain NBI’。
现在支持Hyperledger Fabric1.0 和 Sawtooth,以太坊和其他的区块链系统仍在计划中。
src
文件夹下为该项目的主要代码。
src/comm
包内是Interface & Core Layer的代码;
src/fabric
、src/sawtooth
、src/iroha
包内是Adaptation layer的具体代码。
fabric.js
中的Fabric
类就继承自BlockchainInterface
,实现了初始化、安装/执行智能合约等功能。
这些功能内部自然就是调用了fabric-client
,所以测试前需要先进行安装:
npm install grpc@1.10.1 fabric-ca-client fabric-client
Benchmark layer
测试用例位于benchmark/
目录下, 由测试人员编写,并配置到-c
指定的配置文件的test.rounds[.callback]
中。
每个测试用例都包含定义了具体功能的.js
文件(如main.js
,query.js
)和两种配置文件(config.json和fabric.json)。如下为marbles示例:
Configuration file
使用两种配置文件:
config.json
:benchmark配置文件,定义benchmark测试的参数,如测试轮次,工作负载等;
fabric.json
:区块链配置文件,指定了与SUT(system under test)交互的必要信息,即区块链网络的配置,如peer数,client数等。
这两个配置文件很重要,caliper就是根据这两个配置文件的设置来进行测试的。
- benchmark configuration file
example:
{
"blockchain": {
"type": "fabric",
"config": "./fabric.json"
},
"command" : {
"start": "docker-compose -f ../../network/fabric/simplenetwork/docker-compose.yaml up -d",
"end" : "docker-compose -f ../../network/fabric/simplenetwork/docker-compose.yaml down;docker rm $(docker ps -aq)"
},
"test": {
"name": "simple",
"description" : "This is an example benchmark for caliper",
"clients": {
"type": "local",
"number": 5
},
"rounds": [{
"label" : "open",
"txNumber" : [5000, 5000, 5000],
"rateControl" : [{
"type": "fixed-rate", "opts": {
"tps" : 100}}, {
"type": "fixed-rate", "opts": {
"tps" : 200}}, {
"type": "fixed-rate", "opts": {
"tps" : 300}}],
"arguments": {
"money": 10000 },
"callback" : "benchmark/simple/open.js"
},
{
"label" : "query",
"txNumber" : [5000, 5000],
"rateControl" : [{
"type": "fixed-rate", "opts": {
"tps" : 300}}, {
"type": "fixed-rate", "opts": {
"tps" : 400}}],
"callback" : "benchmark/simple/query.js"
}]
},
"monitor": {
"type": ["docker", "process"],
"docker":{
"name": ["peer0.org1.example.com", "http://192.168.1.100:2375/orderer.example.com"]
},
"process": [
{
"command" : "node",
"arguments" : "local-client.js",
"multiOutput" : "avg"
}
],
"interval": 1
}
}
- Fabric Configuration file
example
{
"fabric": {
"cryptodir": "network/fabric/simplenetwork/crypto-config",
"network": {
"orderer": {
"url": "grpcs://localhost:7050",
"mspid": "OrdererMSP",
"domain": "example.com",
"user"