我想使用像Cucumber JVM这样的东西来驱动为Gatling编写的性能测试。
理想情况下,Cucumber功能会以某种方式动态构建场景 - 可能会重复使用类似于“高级教程”中描述的方法的预定义链对象,例如
val scn = scenario("Scenario Name").exec(Search.search("foo"), Browse.browse, Edit.edit("foo", "bar")
我已经看过Maven插件如何执行脚本了,我也看到了使用App特性的提及但我找不到后面的任何文档而且让我觉得别人会想要在......之前这样做。
任何人都可以指向(Gatling noob)某些文档的方向或如何实现此示例代码吗?
编辑20150515
所以要解释一下:
我创建了一个特性,用于构建一系列由Cucumber步骤触发的ChainBuilders:
trait GatlingDsl extends ScalaDsl with EN {
private val gatlingActions = new ArrayBuffer[GatlingBehaviour]
def withGatling(action: GatlingBehaviour): Unit = {
gatlingActions += action
}
}
GatlingBehaviour看起来像是:
object Google {
class Home extends GatlingBehaviour {
def execute: ChainBuilder =
exec(http("Google Home")
.get("/")
)
}
class Search extends GatlingBehaviour {...}
class FindResult extends GatlingBehaviour {...}
}
在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))
}
}
我的想法是,我可以通过以下方式执行整个行动序列:
val scn = Scenario(cucumberScenario).exec(gatlingActions)
setup(scn.inject(atOnceUsers(1)).protocols(httpConf))
然后检查报告或在测试失败时捕获异常,例如响应时间太长了。
似乎无论我如何使用'exec'方法,它都会尝试立即在那里执行它,然后不等待场景。
另外我不知道这是否是最好的方法,我们想为我们的Gatling测试构建一些可重复使用的块,这些块可以通过Cucumber的Given / When / Then样式构建。是否有更好的或已经存在的方法?
答案 0 :(得分:3)
Sadly, it's not currently feasible to have Gatling directly start a Simulation instance.
Not that's it's not technically feasible, but you're just the first person to try to do this. Currently, Gatling is usually in charge of compiling and can only be passed the name of the class to load, not an instance itself.
You can maybe start by forking [Launcher,27487/stderr] Error during info_callback
Traceback (most recent call last):
File "/opt/webapps/link_crawler/lib/python2.7/site-packages/twisted/protocols/tls.py", line 415, in dataReceived
self._write(bytes)
File "/opt/webapps/link_crawler/lib/python2.7/site-packages/twisted/protocols/tls.py", line 554, in _write
sent = self._tlsConnection.send(toSend)
File "/opt/webapps/link_crawler/lib/python2.7/site-packages/OpenSSL/SSL.py", line 1270, in send
result = _lib.SSL_write(self._ssl, buf, len(buf))
File "/opt/webapps/link_crawler/lib/python2.7/site-packages/OpenSSL/SSL.py", line 926, in wrapper
callback(Connection._reverse_mapping[ssl], where, return_code)
--- <exception caught here> ---
File "/opt/webapps/link_crawler/lib/python2.7/site-packages/twisted/internet/_sslverify.py", line 1055, in infoCallback
return wrapped(connection, where, ret)
File "/opt/webapps/link_crawler/lib/python2.7/site-packages/twisted/internet/_sslverify.py", line 1157, in _identityVerifyingInfoCallback
transport = connection.get_app_data()
File "/opt/webapps/link_crawler/lib/python2.7/site-packages/OpenSSL/SSL.py", line 1589, in get_app_data
return self._app_data
File "/opt/webapps/link_crawler/lib/python2.7/site-packages/OpenSSL/SSL.py", line 1148, in __getattr__
return getattr(self._socket, name)
exceptions.AttributeError: 'NoneType' object has no attribute '_app_data'
and io.gatling.app.Gatling
, and then provide a PR to support this new behavior. The former is the main entry point, and the latter the one can instanciate and run the simulation.
答案 1 :(得分:3)
还有另一种选择。 Gatling是用Java和Scala编写的,因此您可以直接调用Gatling.main并将其传递给您运行所需的Gatling Simulation所需的参数。问题是,main显式调用System.exit,因此您还必须使用自定义安全管理器来阻止它实际退出。 你需要知道两件事:
运行模拟的代码:
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;
}
}
问题是在运行之后将所需信息从模拟中获取。