出现错误:在运行 jar 进行性能测试时找不到或加载主类 io.gatling.app.Gatling,但是当我在 intellij 上运行相同的代码时工作正常 下面是执行代码的示例步骤
步骤:1 使用 Gatling 创建了 PerformanceTest.jar 的 mvn 包。
SampleTestPlayer.scala
package com.performance.SampleTest
import com.performance.config.ScenarioConfiguration
import com.performance.{Player, Feeders}
import io.gatling.core.Predef._
import io.gatling.core.structure.{ChainBuilder, ScenarioBuilder}
import io.gatling.http.Predef._
import scala.concurrent.duration._
import scala.util.Random
class SampleTestPlayer extends SomeClass {
val config: ScenarioConfiguration.type = ScenarioConfiguration
gdmServerEndpoint = config.gdmServerUrl
Name = config.Name
val regexGsdViewArea = "VA~(?:[\\d\\|]+?)+"
def play(): ChainBuilder = {
exec(http(TEST)
.post(ServerEndpoint)
.body(StringBody(session => bodyXml(session, config.Name,
prepareRequest(session, config.Name, 10, 5))))
.check(status.is(200)
, regex("<SUCCESS>true</SUCCESS>").exists
, regex(errorMsgId).notExists
, regex(errorProtocol).optional.saveAs(errorProtocol)
)
)
.exec(checkForErrors)
.pause(requestsInterval)
}
val scn: ScenarioBuilder = scenario("SampleTest_Performance_Tests")
.feed(Feeders.operator1UsersFeeder)
.exec({ session => session.set("recoverAfterRequest", false)})
.repeat(config.numPlays) {
play()
}
if (config.model == "closed") {
setUp(
scn.inject(
rampUsers(config.numUsers) during (config.rampUpDuration)
).protocols(buildHttpConf(config.baseUrl))
)
}
if (config.model == "open") {
setUp(
scn.inject(
nothingFor(4 seconds), // 1
atOnceUsers(10), // 2
rampUsers(5) during (20 seconds), // 3
constantUsersPerSec(2) during (15 seconds), // 4
constantUsersPerSec(2) during (15 seconds) randomized, // 5
rampUsersPerSec(2) to 5 during (1 minutes), // 6
rampUsersPerSec(2) to 5 during (1 minutes) randomized, // 7
heavisideUsers(100) during (20 seconds) // 8
).protocols(buildHttpConf(config.baseUrl))
)
}
}constantUsersPerSec(2) during (15 seconds) randomized, // 5
rampUsersPerSec(2) to 5 during (1 minutes), // 6
rampUsersPerSec(2) to 5 during (1 minutes) randomized, // 7
heavisideUsers(100) during (20 seconds) // 8
).protocols(buildHttpConf(config.baseUrl))
)
}
}
SampleTestRunner.scala
package com.performance.SampleTest
import io.gatling.app.Gatling
import io.gatling.core.config.GatlingPropertiesBuilder
object SampleTestRunner {
def main(args: Array[String]): Unit = {
System.setProperty("baseUrl", "https://SomeUrl")
System.setProperty("Name", "SampleTest")
System.setProperty("requestsInterval", "1000")
val simulationClass = classOf[SampleTestPlayer].getName
val props = new GatlingPropertiesBuilder
props.simulationClass(simulationClass)
Gatling.fromMap(props.build)
}
}
步骤:2 创建一个launch.sh 文件来运行这些测试。
启动.sh
#!/bin/sh
USER_ARGS="-Dsomething=$1"
COMPILATION_CLASSPATH=`find -L . -maxdepth 1 -name "*.jar" -type f -exec printf :{} ';'`
JAVA_OPTS="-server -XX:+UseThreadPriorities -XX:ThreadPriorityPolicy=42 -Xms512M -Xmx2048M -XX:+HeapDumpOnOutOfMemoryError -XX:+AggressiveOpts -XX:+OptimizeStringConcat -XX:+UseFastAccessorMethods -XX:+UseParNewGC -XX:+UseConcMarkSweepGC -XX:+CMSParallelRemarkEnabled -Djava.net.preferIPv4Stack=true -Djava.net.preferIPv6Addresses=false ${JAVA_OPTS} -DName=sampletest -DbaseUrl=https://SomeUrl -DnumPlays=100 -Dmodel=closed -DnumUsers=1000 -DrequestsInterval=10000 -DrampUpDuration=3000"
java $JAVA_OPTS $USER_ARGS -cp $COMPILATION_CLASSPATH io.gatling.app.Gatling -s com.performance.sampletest.SampleTestPlayer