前言

HyperBench 为区块链提供了一套通用的性能测试方案,能够适配多种不同的区块链平台,基于脚本和虚拟机可以灵活快速地构建区块链性能测试用例,秉承高效、灵活、可扩展的设计理念,为区块链性能测试标准化、易用化助力,促进区块链技术发展,更好地为区块链业务赋能。

本篇我们主要为大家介绍如何使用HyperBench去测试以太坊。

快速开始

【前置要求】

编译HyperBench需要go版本为1.11及以上版本,使用下方wget命令即可下载go官网安装包:

wget https://golang.google.cn/dl/go1.17.7.linux-amd64.tar.gz

下载完成后将压缩包解压至/usr/local/;

tar -zxvf go1.17.7.linux-amd64.tar.gz -C /usr/local

解压完成后 vim 打开/etc/profile 文件,添加go相关环境变量;

vim /etc/profileexport GOROOT=/usr/local/go export GOPATH=/usr/local/gopathexport PATH=$PATH:$GOROOT/bin:$GOPATH/bin

添加完成后 使用source命令使变量配置生效;

source /etc/profile--使变量配置生效go version --查看golang的版本#使用go version命令查看go是否安装完成go version go1.17.7 linux/amd64--打印输出

完成go环境配置后,首先使用go env命令为go添加一个国内的代理,这里是为了使go在安装一些依赖时可以更快的安装完成;

go env -w GOPROXY=https://goproxy.cn,direct

在编译HyperBench之前需要使用go get命令安装packr二进制,packr会在编译HyperBench时使用到,所以需要提前安装好;

go get -u github.com/gobuffalo/packr/v2/packr2

【编译安装HyperBench】

环境准备完成,那么接下来为大家介绍HyperBench源代码的clone以及编译。

首先源码可以通过github中HyperBench开源项目库clone到本地,项目可以使用https://github.com/hyperbench 进入,项目中可以看到三个库,本次视频使用到的库为hyperbench、hyperbench-plugins,进入到HyperBench仓库,在code菜单中选择https,点击地址右侧复制按钮。

接下来开始clone与编译HyperBench:

#首先创建一个hyperbench的运行目录并进入到目录mkdir /opt/hyperbench cd /opt/hyperbench#然后使用git命令将hyperbench源码clone到本地#这边为了方便演示所有的源码都已经clone好了,所以就只展示下git clone https://github.com/hyperbench/hyperbench.git#clone完成后进入到hyperbench源码目录cd hyperbench#使用make build命令编译hyperbenchmake build#编译完成后使用help来查看编译是否成功./hyperbench --help#如果有输出hyperbench命令使用说明则代表编译成功Usage:hyperbench [command]Examples:hyperbench --doc ./doc (generate document to specify path)Available Commands:completionGenerate the autocompletion script for the specified shellhelpHelp about any commandinitinit a stress test dirnew initialize a test planstart start a benchmarkversion get code versionworkerstart as a worker serverFlags:--debugenable debug mode--doc string use to create doc and specify the doc path-h, --help help for hyperbenchUse "hyperbench [command] --help" for more information about a command.

HyperBench主程序编译完成后,还需要编译HyperBench的区块链适配插件

#进入到前面所创建的hyperbench目录cd /opt/hyperbench#使用git命令将hyperbench-plugins源码clone本地git clone https://github.com/hyperbench/hyperbench-plugins.git#clone完成后进入到hyperbench-plugins目录cd hyperbench-plugins#目录中可以看到hyperbench所适配的各个区块链平台#本次视频是介绍hyperbench对以太坊区块链平台的测试,所以进入以太坊目录cd eth/#进入目录后使用make build命令编译插件make build#编译完成后可以在当前目录看到eth.so文件#需要将eth.so文件copy到hyperbench主程序所在的目录cp eth.so ../../hyperbench/eth.so

【编译安装并配置启动geth】

HyperBench主程序与插件编译完成后,我们还需要搭建一条以太坊私有链以作测试:

#首先创建一个geth目录mkdir /opt/geth/#进入目录cd /opt/geth/#进入到github上以太坊项目库#使用git命令clone 以太坊源码git clone https://github.com/ethereum/go-ethereum.git#clone完成后进入源码目录cd ethereum#使用make命令编译geth主程序make geth#进入到build/bin/目录就能看到geth主程序cd ./build/bin/#使用./geth version查看编译是否成功./geth version#成功输出了版本信息则代表编译成功#然后将geth文件copy到/usr/local/bin/目录,这样就可以在任何目录下执行geth命令cp ./geth /usr/local/bin/

接下来需要创建geth初始化文件,本次教程重点在HyperBench,所以就不详细介绍geth的配置了。

初始化文件中需要注意的是alloc配置项,此配置项是以太坊区块链私链的创世账户,如果初始化时没有创建创世账户并且分配一定的原生币,那在后续测试中的转账交易就无法完成转账。

如果自己在测试中没有创世账户的话,可以使用本次教程提供的账户:

vim genesis.json{"config": {"chainId": 777,"homesteadBlock": 0,"eip150Block": 0,"eip150Hash": "0x0000000000000000000000000000000000000000000000000000000000000000","eip155Block": 0,"eip158Block": 0,"byzantiumBlock": 0,"constantinopleBlock": 0,"petersburgBlock": 0,"istanbulBlock": 0,"ethash": {}},"nonce": "0x0","timestamp": "0x5ddf8f3e","extraData": "0x0000000000000000000000000000000000000000000000000000000000000000","gasLimit": "0x2540be400","difficulty": "0x00002","mixHash": "0x0000000000000000000000000000000000000000000000000000000000000000","coinbase": "0x0000000000000000000000000000000000000000","alloc": {"0x4e5786bf8546e60ca38aa68eb8dc82b1b24d26f3": {"balance": "100000000000000000000000000000000"} },"number": "0x0","gasUsed": "0x0","parentHash": "0x0000000000000000000000000000000000000000000000000000000000000000"}

创建geth数据目录,并初始化目录

mkdir /opt/geth1/#执行初始化命令./geth --datadir /opt/geth1/ init ./genesis.json#执行返回如下则为成功INFO [10-18|10:41:55.859] Maximum peer count ETH=50 LES=0 total=50INFO [10-18|10:41:55.860] Smartcard socket not found, disablingerr="stat /run/pcscd/pcscd.comm: no such file or directory"INFO [10-18|10:41:55.864] Set global gas cap cap=50,000,000INFO [10-18|10:41:55.865] Allocated cache and file handles database=/opt/geth1/geth/chaindata cache=16.00MiB handles=16INFO [10-18|10:41:56.236] Opened ancient databasedatabase=/opt/geth1/geth/chaindata/ancient/chain readonly=falseINFO [10-18|10:41:56.236] Writing custom genesis blockINFO [10-18|10:41:56.238] Persisted trie from memory databasenodes=1 size=153.00B time="171.63µs" gcnodes=0 gcsize=0.00B gctime=0s livenodes=1 livesize=0.00BINFO [10-18|10:41:56.239] Successfully wrote genesis state database=chaindata hash=ced4ca..c9a1f8INFO [10-18|10:41:56.239] Allocated cache and file handles database=/opt/geth1/geth/lightchaindatacache=16.00MiB handles=16INFO [10-18|10:41:56.513] Opened ancient databasedatabase=/opt/geth1/geth/lightchaindata/ancient/chain readonly=falseINFO [10-18|10:41:56.513] Writing custom genesis blockINFO [10-18|10:41:56.514] Persisted trie from memory databasenodes=1 size=153.00B time="91.645µs" gcnodes=0 gcsize=0.00B gctime=0s livenodes=1 livesize=0.00BINFO [10-18|10:41:56.515] Successfully wrote genesis state

初始化完成后,在/opt/geth1/keystore/目录下新增文件

这个文件是在genesis.sjon配置中的创世账户的私钥,默认分配了余额

vim UTC--2022-10-17T08-41-53.685200708Z--4e5786bf8546e60ca38aa68eb8dc82b1b24d26f3#文件内容{"address":"4e5786bf8546e60ca38aa68eb8dc82b1b24d26f3","crypto":{"cipher":"aes-128-ctr","ciphertext":"f26e27f300b7ba46e968f8c656e534cbb706a15c825eb351d2ba4690d1b6bf26","cipherparams":{"iv":"bbb9f922fc48bcb2c1fb566453f328d8"},"kdf":"scrypt","kdfparams":{"dklen":32,"n":262144,"p":1,"r":8,"salt":"f0ffc7fa97c82b94c2fcea53127ea3b8d5110c020830089283173e9d998cb8d9"},"mac":"2b6bdb9ae958ff55516ac313f4b68d5a8c66df018103903d1348a7158fe1fd2c"},"id":"654e1fa7-9c06-4643-bc3d-26b6186ff2cf","version":3}

启动geth

#使用geth命令启动以太坊私链vim scnohup geth --datadir "/opt/geth1/" --http --http.addr=0.0.0.0 --http.port 8545 --authrpc.port 8552 --http.corsdomain "*" --http.api "eth,net,web3,personal,admin,txpool,debug,miner" --nodiscover --maxpeers 30 --networkid 1981 --port 30303 --allow-insecure-unlock2>> /opt/geth1/geth.log

✨ 开源小贴士 ✨

对HyperBench感兴趣的小伙伴,可添加微信小助手18458407117,进入技术交流群讨论.

HyperBench github

https://github.com/hyperbench/hyperbench

HyperBench quickstart

https://github.com/hyperbench/hyperbench/blob/master/README.md