zoukankan      html  css  js  c++  java
  • jmeter系列-如何实现像loadrunner一样,多个并发用户先通过登录初始化,然后做并发的接口性能压测

    自动转开发后,就很少关注性能测试方面的东西,最近在帮朋友做一个性能压测,由于朋友那边的公司比较小,环境比较简单,而且是对http服务进行的压测,所以最终

    选用了jmeter来实现这个压测。

      

    如下就是我们的场景,多个并发用户,每个用户需要先登录,而且只需要登录一次,每个并发登录成功后,会产生一个accessToken,也是就是代表了登录服务器端登录鉴权通过后,

    返回给请求调用的一个标志,在后面具体的http接口请求中,每个并发用户都需要在请求中传入这个accessToken,不然的话,服务器端请求的鉴权就失败。

    1、需要做登录,而且登录只需要一次即可,这个时候,我们能想到的就是用jmeter 逻辑控制器中的仅一次控制器来实现。

    看下面的这个图,我们新建了一个仅一次控制器,然后在这个控制器下面添加了一个登录操作的http请求。

    这是一个标准的restful 形式的登录接口。从图片中的请求路径就可以看出来。

    2、你肯定也看到了,这个路径中的/MJJX/users/login/${user},${user}我们做了参数化,因为我们要支持多个并发用户。那么如何来参数化的呢,我们用了jmeter配置元件下的CSV Data Set Config组件。

    下图中可以看到,我们在本地有一个参数化文件。然后设置了user,idid变量用于登录。上一步骤中的${user}变量就是从这个地方取过来的。

    3、登录成功后,那么我们需要获取服务端返回的accesstoken值,这里我们使用了后置处理器下面的JSON Path PostProcessor组件来处理。我们通过json path $.data.accesstoken来获取accesstoken

    注意:这里我们选择的是Sub-sanples-only。 因为我们只需要对子流程进行处理。

    JsonPath的语法可以参考(参考自github):

    https://github.com/json-path/JsonPath

    Path Examples

    Given the json

    {
        "store": {
            "book": [
                {
                    "category": "reference",
                    "author": "Nigel Rees",
                    "title": "Sayings of the Century",
                    "price": 8.95
                },
                {
                    "category": "fiction",
                    "author": "Evelyn Waugh",
                    "title": "Sword of Honour",
                    "price": 12.99
                },
                {
                    "category": "fiction",
                    "author": "Herman Melville",
                    "title": "Moby Dick",
                    "isbn": "0-553-21311-3",
                    "price": 8.99
                },
                {
                    "category": "fiction",
                    "author": "J. R. R. Tolkien",
                    "title": "The Lord of the Rings",
                    "isbn": "0-395-19395-8",
                    "price": 22.99
                }
            ],
            "bicycle": {
                "color": "red",
                "price": 19.95
            }
        },
        "expensive": 10
    }


    JsonPath (click link to try) Result
    $.store.book[*].author The authors of all books
    $..author All authors
    $.store.* All things, both books and bicycles
    $.store..price The price of everything
    $..book[2] The third book
    $..book[-2] The second to last book
    $..book[0,1] The first two books
    $..book[:2] All books from index 0 (inclusive) until index 2 (exclusive)
    $..book[1:2] All books from index 1 (inclusive) until index 2 (exclusive)
    $..book[-2:] Last two books
    $..book[2:] Book number two from tail
    $..book[?(@.isbn)] All books with an ISBN number
    $.store.book[?(@.price < 10)] All books in store cheaper than 10
    $..book[?(@.price <= $['expensive'])] All books in store that are not "expensive"
    $..book[?(@.author =~ /.*REES/i)] All books matching regex (ignore case)
    $..* Give me every thing
    $..book.length() The number of books

     

    3、登录请求处理完之后,我们就可以处理后面压测接口的http请求了,下面这就是一个普通的post请求了。参数中传入了前面获取到的accesstoken。

     4、之后就是我们常规压测时,需要添加的一些组件。比如响应断言,聚合报告,Summary Report等。

    上面的都做完后,就可以开始压测了。

     【原文归作者所有,欢迎转载,但是保留版权,并且转载时,需要注明出处

  • 相关阅读:
    mysql删除重复数据
    Spring缓存注解
    Spring事物不回滚
    java.net.MalformedURLException: no protocol: www.baidu.com
    shred:减少删除文件的还原度
    rm命令防止误操作
    软链接:要根据软链接来写路径
    linux与windows文本文件间的转换:针对回车换行
    cat:文本编辑工具
    算术运算;赋值
  • 原文地址:https://www.cnblogs.com/laoqing/p/9350416.html
Copyright © 2011-2022 走看看