Hi,
We have an api, which always expects unique userids. If a userid has already been used, it can't be used again or else the API will return 400. So we created an array of userId's, Now what we need is all the threads across all the processes to access userId's from this array only once. runNumber is not unique across the test, it's unique only for the thread. Is there a way to do this in nGrinder? Thanks, Apurv |
Administrator
|
Could you create a userid by using 'curNumber' in below example?
def curNumber; @BeforeThread public void beforeThread() { // get the number of processes per agent. int totalProcessCount = grinder.getProperties().getInt("grinder.processes", 1); // get the number of threads per process. int totalThreadCount = grinder.getProperties().getInt("grinder.threads", 1); // get the number of the agent currently running this thread (starting from 0). int agentNumber = grinder.agentNumber // get the number of processes currently running on this thread (starting at 0). int processNumber = grinder.processNumber // get current thread number. int threadNumber = grinder.threadNumber // create unique id for this thread. curNumber = (agentNumber * totalProcessCount * totalThreadCount) + (processNumber * totalThreadCount) + threadNumber; .... } @Test public doTest() { // use 'curNumber' for unique id in the overall test. } |
Does this work for anyone? for my side, it didn't work, kindly remind me if any place in my code wrong.
@RunWith(GrinderRunner) class TestRunner { public static GTest test public static HTTPRequest request public static NVPair[] headers = [] public static NVPair[] params = [] public static Cookie[] cookies = [] def curNumber; @BeforeProcess public static void beforeProcess() { HTTPPluginControl.getConnectionDefaults().timeout = 6000 test = new GTest(1, "www.google.com") request = new HTTPRequest() grinder.logger.info("before process."); } @BeforeThread public void beforeThread() { test.record(this, "test"); grinder.statistics.delayReports=true; grinder.logger.info("before thread."); // get the number of processes per agent. int totalProcessCount = grinder.getProperties().getInt("grinder.processes", 1); //grinder.logger.info("totalProcessCount: "+totalProcessCount); // get the number of threads per process. int totalThreadCount = grinder.getProperties().getInt("grinder.threads", 1); //grinder.logger.info("totalThreadCount: "+totalThreadCount); // get the number of the agent currently running this thread (starting from 0). int agentNumber = grinder.agentNumber; //grinder.logger.info("agentNumber: "+agentNumber); // get the number of processes currently running on this thread (starting at 0). int processNumber = grinder.processNumber; //grinder.logger.info("processNumber: "+processNumber); // get current thread number. int threadNumber = grinder.threadNumber; //grinder.logger.info("threadNumber: "+threadNumber); // create unique id for this thread. curNumber = (agentNumber * totalProcessCount * totalThreadCount) + (processNumber * totalThreadCount) + threadNumber; //grinder.logger.info("curNumber: "+curNumber); } @Before public void before() { request.setHeaders(headers); cookies.each { CookieModule.addCookie(it, HTTPPluginControl.getThreadHTTPClientContext()) }; //grinder.logger.info("before thread. init headers and cookies"); } @Test public void test(){ grinder.logger.info("----- curNumber: "+curNumber+" -----"); } } ================================================== Below is the output: 2020-05-18 17:51:35,145 INFO The Grinder version 3.9.1 2020-05-18 17:51:35,148 INFO Java(TM) SE Runtime Environment 1.8.0_102-b14: Java HotSpot(TM) 64-Bit Server VM (25.102-b14, mixed mode) on Linux amd64 3.10.0-229.el7.x86_64 2020-05-18 17:51:35,152 INFO time zone is CST (+0800) 2020-05-18 17:51:35,231 INFO worker process 0 of agent number 0 2020-05-18 17:51:35,249 INFO Instrumentation agents: byte code transforming instrumenter for Java; byte code transforming instrumenter for Java 2020-05-18 17:51:36,243 INFO registered plug-in net.grinder.plugin.http.HTTPPlugin 2020-05-18 17:51:36,258 INFO before process. 2020-05-18 17:51:36,260 INFO Running "TestRunner.groovy" using GroovyScriptEngine running with groovy version: 2.2.1 2020-05-18 17:51:36,332 INFO before thread. 2020-05-18 17:51:36,333 INFO before thread. 2020-05-18 17:51:36,333 INFO before thread. 2020-05-18 17:51:36,333 INFO before thread. 2020-05-18 17:51:36,333 INFO before thread. 2020-05-18 17:51:36,448 INFO starting, will do 10 runs 2020-05-18 17:51:36,448 INFO starting, will do 10 runs 2020-05-18 17:51:36,448 INFO starting, will do 10 runs 2020-05-18 17:51:36,449 INFO starting, will do 10 runs 2020-05-18 17:51:36,449 INFO starting, will do 10 runs 2020-05-18 17:51:36,449 INFO Start time is 1589795496449 ms since Epoch 2020-05-18 17:51:36,486 INFO ----- curNumber: 0 ----- 2020-05-18 17:51:36,486 INFO ----- curNumber: 0 ----- 2020-05-18 17:51:36,486 INFO ----- curNumber: 0 ----- 2020-05-18 17:51:36,486 INFO ----- curNumber: 0 ----- 2020-05-18 17:51:36,487 INFO ----- curNumber: 0 ----- 2020-05-18 17:51:36,488 INFO ----- curNumber: 0 ----- 2020-05-18 17:51:36,488 INFO ----- curNumber: 0 ----- 2020-05-18 17:51:36,488 INFO ----- curNumber: 0 ----- 2020-05-18 17:51:36,489 INFO ----- curNumber: 0 ----- 2020-05-18 17:51:36,489 INFO ----- curNumber: 0 ----- 2020-05-18 17:51:36,490 INFO ----- curNumber: 0 ----- 2020-05-18 17:51:36,490 INFO ----- curNumber: 0 ----- 2020-05-18 17:51:36,490 INFO ----- curNumber: 0 ----- 2020-05-18 17:51:36,491 INFO ----- curNumber: 0 ----- 2020-05-18 17:51:36,491 INFO ----- curNumber: 0 ----- 2020-05-18 17:51:36,491 INFO ----- curNumber: 0 ----- 2020-05-18 17:51:36,491 INFO ----- curNumber: 0 ----- 2020-05-18 17:51:36,492 INFO ----- curNumber: 0 ----- 2020-05-18 17:51:36,492 INFO ----- curNumber: 0 ----- 2020-05-18 17:51:36,492 INFO ----- curNumber: 0 ----- 2020-05-18 17:51:36,493 INFO ----- curNumber: 0 ----- 2020-05-18 17:51:36,493 INFO ----- curNumber: 0 ----- 2020-05-18 17:51:36,493 INFO ----- curNumber: 0 ----- 2020-05-18 17:51:36,493 INFO ----- curNumber: 0 ----- 2020-05-18 17:51:36,493 INFO ----- curNumber: 0 ----- 2020-05-18 17:51:36,493 INFO ----- curNumber: 0 ----- 2020-05-18 17:51:36,494 INFO ----- curNumber: 0 ----- 2020-05-18 17:51:36,494 INFO ----- curNumber: 0 ----- 2020-05-18 17:51:36,494 INFO ----- curNumber: 0 ----- 2020-05-18 17:51:36,494 INFO ----- curNumber: 0 ----- 2020-05-18 17:51:36,494 INFO ----- curNumber: 0 ----- 2020-05-18 17:51:36,495 INFO ----- curNumber: 0 ----- 2020-05-18 17:51:36,495 INFO ----- curNumber: 0 ----- 2020-05-18 17:51:36,494 INFO ----- curNumber: 0 ----- 2020-05-18 17:51:36,495 INFO ----- curNumber: 0 ----- 2020-05-18 17:51:36,495 INFO ----- curNumber: 0 ----- 2020-05-18 17:51:36,495 INFO finished 10 runs 2020-05-18 17:51:36,495 INFO ----- curNumber: 0 ----- 2020-05-18 17:51:36,496 INFO ----- curNumber: 0 ----- 2020-05-18 17:51:36,497 INFO ----- curNumber: 0 ----- 2020-05-18 17:51:36,497 INFO ----- curNumber: 0 ----- 2020-05-18 17:51:36,497 INFO ----- curNumber: 0 ----- 2020-05-18 17:51:36,497 INFO ----- curNumber: 0 ----- 2020-05-18 17:51:36,498 INFO ----- curNumber: 0 ----- 2020-05-18 17:51:36,498 INFO ----- curNumber: 0 ----- 2020-05-18 17:51:36,498 INFO finished 10 runs 2020-05-18 17:51:36,498 INFO ----- curNumber: 0 ----- 2020-05-18 17:51:36,498 INFO ----- curNumber: 0 ----- 2020-05-18 17:51:36,498 INFO finished 10 runs 2020-05-18 17:51:36,499 INFO ----- curNumber: 0 ----- 2020-05-18 17:51:36,499 INFO finished 10 runs 2020-05-18 17:51:36,499 INFO ----- curNumber: 0 ----- 2020-05-18 17:51:36,500 INFO ----- curNumber: 0 ----- 2020-05-18 17:51:36,501 INFO ----- curNumber: 0 ----- 2020-05-18 17:51:36,501 INFO finished 10 runs 2020-05-18 17:51:36,505 INFO elapsed time is 56 ms 2020-05-18 17:51:36,505 INFO Final statistics for this process: 2020-05-18 17:51:36,516 INFO |
Administrator
|
Attach my full code.
import static net.grinder.script.Grinder.grinder import static org.junit.Assert.* import static org.hamcrest.Matchers.* import net.grinder.plugin.http.HTTPRequest import net.grinder.plugin.http.HTTPPluginControl import net.grinder.script.GTest import net.grinder.script.Grinder import net.grinder.scriptengine.groovy.junit.GrinderRunner import net.grinder.scriptengine.groovy.junit.annotation.BeforeProcess import net.grinder.scriptengine.groovy.junit.annotation.BeforeThread // import static net.grinder.util.GrinderUtils.* // You can use this if you're using nGrinder after 3.2.3 import org.junit.Before import org.junit.BeforeClass import org.junit.Test import org.junit.runner.RunWith import java.util.Date import java.util.List import java.util.ArrayList import HTTPClient.Cookie import HTTPClient.CookieModule import HTTPClient.HTTPResponse import HTTPClient.NVPair /** * A simple example using the HTTP plugin that shows the retrieval of a * single page via HTTP. * * This script is automatically generated by ngrinder. * * @author admin */ @RunWith(GrinderRunner) class TestRunner { public static GTest test public static HTTPRequest request public static NVPair[] headers = [] public static NVPair[] params = [] public static Cookie[] cookies = [] def curNumber; @BeforeProcess public static void beforeProcess() { HTTPPluginControl.getConnectionDefaults().timeout = 6000 test = new GTest(1, "Test1") request = new HTTPRequest() grinder.logger.info("before process."); } @BeforeThread public void beforeThread() { test.record(this, "test") grinder.statistics.delayReports=true; grinder.logger.info("before thread."); int totalProcessCount = grinder.getProperties().getInt("grinder.processes", 1); int totalThreadCount = grinder.getProperties().getInt("grinder.threads", 1); int agentNumber = grinder.agentNumber int processNumber = grinder.processNumber int threadNumber = grinder.threadNumber curNumber = ((agentNumber * totalProcessCount * totalThreadCount) + (processNumber * totalThreadCount) + threadNumber); } @Before public void before() { request.setHeaders(headers) cookies.each { CookieModule.addCookie(it, HTTPPluginControl.getThreadHTTPClientContext()) } grinder.logger.info("before. init headers and cookies"); } @Test public void test(){ sleep(1000); grinder.logger.info("curNum is : " + curNumber); } } ------------------------------------------------------------------------------------- 2020-05-22 16:48:08,546 INFO before. init headers and cookies 2020-05-22 16:48:09,636 INFO curNum is : 1 2020-05-22 16:48:09,636 INFO curNum is : 2 2020-05-22 16:48:09,636 INFO curNum is : 4 2020-05-22 16:48:09,636 INFO curNum is : 0 2020-05-22 16:48:09,636 INFO curNum is : 3 2020-05-22 16:48:09,638 INFO before. init headers and cookies 2020-05-22 16:48:09,638 INFO before. init headers and cookies 2020-05-22 16:48:09,638 INFO before. init headers and cookies 2020-05-22 16:48:09,638 INFO before. init headers and cookies 2020-05-22 16:48:09,638 INFO before. init headers and cookies 2020-05-22 16:48:10,640 INFO curNum is : 2 2020-05-22 16:48:10,641 INFO curNum is : 1 2020-05-22 16:48:10,640 INFO curNum is : 3 2020-05-22 16:48:10,641 INFO curNum is : 4 2020-05-22 16:48:10,640 INFO curNum is : 0 2020-05-22 16:48:10,642 INFO before. init headers and cookies 2020-05-22 16:48:10,642 INFO before. init headers and cookies 2020-05-22 16:48:10,642 INFO before. init headers and cookies 2020-05-22 16:48:10,642 INFO before. init headers and cookies 2020-05-22 16:48:10,642 INFO before. init headers and cookies 2020-05-22 16:48:11,643 INFO curNum is : 1 2020-05-22 16:48:11,643 INFO curNum is : 2 2020-05-22 16:48:11,643 INFO curNum is : 3 2020-05-22 16:48:11,643 INFO curNum is : 0 2020-05-22 16:48:11,643 INFO curNum is : 4 2020-05-22 16:48:11,644 INFO before. init headers and cookies 2020-05-22 16:48:11,644 INFO before. init headers and cookies 2020-05-22 16:48:11,644 INFO before. init headers and cookies |
Banned User
|
This post was updated on .
In reply to this post by apurvjain17
CONTENTS DELETED
The author has deleted this message.
|
Free forum by Nabble | Edit this page |