Testing 以编程方式执行Gatling测试

Testing 以编程方式执行Gatling测试,testing,performance-testing,cucumber-jvm,gatling,Testing,Performance Testing,Cucumber Jvm,Gatling,我想使用类似cumberjvm的东西来驱动为Gatling编写的性能测试 理想情况下,Cucumber特性将以某种方式动态构建场景-可能重用预定义的链对象,类似于“高级教程”中描述的方法,例如 我已经看过Maven插件是如何执行脚本的,我也看到有人提到使用一个App trait,但我找不到以后的任何文档,这让我觉得以前会有人想这么做 有人能指出(一个加特林noob)如何实现这一点的一些文档或示例代码的方向吗 编辑20150515 再解释一下: 我创造了一种特质,目的是建立一系列,我认为,由黄瓜步

我想使用类似cumberjvm的东西来驱动为Gatling编写的性能测试

理想情况下,Cucumber特性将以某种方式动态构建场景-可能重用预定义的链对象,类似于“高级教程”中描述的方法,例如

我已经看过Maven插件是如何执行脚本的,我也看到有人提到使用一个App trait,但我找不到以后的任何文档,这让我觉得以前会有人想这么做

有人能指出(一个加特林noob)如何实现这一点的一些文档或示例代码的方向吗

编辑20150515

再解释一下:

我创造了一种特质,目的是建立一系列,我认为,由黄瓜步骤触发的链构建器:

trait GatlingDsl extends ScalaDsl with EN {

  private val gatlingActions = new ArrayBuffer[GatlingBehaviour]

  def withGatling(action: GatlingBehaviour): Unit = {
    gatlingActions += action
  }
}
GatlingBehavior看起来像:

object Google {

  class Home extends GatlingBehaviour {
    def execute: ChainBuilder =
      exec(http("Google Home")
        .get("/")
      )
  }

  class Search extends GatlingBehaviour {...}

  class FindResult extends GatlingBehaviour {...}
}
val scn = Scenario(cucumberScenario).exec(gatlingActions)
setup(scn.inject(atOnceUsers(1)).protocols(httpConf))
在StepDef类中:

class GoogleStepDefinitions extends GatlingDsl {

  Given( """^the Google search page is displayed$""") { () =>
    println("Loading www.google.com")
    withGatling(Home())
  }

  When( """^I search for the term "(.*)"$""") { (searchTerm: String) =>
    println("Searching for '" + searchTerm + "'...")
    withGatling(Search(searchTerm))
  }

  Then( """^"(.*)" appears in the search results$""") { (expectedResult: String) =>
    println("Found " + expectedResult)
    withGatling(FindResult(expectedResult))
  }
}
我的想法是,我可以通过以下方式执行整个动作序列:

object Google {

  class Home extends GatlingBehaviour {
    def execute: ChainBuilder =
      exec(http("Google Home")
        .get("/")
      )
  }

  class Search extends GatlingBehaviour {...}

  class FindResult extends GatlingBehaviour {...}
}
val scn = Scenario(cucumberScenario).exec(gatlingActions)
setup(scn.inject(atOnceUsers(1)).protocols(httpConf))
然后检查报告,或在测试失败时捕获异常,例如响应时间过长

似乎无论我如何使用“exec”方法,它都会尝试立即在那里执行,而不是等待场景


另外,我不知道这是否是最好的方法,我们想为我们的Gatling测试构建一些可重用的块,这些块可以通过cumber的Given/When/Then样式构建。有更好的或已经存在的方法吗?

遗憾的是,让Gatling直接启动一个模拟实例目前是不可行的

不是这样,技术上不可行,但你只是第一个尝试这样做的人。 目前,Gatling通常负责编译,只能传递要加载的类的名称,而不能传递实例本身


您可以从分叉
io.gatling.app.gatling
io.gatling.core.runner.runner
开始,然后提供一个PR来支持这种新行为。前者是主要的切入点,而后者可以实例化并运行模拟。

我最近遇到了类似的情况,我不想用fork gatling。虽然这解决了我眼前的问题,但它只是部分解决了你想做的事情,但希望其他人会发现这很有用

还有一种选择。Gatling是用Java和Scala编写的,因此您可以直接调用Gatling.main,并将运行Gatling模拟所需的参数传递给它。问题是,main显式调用System.exit,因此您还必须使用自定义安全管理器来防止它实际退出。 你需要知道两件事:

  • 要运行的模拟的类(包含完整包) 示例:com.package.your.Simulation1
  • 编译二进制文件的路径
  • 运行模拟的代码如下:

    protected void fire(String gatlingGun, String binaries){
        SecurityManager sm = System.getSecurityManager();
        System.setSecurityManager(new GatlingSecurityManager());
        String[] args = {"--simulation", gatlingGun,
                "--results-folder", "gatling-results",
                "--binaries-folder", binaries};
        try {
            io.gatling.app.Gatling.main(args);
        }catch(SecurityException se){
            LOG.debug("gatling test finished.");
        }
        System.setSecurityManager(sm);
    }
    
    我使用的简单安全管理器:

    public class GatlingSecurityManager extends SecurityManager {
        @Override
        public void checkExit(int status){
            throw new SecurityException("Tried to exit.");
        }
        @Override
        public void checkPermission(Permission perm) {
            return;
        }
    }
    

    问题是在模拟运行后,从中获取您想要的信息。

    谢谢,我已经看到了,但它似乎加载了静态Gatling脚本,而没有传递动态生成的场景-我是否遗漏了什么?我也遇到了类似的情况@pilotg2您能提供详细的解决方案吗,我迫切需要gatling simulation以java代码@markliberhman的形式运行抱歉,我没有注意到我收到了一条消息,因为我没有注意到我已登录。您评论中链接中的问题似乎未被删除。如果不知道您的项目设置,我无法提供更详细的解决方案。但您所要做的就是调用fire方法,调用方式如下:fire(GatlingTestClass.getClass().getCannonicalName(),“build”)