51Testing软件测试论坛

 找回密码
 (注-册)加入51Testing

QQ登录

只需一步,快速开始

微信登录,快人一步

查看: 796|回复: 0
打印 上一主题 下一主题

gatling新手必备资料

[复制链接]
  • TA的每日心情
    无聊
    2024-3-7 09:16
  • 签到天数: 43 天

    连续签到: 2 天

    [LV.5]测试团长

    跳转到指定楼层
    1#
    发表于 2022-8-11 14:59:06 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
    一、搭建环境与下载
    gatling是基于scala环境,但是scala是基于java的,需要二种环境,
    1.java环境
    地址:https://www.runoob.com/java/java-environment-setup.html
    2.scala环境
    地址:https://www.scala-lang.org/download/


    直接下载安装,不需要配置环境变量。验证scala -version

    3.下载gatling
    地址:https://gatling.io/download/


    文档:https://gatling.io/documentation/
    入门:https://gatling.io/docs/current/quickstart/

    1. <font size="3" color="#000000">-gatling-charts-highcharts-bundle-3.0.3-bundle
    2. ---- bin 执行程序
    3. ---- ---- gatling.bat  windows 环境运行
    4. ---- ---- gatling.sh liunx 环境运行
    5. ---- ---- recorder.bat  windows 环境运行
    6. ---- ---- recorder.sh  liunx 环境运行
    7. ---- conf 配置信息
    8. ---- lib 依赖包
    9. ---- user-files 用户文件
    10. ---- ---- bodies 用来存放请求的body数据
    11. ---- ---- data 数据目录,csv参数文件存放在这里
    12. ---- ---- simulations 测试脚本
    13. #####Run:
    14. > ```bash
    15. > #Linux/Unix执行脚本
    16. > >$GATLING_HOME/bin/gatling.sh
    17. > #Windows执行脚本
    18. > >%GATLING_HOME%\bin\gatling.bat
    19. </font>
    复制代码
    使用bat脚本运行下是此画面。


    二、springboot与gatling结合进行压测
    1.需要在IDEA安装scala插件
    2.安装scala SDK 由于步骤一 我们已经安装完毕,不需要安装,只需要重新配置。


    3.环境搭建
    pom.xml

    1. <font size="3" color="#000000"><?xml version="1.0" encoding="UTF-8"?>
    2. <project xmlns="http://maven.apache.org/POM/4.0.0"
    3.          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    4.          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    5.     <modelVersion>4.0.0</modelVersion>

    6.     <groupId>com.anoyi</groupId>
    7.     <artifactId>gatling-test</artifactId>
    8.     <version>1.0-SNAPSHOT</version>

    9.     <parent>
    10.         <groupId>org.springframework.boot</groupId>
    11.         <artifactId>spring-boot-starter-parent</artifactId>
    12.         <version>1.5.9.RELEASE</version>
    13.     </parent>

    14.     <properties>
    15.         <lombok.version>1.16.18</lombok.version>
    16.         <mariadb-client.version>2.1.2</mariadb-client.version>
    17.         <hikariCP.version>2.7.1</hikariCP.version>
    18.         <postgresql.version>42.1.4</postgresql.version>
    19.         <gatling-plugin.version>2.2.4</gatling-plugin.version>
    20.     </properties>

    21.     <dependencies>
    22.         <!-- web 依赖 -->
    23.         <dependency>
    24.             <groupId>org.springframework.boot</groupId>
    25.             <artifactId>spring-boot-starter-web</artifactId>
    26.             <exclusions>
    27.                 <exclusion>
    28.                     <groupId>org.springframework.boot</groupId>
    29.                     <artifactId>spring-boot-starter-tomcat</artifactId>
    30.                 </exclusion>
    31.             </exclusions>
    32.         </dependency>

    33.         <!-- 容器 undertow -->
    34.         <dependency>
    35.             <groupId>org.springframework.boot</groupId>
    36.             <artifactId>spring-boot-starter-undertow</artifactId>
    37.         </dependency>

    38.         <!-- 移除 Tomcat 连接池,使用 HikariCP  -->
    39.         <dependency>
    40.             <groupId>org.springframework.boot</groupId>
    41.             <artifactId>spring-boot-starter-data-jpa</artifactId>
    42.             <exclusions>
    43.                 <exclusion>
    44.                     <groupId>org.apache.tomcat</groupId>
    45.                     <artifactId>tomcat-jdbc</artifactId>
    46.                 </exclusion>
    47.             </exclusions>
    48.         </dependency>

    49.         <!-- 移除 tomcat-jdbc, Spring Boot 将会自动使用 HikariCP -->
    50.         <dependency>
    51.             <groupId>com.zaxxer</groupId>
    52.             <artifactId>HikariCP</artifactId>
    53.             <version>${hikariCP.version}</version>
    54.         </dependency>

    55.         <!-- 简化 lombok -->
    56.         <dependency>
    57.             <groupId>org.projectlombok</groupId>
    58.             <artifactId>lombok</artifactId>
    59.             <version>${lombok.version}</version>
    60.         </dependency>

    61.         <!-- 数据库驱动 postgresql -->
    62.         <dependency>
    63.             <groupId>org.postgresql</groupId>
    64.             <artifactId>postgresql</artifactId>
    65.             <version>${postgresql.version}</version>
    66.         </dependency>

    67.         <!-- 性能测试 Gatling -->
    68.         <dependency>
    69.             <groupId>io.gatling.highcharts</groupId>
    70.             <artifactId>gatling-charts-highcharts</artifactId>
    71.             <version>2.3.0</version>
    72.         </dependency>

    73.     </dependencies>

    74.     <build>
    75.         <plugins>
    76.             <plugin>
    77.                 <groupId>io.gatling</groupId>
    78.                 <artifactId>gatling-maven-plugin</artifactId>
    79.                 <version>${gatling-plugin.version}</version>
    80.                 <configuration>
    81.                     <!-- 测试脚本 -->
    82.                     <simulationClass>com.anoyi.test.ApiGatlingSimulationTest</simulationClass>
    83.                     <!-- 结果输出地址 -->
    84.                     <resultsFolder>/Users/admin/code/gatling</resultsFolder>
    85.                 </configuration>
    86.             </plugin>
    87.         </plugins>
    88.     </build>

    89. </project>
    90. </font>
    复制代码
    4.编写测试脚本
    每一个 Gatling 测试都要继承 Simulation 类,在里面你可以使用Gatling Scala DSL 来声明一个场景列表。这里的目标是运行 30 个客户端,同时发送 1000 次请求
    项目目录结构:


    1. <font size="3" color="#000000">package com.anoyi.test

    2. import io.gatling.core.scenario.Simulation
    3. import io.gatling.core.Predef._
    4. import io.gatling.http.Predef._
    5. import scala.concurrent.duration.FiniteDuration
    6. import scala.concurrent.duration.Duration
    7. import java.util.concurrent.TimeUnit

    8. class ApiGatlingSimulationTest extends Simulation {

    9.   val scn = scenario("AddAndFindPersons").repeat(1000, "n") {
    10.     exec(
    11.       http("AddPerson-API")
    12.         .post("http://localhost:8080/persons")
    13.         .header("Content-Type", "application/json")
    14.         .body(StringBody("""{"firstName":"John${n}","lastName":"Smith${n}","birthDate":"1980-01-01", "address": {"country":"pl","city":"Warsaw","street":"Test${n}","postalCode":"02-200","houseNo":${n}}}"""))
    15.         .check(status.is(200))
    16.     ).pause(Duration.apply(5, TimeUnit.MILLISECONDS))
    17.   }.repeat(1000, "n") {
    18.     exec(
    19.       http("GetPerson-API")
    20.         .get("http://localhost:8080/persons/${n}")
    21.         .check(status.is(200))
    22.     )
    23.   }
    24. setUp(scn.inject(atOnceUsers(30))).maxDuration(FiniteDuration.apply(10, "minutes"))

    25. }
    26. </font>
    复制代码
    转自:http://t.csdn.cn/70vgv

    本帖子中包含更多资源

    您需要 登录 才可以下载或查看,没有帐号?(注-册)加入51Testing

    x
    分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
    收藏收藏
    回复

    使用道具 举报

    本版积分规则

    关闭

    站长推荐上一条 /1 下一条

    小黑屋|手机版|Archiver|51Testing软件测试网 ( 沪ICP备05003035号 关于我们

    GMT+8, 2024-5-2 10:09 , Processed in 0.065765 second(s), 24 queries .

    Powered by Discuz! X3.2

    © 2001-2024 Comsenz Inc.

    快速回复 返回顶部 返回列表