1

我正在使用 TestNG 运行测试,但我想动态限制测试服务器上的负载。为此,我想在运行时验证我每秒向服务器发送的请求不超过 X 个,如果我在上面,则让线程进入睡眠状态。

我的问题是:如何在运行时获取线程数?

我正在调查 ITestContext 但它似乎不存在。

4

2 回答 2

3

Within your running TestNG tagged test, Thread.activeCount() gives you the number of threads just in your current TestNG group. If your threadPoolSize is set to 1, this number will be 1. If your threadPoolSize is greater than 1, this number will be threadPoolSize + 1, because there is also a TestNG manager thread in addition to the pool size. For threads that you create on the fly, you need to construct them passing in your TestNG ThreadGroup. Example:

    Thread aNewWorkerThread = new Thread( Thread.currentThread().getThreadGroup(), yourRunnableThingHere ); 

Those threads, when active, should then be reflected in your Thread.activeCount() return value.

于 2018-04-03T21:24:28.880 回答
0

您可以注入 TestNg 的“IInvokedMethod 方法”,然后使用以下代码

 int count = method.getTestMethod().getXmlTest().getThreadCount();
于 2018-03-06T05:04:35.880 回答