6

使用Tape测试工具实现对超级账本fabric区块链的交易压力测试

 2 years ago
source link: https://learnblockchain.cn/article/3452
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

使用Tape测试工具实现对超级账本fabric区块链的交易压力测试

使用Tape测试工具实现对超级账本fabric区块链的交易压力测试

如果想测试一下超级账本fabric对某个合约函数的执行时间是多少,简单地可以通过打印合约函数开始执行时间和结束执行时间来计算时间差就可以了。但如果想对某个合约函数进行压力测试呢?那么可能需要自己编写测试工具了。但如果有现成的工具,又何必重复造轮子呢?Tape测试工具可以帮我们实现这个测试工作,而且简单好用。下面就让我们一起来用一下Tape测试工具吧!

1 下载Tape

Tape的github地址是:https://github.com/Hyperledger-TWGC/tape

git clone https://github.com/Hyperledger-TWGC/tape.git

假设Tape的下载目录是: ~/code/tape

2 编译Tape

在下载的目录下,运行下面的命令

cd ~/code/tape

go build ./cmd/tape

会在当前目录下生成可执行文件 tape

3 复制Fabric 1.41的私钥和证书

这里以 Fabric 1.41为例,假设 Fabric 1.41的示例是放在这个目录: ~/code/fabric/1.4.1_fabric-samples/

复制Fabric 1.41的私钥和证书到Tape目录下的organizations子目录

cp -r ~/code/fabric/1.4.1_fabric-samples/first-network/crypto-config/ordererOrganizations ~/code/tape/organizations/

cp -r ~/code/fabric/1.4.1_fabric-samples/first-network/crypto-config/peerOrganizations ~/code/tape/organizations/

4 安装测试链码

假设测试链码放在这个目录: ~/code/fabric/1.4.1_fabric-samples/chaincode/tape_test

链码名称是: tapetest

合约函数是:Test

合约函数的参数是2个字符串

安装链码命令(需要进入docker环境):

peer chaincode install -p github.com/chaincode/tape_test -n tapetest -v 1

实例化链码命令(需要进入docker环境):

peer chaincode instantiate -o orderer.example.com:7050 --tls true --cafile /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem -C mychannel -n tapetest -l golang -v 1 -c '{"Args":["init","a","100","b","200"]}' -P 'OR ('''Org1MSP.peer''','''Org2MSP.peer''')'

5 配置config.yaml

Tape的配置文件是config.yaml,对应上面的配置,修改config.yaml,见下图:

configyaml.png

需要修改内容包括:

相关证书和私钥的对应目录路径,以及通道名称,合约名称,合约函数和函数参数

config.yaml的示例如下:

peer1: &peer1

addr: localhost:7051

tls_ca_cert: ./organizations/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/msp/tlscacerts/tlsca.org1.example.com-cert.pem

peer2: &peer2

addr: localhost:9051

tls_ca_cert: ./organizations/peerOrganizations/org2.example.com/peers/peer0.org2.example.com/msp/tlscacerts/tlsca.org2.example.com-cert.pem

orderer1: &orderer1

addr: localhost:7050

tls_ca_cert: ./organizations/ordererOrganizations/example.com/msp/tlscacerts/tlsca.example.com-cert.pem

# Nodes to interact with

endorsers:

- *peer1
- *peer2

# we might support multi-committer in the future for more complex test scenario,

# i.e. consider tx committed only if it's done on >50% of nodes. But for now,

# it seems sufficient to support single committer.

committers:

- *peer1
- *peer2

commitThreshold: 2

orderer: *orderer1

# Invocation configs

#channel: mychannel

#chaincode: basic

channel: mychannel

chaincode: tapetest

args:

- Test
- Hello
- World

#args:

# - Produce

# - create

#args:

# - GetAllAssets

mspid: Org1MSP

#private_key: ./organizations/peerOrganizations/org1.example.com/users/[email protected]/msp/keystore/priv_sk

private_key: ./organizations/peerOrganizations/org1.example.com/users/[email protected]/msp/keystore/414f06dff0f8f31a52078b6a5def51b6ad001d067028327a1e527dc8ce684719_sk

sign_cert: ./organizations/peerOrganizations/org1.example.com/users/[email protected]/msp/signcerts/[email protected]

num_of_conn: 10

client_per_conn: 10

6 启动Fabric区块链

这里以启动Fabric 1.41的示例里的first-network示例为例子:

cd ~/code/fabric/1.4.1_fabric-samples/first-network

./byfn.sh up -v -s couchdb

这里使用couchdb

7 运行Tape

Tape运行命令帮助如下

如果要运行1000次指定的合约函数,可以运行下面的命令:

./tape --config=config.yaml --number=1000

./tape -c config.yaml -n 1000

上面的命令是直接运行1000次指定的合约函数,这样有可能瞬间的交易量过大,区块链处理不过来而导致交易丢失。Tape有2个参数--rate和--brust,利用令牌桶的机制实现限流。

burst参数是一个整数,定义了令牌桶的大小,

rate参数是一个浮点数,定义了每秒钟往令牌桶增加令牌的数量(注意,rate可以是小数,如rate为0.2,表示每秒钟往令牌桶增加0.2个令牌),0表示没有限制

rate参数不能大于burst,否则,rate的大小设为burst的大小

./tape -c config.yaml -n 1000 --rate=0.2 --burst=1 表示令牌桶里的令牌初始值1个,一开始执行1次合约函数(每执行1次合约函数,令牌数减1),令牌桶里的令牌剩余数变为0。后面是每秒增加0.2个令牌,到第5秒时,令牌桶里的令牌数为1,那么再次执行1次合约函数,令牌桶里的令牌剩余数又变为0,如此类推。

./tape -c config.yaml -n 1000 --rate=0.2 --burst=2 表示令牌桶里的令牌初始值2个,一开始执行2次合约函数,令牌桶里的令牌剩余数变为0。后面是每秒增加0.2个令牌,到第5秒时,令牌桶里的令牌数为1,那么再次执行1次合约函数,令牌桶里的令牌剩余数又变为0,如此类推。

8 运行结果

假设只运行2次Test函数, 执行命令:./tape -c config.yaml -n 2

我们可以看到运行结果:

Time 2.15s Block 1049 Tx 2

tx: 2, duration: 2.146063431s, tps: 0.931939

上述结果表示:

总耗时约 2.146063431秒,交易写入的区块号是1049,交易数量是2个,TPS(每秒交易数量)是0.931939 (TPS = 交易数量 / 交易总耗时)

通过Tape工具,可以实现Fabric对某个指定合约函数的压力测试,并得到相应的TPS测试结果


我的知乎:https://www.zhihu.com/people/powervip

我的csdn:https://blog.csdn.net/powervip

我的github:https://powervip.github.io/

我的公众号:区块链战斗机

我的腾讯微云网盘:https://share.weiyun.com/5qT0TvG

*如果你觉得这篇文章写得还可以,请帮忙点个赞,谢谢!如需转载,请注明原文出处并保留原文链接。​

你的鼓励,我的动力!*

本文参与登链社区写作激励计划 ,好文好收益,欢迎正在阅读的你也加入。

  • 发表于 16小时前
  • 阅读 ( 46 )
  • 学分 ( 1 )
  • 分类:联盟链

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK