自学内容网 自学内容网

合约测试用例详解

合约测试的综合案例

第一部分 压力测试(使用caliper工具测试)

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://raw.githubusercontent.com/creationix/nvm/v0.33.2/install.sh | bash
    
    # 若出现因网络问题导致长时间下载失败,可尝试以下命令
    curl -o- https://gitee.com/mirrors/nvm/raw/v0.33.2/install.sh | bash
    
    # 加载nvm配置
    source ~/.$(basename $SHELL)rc
    # 安装Node.js 8
    nvm install 8
    # 使用Node.js 8
    nvm use 8
    
    部署Docker
    • 版本要求:>= 18.06.01

    • 安装指南:

      ubuntu:

# 更新包索引
sudo apt-get update
# 安装基础依赖库
sudo apt-get install \
    apt-transport-https \
    ca-certificates \
    curl \
    gnupg-agent \
    software-properties-common
# 添加Docker官方GPG key
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
# 添加docker仓库
sudo add-apt-repository \
"deb [arch=amd64] https://download.docker.com/linux/ubuntu \
$(lsb_release -cs) \
stable"
# 更新包索引
sudo apt-get update
# 安装Docker
sudo apt-get install docker-ce docker-ce-cli containerd.io

加入Docker用户组

sudo groupadd docker
sudo usermod -aG docker $USER
安装Docker Compose
  • 版本要求:>= 1.22.0
  • 安装指南:
sudo curl -L "https://github.com/docker/compose/releases/download/1.24.0/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
sudo chmod +x /usr/local/bin/docker-compose

2. Caliper部署

部署

Caliper提供了方便易用的命令行界面工具caliper-cli,推荐在本地进行局部安装:

  1. 建立一个工作目录:

    mkdir benchmarks && cd benchmarks
    

    2.对NPM项目进行初始化

npm init

这一步主要是为在工作目录下创建package.json文件以方便后续依赖项的安装,如果不需要填写项目信息的话可以直接执行npm init -y

  1. 安装caliper-cli
npm install --only=prod @hyperledger/caliper-cli@0.2.0

由于Caliper所有依赖项的安装较为耗时,因此使用--only=prod选项用于指定NPM只安装Caliper的核心组件,而不安装其他的依赖项(如各个区块链平台针对Caliper的适配器)。在部署完成后,可以通过caliper-cli显式绑定需要测试的区块链平台及相应的适配器。

  1. 验证caliper-cli安装成功
npx caliper --version
绑定

由于Caliper采用了轻量级的部署方式,因此需要显式的绑定步骤指定要测试的平台及适配器版本,caliper-cli会自动进行相应依赖项的安装。使用npx caliper bind命令进行绑定,命令所需的各项参数可以通过如下命令查看:

对于FISCO BCOS,可以采用如下方式进行绑定:

npx caliper bind --caliper-bind-sut fisco-bcos --caliper-bind-sdk latest

至此caliper环境配置工作准备完毕,现在我们就来进行基于caliper的压力测试吧。

caliper文件结构

在这里插入图片描述

案例一:测试helloworld合约

合约代码如下:

pragma solidity >=0.4.24 <0.6.11;

contract HelloWorld {
    string name;
    constructor () public {
        name = "hello word!";
    }
    function get() public view returns(string memory){
        return name;
    }
    function set(string memory n )public {
        name = n;
    }
}

编写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
  - 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
monitor:
  type:
  - docker
  - process
  docker:
    name:
    - node0
    - node1
    - node2
    - node3
  process:
    - command: node
      arguments: fiscoBcosClientWorker.js
      multiOutput: avg
  interval: 0.5

编写测试用例(get方法):

'use strict';

module.exports.info = 'helloworld';

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();
};

在这里插入图片描述

测试用例二(使用set方法):

'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();
};

/**
 * Generates simple workload
 * @return {Object} array of json objects
 */
function generateWorkload() {
   
    let workload =

原文地址:https://blog.csdn.net/2401_84837659/article/details/142769262

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!