How to share state between test processes?

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

How to share state between test processes?

apurvjain17
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
Reply | Threaded
Open this post in threaded view
|

Re: How to share state between test processes?

leedonggyu
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.
}
ken
Reply | Threaded
Open this post in threaded view
|

Re: How to share state between test processes?

ken
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  
Reply | Threaded
Open this post in threaded view
|

Re: How to share state between test processes?

leedonggyu
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
Reply | Threaded
Open this post in threaded view
|

Re: How to share state between test processes?

darrenmooleZ

Buy Backpackboyz kush

Berner Cookies for sale

Buy Backwoods Cigars

Buy Gumbo Kush

Buy Runtz weed

Buy Space Monkey Meds

Buy West Coast Cure

Jungle boys for sale

Buy Muha Meds Cartridges

Prerolls For Sale,

Stiiizy carts for sale


<a href="https://potsnbuds.com/product/buy-banana-runtz/ " rel="dofollow"> Buy Banana Runtz
<a href="https://potsnbuds.com/product/buy-buddhas-hands-runtz/ " rel="dofollow">Buy Buddhas Runtz
<a href="https://potsnbuds.com/product/buy-ether-runtz/ " rel="dofollow"> Buy Ether Runtz <a href="https://potsnbuds.com/product/buy-gruntz-online/ " rel="dofollow">Buy Gruntz Online
<a href="https://potsnbuds.com/product/buy-hawaiian-runtz/ " rel="dofollow">Buy Hawaiian Runtz
<a href="https://potsnbuds.com/product/buy-moneybagg-runtz/ " rel="dofollow">Buy Moneybagg Runtz
<a href="https://potsnbuds.com/product/buy-pink-runtz/ " rel="dofollow">Buy Pink Runtz
<a href="https://potsnbuds.com/product/buy-runtz-og/ " rel="dofollow">Buy Runtz Og
<a href="https://potsnbuds.com/product/buy-sharklato-runtz/ " rel="dofollow">Buy Sharklato Runtz
<a href="https://potsnbuds.com/product/buy-white-runtz/ " rel="dofollow">Buy White Runtz
<a href="https://potsnbuds.com/product/frosties-runtz/ " rel="dofollow">Frosties Runtz
<a href="https://potsnbuds.com/product/peach-kobbler-runtz/ " rel="dofollow"> Peach Kobbler Runtz
<a href="https://potsnbuds.com/product/runtz-weed/ " rel="dofollow">Runtz Weed
<a href="https://potsnbuds.com/product/vlone-runtz/ " rel="dofollow"> Vlone Runtz
<a href="https://potsnbuds.com/product/whole-lotta-runtz/ " rel="dofollow"> Whole Lotta Runtz
<a href="https://potsnbuds.com/product/zourz-runtz/ " rel="dofollow"> Zourz Runtz
Cosmic Cookies West Coast Cure 
MAC1 West Coast Cure
 Budder Cake West Coast Cure
 Crossroads OG West Coast Cure  
Cosmic Cookies West Coast Cure 
TITS West Coast Cure 
CUREpod Battery Black  
Carol B West Coast Cure
 Watermelon Sandia West Coast Cure 
Irene West Coast Cure
 Gas OG West Coast Cure 
Everest West Coast Cure 
Jungle Boys Jungle Lemons 
Jungle Boys Perfect Triangle 
Jungle Boys Lava Kush #6 
Jungle Boys Jungle Driver #12 
Jungle Boys Gelato Sundae #2 
Jungle Boys GATOR BREATH #19 
Jungle Boys CHERRY COOKIES 
Jungle Boys Grape Cake Head 
Jungle Boys FLORIDA KUSH 
Jungle Boys Dosi Killer 
CHEM 91 SKUNK VA Jungle boys 
Jungle Boys CHERRY COOKIES 
Jungle Boys Candy Cake 
Jungle Boys Bakers Man 
Zourz Runtz 
Frosties Runtz 
Buy Buddha’s Hands Runtz 
Vlone Runtz 
Whole Lotta Runtz 
Legend OG West Coast Cure 
Buy White Runtz 
Buy Sharklato Runtz 
Buy Runtz OG 
Buy Banana Runtz 
Peach kobbler Runtz 
Buy Gruntz online 
Buy Pink Runtz  
Buy Ether runtz 
Buy Hawaiian Runtz 
Buy Moneybagg runtz
Buy Runtz weed
Jungle Boys Packs

Buy Space Monkey Meds
Cosmic Cookies West Coast Cure 
MAC1 West Coast Cure
 Budder Cake West Coast Cure
 Crossroads OG West Coast Cure  
Cosmic Cookies West Coast Cure 
TITS West Coast Cure 
CUREpod Battery Black  
Carol B West Coast Cure
 Watermelon Sandia West Coast Cure 
Irene West Coast Cure
 Gas OG West Coast Cure 
Everest West Coast Cure 
Jungle Boys Jungle Lemons 
Jungle Boys Perfect Triangle 
Jungle Boys Lava Kush #6 
Jungle Boys Jungle Driver #12 
Jungle Boys Gelato Sundae #2 
Jungle Boys GATOR BREATH #19 
Jungle Boys CHERRY COOKIES 
Jungle Boys Grape Cake Head 
Jungle Boys FLORIDA KUSH 
Jungle Boys Dosi Killer 
CHEM 91 SKUNK VA Jungle boys 
Jungle Boys CHERRY COOKIES 
Reply | Threaded
Open this post in threaded view
|

Re: How to share state between test processes?

darrenmoole
Banned User
This post was updated on .
In reply to this post by apurvjain17
CONTENTS DELETED
The author has deleted this message.