php企业网站开发深圳app设计
web/
2025/10/1 2:38:07/
文章来源:
php企业网站开发,深圳app设计,网站域名查询工具,百度指数免费查询入口在这篇文章中#xff0c;我将解释如何使用Gatling项目为您的JAX-RS Java EE端点编写压力测试#xff0c;以及如何将它们与Gradle和Jenkins Pipeline集成#xff0c;因此#xff0c;除了进行简单的压力测试外#xff0c;您还可以使用以下方法#xff1a; 连续的压力测试我将解释如何使用Gatling项目为您的JAX-RS Java EE端点编写压力测试以及如何将它们与Gradle和Jenkins Pipeline集成因此除了进行简单的压力测试外您还可以使用以下方法 连续的压力测试其中每个提交都可能自动触发此类测试并提供自动断言和每个执行的更重要的图形反馈因此您可以监视应用程序中性能的变化。 首先要开发的是JAX-RS JavaEE服务 Path(/planet)
Singleton
Lock(LockType.READ)
public class PlanetResources {InjectSwapiGateway swapiGateway;InjectPlanetService planetService;InjectAverageFormatterDecimalFormat averageFormatter;GETPath(/orbital/average)Produces(MediaType.TEXT_PLAIN)Asynchronouspublic void calculateAverageOfOrbitalPeriod(Suspended final AsyncResponse response) {// Timeout controlresponse.setTimeoutHandler(asyncResponse - asyncResponse.resume(Response.status(Response.Status.SERVICE_UNAVAILABLE).entity(TIME OUT !).build()));response.setTimeout(30, TimeUnit.SECONDS);try {// SwapiGateway is an interface to swapi.co (Star Wars API)JsonObject planets swapiGateway.getAllPlanets();final JsonArray results planets.getJsonArray(results);// Make some calculations with the result retrieved from swapi.codouble average planetService.calculateAverageOfOrbitalPeriod(results);final Response averageResponse Response.ok(averageFormatter.format(average)).build();response.resume(averageResponse);} catch(Throwable e) {response.resume(e);}}
} 没什么特别的这是一个异步的JAX-RS端点它连接到swapi.co网站检索“星球大战”行星的所有信息计算出轨道周期的平均值最后以文本形式返回。 为了简单起见我不会向您展示所有其他类但是它们非常简单在文章结尾我将为您提供github存储库。 该应用程序打包在war文件中并部署到应用程序服务器中。 在这种情况下将部署到官方Apache TomEE Docker映像内部署的Apache TomEE 7 。 下一步是使用Gatling依赖项配置Gradle构建脚本。 由于Gatling是用Scala编写的因此您需要使用Scala插件。 apply plugin: java
apply plugin: scaladef gatlingVersion 2.1.7dependencies {compile org.scala-lang:scala-library:2.11.7testCompile io.gatling:gatling-app:${gatlingVersion}testCompile io.gatling.highcharts:gatling-charts-highcharts:${gatlingVersion}
} 之后是时候编写我们的第一个压力测试了。 重要的是要注意为加特林编写压力测试正在使用提供的DSL编写Scala类。 即使对于从未看过Scala的人来说如何使用它也非常直观。 因此创建一个名为src / test / scala的目录并创建一个具有下一个内容的名为AverageOrbitalPeriodSimulation.scala的新类 package org.starwarsimport io.gatling.core.Predef._
import io.gatling.http.Predef._
import scala.concurrent.duration._
import scala.util.Properties// Extends from Simulation
class AverageOrbitalPeriodSimulation extends Simulation {// Gets the base URL where our service is running from environment/system propertyval LOCATION_PROPERTY starwars_planets_url;val location Properties.envOrElse(LOCATION_PROPERTY, Properties.propOrElse(LOCATION_PROPERTY, http://localhost:8080/))// configures the base URLval conf http.baseURL(location)// defines the scenario to run, which in this case is a GET to endpoint defined in JAX-RS serviceval scn scenario(calculate average orbital period).exec(http(get average orbital period).get(rest/planet/orbital/average)).pause(1)// instead of simulating 10 users at once, it adds gradullay the 10 users during 3 seconds// asserts that there is no failing requests and that at max each request takes less than 3 secondssetUp(scn.inject(rampUsers(10).over(3 seconds))).protocols(conf).assertions(global.successfulRequests.percent.is(100), global.responseTime.max.lessThan(3000))
} 每个模拟都必须扩展模拟对象。 此模拟从starwars_planets_url环境或系统属性中获取服务的基本URL创建指向JAX-RS中定义的端点的方案最后在3秒钟内它将逐步添加用户直到同时运行10个用户。 仅在所有请求在3秒内成功通过后测试才能通过。 现在我们需要运行此测试。 您会注意到这不是JUnit测试因此您无法执行Run As JUnit测试。 您需要做的是使用Gatling提供的可运行类该类要求您将模拟类作为参数传递。 使用Gradle确实很容易做到。 task runLoadTest(type: JavaExec) {// before runnign the task we need to compile the testsdependsOn testClassesdescription Stress Test Calculating Orbital Periodclasspath sourceSets.main.runtimeClasspath sourceSets.test.runtimeClasspath// if starwars_planets_url is not provided we add the DOCKER_HOST one automaticallydef starwarsUrl;if (!System.env.containsKey(starwars_planets_url) !System.properties.containsKey(starwars_planets_url)) {if (System.env.containsKey(DOCKER_HOST)) {starwarsUrl System.env.DOCKER_HOST.replace(tcp, http).replace(2376, 9090) /starwars/} else {starwarsUrl http://localhost:8080/starwars/}}jvmArgs [ -Dgatling.core.directory.binaries${sourceSets.test.output.classesDir.toString()} ]// Means that the url has been calculated here and we set itif (starwarsUrl ! null) {environment[starwars_planets_url] starwarsUrl}// Gatling applicationmain io.gatling.app.Gatling// Specify the simulation to run and outputargs [--simulation, org.starwars.AverageOrbitalPeriodSimulation,--results-folder, ${buildDir}/reports/gatling-results,--binaries-folder, sourceSets.test.output.classesDir.toString(),--output-name, averageorbitalperiodsimulation,--bodies-folder, sourceSets.test.resources.srcDirs.toList().first().toString() /gatling/bodies,]
}// when running test task we want to execute the Gatling test
test.dependsOn runLoadTest 我们正在定义JavaExec类型的Gradle任务因为我们想要的是运行一个可运行的类。 然后通过自动检测到是否未设置starwars_planets_url我们将该测试运行到已安装Docker的计算机上因此可能使开发人员的工作变得更加轻松。 最后如果需要我们将覆盖环境变量我们为可运行类设置必需的属性并配置Gradle在每次执行测试任务./gradlew test时执行此任务。 如果运行它您可能会看到来自Gatling的一些输出消息以及所有类似以下的消息请打开以下文件 /Users/…./stress-test/build/reports/gatlingresults / averageorbitalperiodsimulation-1459413095563 / index。 html 这是您获取报告的地方。 请注意在目录末尾附加了一个随机数这很重要因为稍后我们将要看到。 该报告可能如下所示 目前我们已将Gatling与Gradle集成在一起但是这里缺少一个片段它在方程式上添加了连续部分。 为了添加连续的压力测试我们将使用Jenkins和Jenkins Pipeline作为CI服务器因此对于每个提交都执行压力测试 其他任务例如编译运行单元集成测试或代码质量门。 过去 Jenkins作业是使用Web UI配置的要求用户手动创建作业填写作业的详细信息并通过Web浏览器创建管道。 同样这也使得将作业的配置与正在构建的实际代码分开。 随着Jenkins Pipeline插件的引入。 该插件是Groovy DSL可让您在文件中实施整个构建过程并将其与代码一起存储。 Jenkins 2.0默认带有此插件但是如果您使用的是Jenkins 1.X则可以将其安装为其他任何插件 https://wiki.jenkins-ci.org/display/JENKINS/PipelinePlugin 因此现在我们可以开始对发布插件进行编码了但是出于本文的目的仅涉及压力部分。 您需要在项目的根目录上创建一个名为Jenkinsfile的文件名称不是强制性的但实际上是名称在本例中为下一个内容 stage Compile And Unit Teststage Code Qualitystage Integration Teststage Acceptance Test// defines an stage for info purposes
stage Stress Testdef dockerHost ...
//defines a node to run the stage
node {// get source code from location where Jenkinsfile (this) is located.// you could use stash/unstash to get sources from previous stages instead of getting from SCMcheckout scm// defines the environment variable for stress testwithEnv([starwars_planets_urlhttp://${dockerHost}:9090/starwars/]) {// executes shell scriptsh ./gradlew test}} 在这种情况下我们定义了一个新阶段称为压力测试。 阶段步骤仅用作参考将用于记录目的。 接下来定义一个节点。 节点是执行代码的Jenkins执行程序。 在此节点内从放置Jenkinsfile的相同位置检出源代码设置一个新的环境变量该变量指向应用程序的部署位置最后是执行Gradle测试任务的shell步骤。 Jenkins的最后一步是创建类型为Pipeline的新作业并设置Jenkinsfile的位置。 因此转到“ 詹金斯”“新项目”“管道”并为作业命名。 然后您只需要转到“ 管道”部分并配置用于存储项目的SCM存储库。 然后如果您已经正确配置了Jenkins和您的SCM服务器的挂钩那么将在每次提交时执行此作业因此您的压力测试将连续运行。 当然您可能已经注意到压力测试已执行但Jenkins没有发布任何报告因此您无法查看或比较不同执行的结果。 因此您可以使用publishHtml插件将生成的报告存储在Jenkins中 。 如果尚未安装该插件则需要与其他任何Jenkins插件一样安装。 PublishHtml插件使我们可以将构建工具生成的一些html文件发布到Jenkins以便用户可以使用也可以按内部版本号进行分类。 您需要配置要发布的文件目录的位置在这里我们找到第一个问题您还记得盖特林生成带有随机数的目录吗 因此我们需要首先解决此问题。 您可以采用不同的策略但是最简单的方法是在测试后将目录重命名为已知的静态名称。 打开Gradle构建文件并添加下一个内容。 task(renameGatlingDirectory) {// find the directorydef report {file - file.isDirectory() file.getName().startsWith(averageorbitalperiodsimulation)}def reportDirectory new File(${buildDir}/reports/gatling-results).listFiles().toList().findAll(report).sort().last()// rename to a known directory// should always work because in CI it comes from a clean executionreportDirectory.renameTo(${buildDir}/reports/gatling-results/averageorbitalperiodsimulation)
}// it is run after test phase
test.finalizedBy renameGatlingDirectory 我们正在创建一个在测试任务结束时执行的新任务该任务将最后创建的目录重命名为averageorbitalperiodsimulation 。 最后一步是在Jenkinsfile中的shell调用之后的下一个调用中添加 publishHTML(target: [reportDir:stress-test/build/reports/gatling-results/averageorbitalperiodsimulation, reportFiles: index.html, reportName: Gatling report, keepAll: true]) 之后您可能会在作业页面中看到一个指向报告的链接。 就是如此多亏了Gradle和Jenkins您可以轻松地实施持续的压力测试策略而只需使用所有开发人员都讲的语言代码即可。 我们不断学习 亚历克斯 翻译自: https://www.javacodegeeks.com/2016/04/continuous-stress-testing-jax-rs-javaee-applications-gatling-gradle-jenkins-pipeline.html
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/web/84789.shtml
如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!