Gatling入門(二)IDEA+Maven編寫指令碼

weixin_34194087發表於2017-05-11

1. 用scala-archetype-simple新建一個maven專案

3134019-16f68b169d5d6a8c.png
IDEA中建立maven專案

2. 修改pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>demo.test</groupId>
  <artifactId>gatling-performance</artifactId>
  <version>1.0.0-SNAPSHOT</version>
  <name>GatlingMaven</name>

  <properties>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
    <encoding>UTF-8</encoding>
    <scala.version>2.11.11</scala.version>
    <gatling.version>2.1.7</gatling.version>
    <gatling-plugin.version>2.1.7</gatling-plugin.version>
  </properties>

  <dependencies>
    <dependency>
      <groupId>org.scala-lang</groupId>
      <artifactId>scala-library</artifactId>
      <version>${scala.version}</version>
    </dependency>

    <dependency>
      <groupId>io.gatling.highcharts</groupId>
      <artifactId>gatling-charts-highcharts</artifactId>
      <version>${gatling.version}</version>
      <scope>test</scope>
    </dependency>
  </dependencies>

  <build>
    <sourceDirectory>src/main/scala</sourceDirectory>
    <testSourceDirectory>src/test/scala</testSourceDirectory>
    <plugins>
      <!-- Gatling Maven plugin that runs the load-simulation. -->
      <plugin>
        <groupId>io.gatling</groupId>
        <artifactId>gatling-maven-plugin</artifactId>
        <version>${gatling-plugin.version}</version>
        <configuration>
          <simulationClass>gatling.demo.HttpSimulation1</simulationClass>
        </configuration>
      </plugin>
    </plugins>
  </build>
</project>

3. 編寫指令碼

package demo.test
import io.gatling.core.Predef._
import io.gatling.http.Predef._
/**
  * Example Gatling load test that sends two HTTP requests to the same URL.
  */
class HttpSimulation1 extends Simulation {
  val theHttpProtocolBuilder = http
    .baseURL("http://computer-database.gatling.io")
  val theScenarioBuilder = scenario("Scenario1")
    .exec(
      http("myRequest1").get("/")
    )
  setUp(
    theScenarioBuilder.inject(atOnceUsers(1))
  ).protocols(theHttpProtocolBuilder)
}

4. 命令列執行 mvn gatling:execute

3134019-bb0f1884d0a77f70.png
命令列執行

gatling的版本維持在2.1.7,我升級到2.2.4會報錯。


3134019-6594f249bf0e516d.png
gatling2.2.4報錯

解決:需要在POM中,新增scala-reflect的依賴

    <dependency>
      <groupId>org.scala-lang</groupId>
      <artifactId>scala-reflect</artifactId>
      <version>${scala.version}</version>
    </dependency>

另外,pom中如果使用scala-2.12.2版本,也會報錯。stackoverflow上說,是library的版本不匹配,一直不知道如何解決,留著坑待日後解決吧。。。

5. 檢視執行結果

3134019-89b262ebbfc634be.png
執行結果

https://www.ivankrizsan.se/2016/03/27/creating-a-scala-project-with-maven-dependency-management-for-gatling-testing-in-intellij-idea/
http://stackoverflow.com/questions/41118777/scala-2-12-1-classnotfoundexception-productclass

相關文章