使用Tape测试工具实现对超级账本fabric# 前言 如果想测试一下超级账本fabric对某个合约函数的执行时间是多少,简单地可以通过打印合约函数开始执行时间和结束执行时间来计算时间差就可以了。但如果想对某个合约函数进行压力测试呢?那么可能需要自己编写测试工具了。但如果有现成的工具,又何必重复造轮子呢?Tape测试工具可以帮我们实现这个测试工作,而且简单好用。下面就让我们一起来用一下Tape测试工具吧! ### 1 下载Tape Tape的github地址是:[https://github.com/Hyperledger-TWGC/tape](https://github.com/Hyperledger-TWGC/tape) 下载命令 git clone [https://github.com/Hyperledger-TWGC/tape.git](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](https://img.learnblockchain.cn/attachments/2022/01/T1lQ8Q6t61e7a58d7c12f.png) 需要修改内容包括: 相关证书和私钥的对应目录路径,以及通道名称,合约名称,合约函数和函数参数 **config.yaml的示例如下:** “`# Definition of nodes 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/User1@org1.example.com/msp/keystore/priv_sk private_key: ./organizations/peerOrganizations/org1.example.com/users/User1@org1.example.com/msp/keystore/414f06dff0f8f31a52078b6a5def51b6ad001d067028327a1e527dc8ce684719_sk sign_cert: ./organizations/peerOrganizations/org1.example.com/users/User1@org1.example.com/msp/signcerts/User1@org1.example.com-cert.pem num_of_conn: 10 client_per_conn: 10 “` ### 6 启动Fabric

需要修改内容包括:

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

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/User1@org1.example.com/msp/keystore/priv_sk  private_key: ./organizations/peerOrganizations/org1.example.com/users/User1@org1.example.com/msp/keystore/414f06dff0f8f31a52078b6a5def51b6ad001d067028327a1e527dc8ce684719_sk  sign_cert: ./organizations/peerOrganizations/org1.example.com/users/User1@org1.example.com/msp/signcerts/User1@org1.example.com-cert.pem  num_of_conn: 10  client_per_conn: 10

6 启动Fabric

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

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

./tape -c config.yaml -n 1000

上面的命令是直接运行1000次指定的合约函数,这样有可能瞬间的交易量过大,

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注