如何使用 jmeter 向区块链提交表单?

How to sumbit a form to blockchain using jmeter?

我在以太坊区块链上使用 React 开发了一个网络应用程序。

我的应用程序中的一个页面从用户那里获取信息,如下所示:

class AssetNew extends Component {
 state = {
  name: "",
  description: "",
  manufacturer: "",
  price: "",
  errorMessage: ""
 };

 onSubmit = async event => {
  event.preventDefault();

  const { name, description, manufacturer, price} = this.state;
  this.setState({errorMessage: "" });

  try {
    const accounts = await web3.eth.getAccounts();
    await tracker.methods
      .createAsset(name, description, manufacturer, price)
      .send({
       from: accounts[0],
       value: web3.utils.toWei(this.state.price, "ether"),
       gas: "1000000"
     });
 } catch (err) {
   this.setState({ errorMessage: err.message });
 }
};

render() {
return (
     <Form onSubmit={this.onSubmit} error={!!this.state.errorMessage}>
       <Form.Field>
         <label>Name</label>
         <Input
           value={this.state.name}
           onChange={event => this.setState({ name: event.target.value })}
        />
       </Form.Field>
       .... // three more from.field for description, manufacturer and price
 );
 }
}
export default AssetNew;

此页面获取名称、描述、制造商和价格,并将其发送到智能合约以注册新产品。在浏览器上一切正常(我可以使用表单创建新产品)。 但是,我无法使用 Jmeter 创建新产品。 我尝试使用 POST 方法发送参数:

运行 之后,测试 Http 请求成功但没有任何反应(我希望 Jmeter 创建一个新产品)。

我应该期望Jmeter通过传递参数来创建一个新产品吗? 我是否正确检查了性能?

创建新产品的智能合约:

contract AssetTracker {
  uint public id;
  uint nonce;
  struct Asset {
    string name;
    string description;
    string manufacture;
    uint price;
    uint id;
    address owner;
    bool initialized;
 }
 Asset[] public assets;

 function createAsset(string name, string description, string manufacture, uint price) public payable{

    id = uint(keccak256(now, msg.sender, nonce)) % 1000;
    nonce++;
    Asset memory newAsset = Asset(name, description, manufacture, price, id, msg.sender, true);
    assets.push(newAsset);
}

如果您能够使用浏览器发送请求,您应该能够使用 JMeter 的 HTTP(S) Test Script Recorder in order to capture the relevant HTTP POST Request and generate HTTP Request 采样器

  1. 准备 JMeter 进行记录。最快的方法是使用 JMeter Templates 功能

    • 从 JMeter 的主菜单中选择文件 - 模板 - 记录并单击 "Create"
    • 展开 HTTP(S) 测试脚本记录器并单击 "Start"

  2. 准备好您的浏览器以进行录制。将其配置为使用 JMeter 作为代理

    • localhost127.0.0.1 作为代理主机
    • 8888作为代理端口
    • 确保代理设置为所有协议并且没有例外
    • 如果您计划记录 HTTPS 流量 - 将 ApacheJMeterTemporaryRootCA.crt 证书导入您的浏览器,当您启动 HTTP(S) 测试脚本记录器时,该证书将在您的 JMeter 安装的 "bin" 文件夹中生成

  3. 在浏览器中执行请求

  4. JMeter 应该捕获请求并将 HTTP 请求采样器存储在线程组 - 记录控制器下。

更多信息:Apache JMeter HTTP(S) Test Script Recorder

或者您可以使用 JMeter Chrome Extension 创建测试计划,在这种情况下您不必担心代理和证书。