Multi request test with preserving the session

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

Multi request test with preserving the session

ivannov
Hello,

My name is Ivan and I am relatively new to nGrinder.

I have the following use case. The application, which I am using to test the performance of our server is a very simplified web forum. So basically, here is what I want to achieve with it:

0) Before every thread I want to login
1) Using the same connection (to preserve the session) I want to first load the home page
2) Next I want to load the data for a particular forum topic
3) Then I want to add a topic
etc....

What I noticed is that if I initiate the session in a method different than the test methods where I do the real request, the session information is lost. Here is what I did:

    @BeforeThread
    public void login() {
        HTTPPluginControl.getConnectionDefaults().setUseCookies(true)
        NVPair[] loginData = [ ["userName", "myUser"], ["password", "myPassword"] ]
        request.POST("${APPLICATION_HOME}/login", loginData)
    }

And then in the test method:

    @Test
    public void homePage() {
        request.GET("${APPLICATION_HOME}/forum")
    }

I think that if the requests are executed in separate methods, they initiate a new connection to the server, but I am not exactly sure.

However, if I put everything in one test method, it works fine:

    @Test
    public void homePage() {
        HTTPPluginControl.getConnectionDefaults().setUseCookies(true)
        NVPair[] loginData = [ ["userName", "ivan"], ["password", "ivan"] ]
        request.POST("${APPLICATION_HOME}/login", loginData)
        request.GET("${APPLICATION_HOME}/forum")
    }

Another question that I have concerns the final statistic. After a test is executed nGrinder produces this nice little table:

Test 1       142          0            38.06        41.09        20.82        68706.75     1430341.30   0            0.73         3.42         24.51         "My test"

Totals       142          0            38.06        41.09        20.82        68706.75     1430341.30   0            0.73         3.42         24.51        

I'd like to have separate "Test 1"-like results for every request that I execute. I assume that for that I need to create a separate GTest object and a separate HTTPRequest object, which will be recorded by the corresponding GTest. Did I get it right? And at the end I will have a separate row in the table for all the GTest's that I created, i.e. for all the different requests.

If this is the right approach, then how do I handle the session information? I still want to log in once per thread and propagate that to all the subsequent requests.

Thanks,
Ivan
Reply | Threaded
Open this post in threaded view
|

Re: Multi request test with preserving the session

Gisoo.Gwon
Hello :)

first question.


If use one connection, i think get statistics that wrong result.
Use this way.
-------------------------------------------------------------------------------------------------------
public Object cookies = []

@BeforeThread
  // login and record login cookie(if use session,, can solve similar way)
  HTTPRequest loginReq = new HTTPRequest()
  Object threadContext = HTTPPluginControl.getThreadHTTPClientContext()
  cookies = CookieModule.listAllCookies(threadContext)
  cookies.each {
    CookieModule.removeCookie(it, threadContext)
  }
         
  // login
  NVPair[] params = [new NVPair("id", "MY_ID"), new NVPair("pw", "MY_PASSWORD")];
  HTTPResponse res = loginReq.POST("${APPLICATION_HOME}/login", params);
  cookies = CookieModule.listAllCookies(threadContext)

@Before
  Object threadContext = HTTPPluginControl.getThreadHTTPClientContext()
  cookies.each {
    CookieModule.addCookie(it, threadContext)
  }
-------------------------------------------------------------------------------------------------------

second question.


You can solve GTest.record(Object, String).
I guess your script,
---------------------------------------------------
GTest test = new GTest(1, "My test");
HTTPRequest request = new HTTPRequest();
test.record(request);
---------------------------------------------------
This script opertate,
Any request method recorded statistics data.

So, use this script.
---------------------------------------------------
@BeforeProcess
  GTest test = new GTest(1, "My test");
  GTest httpTest = new GTest(2, "call of request");
  HTTPRequest request = new HTTPRequest();
@BeforeThread
  test.record(this, "homePage"); // record homePage() method of this object
  httpTest .record(request);
---------------------------------------------------
Reply | Threaded
Open this post in threaded view
|

Re: Multi request test with preserving the session

ivannov
Hi! :)

Thanks a lot for the answer! Now my test works and looks pretty neat. Just for the record (if anyone else arrives on this page), here is the new version:

import HTTPClient.Cookie
import HTTPClient.CookieModule
import HTTPClient.NVPair
import net.grinder.plugin.http.HTTPPluginControl
import net.grinder.plugin.http.HTTPRequest
import net.grinder.script.GTest
import net.grinder.scriptengine.groovy.junit.GrinderRunner
import net.grinder.scriptengine.groovy.junit.annotation.BeforeProcess
import net.grinder.scriptengine.groovy.junit.annotation.BeforeThread
import net.grinder.util.GrinderUtils
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith

@RunWith(GrinderRunner)
class PersistenceTest {

    public static GTest homePageTest
    public static GTest topicTest

    public static HTTPRequest request
    private static String APPLICATION_HOME
    private Object cookies = []

    @BeforeProcess
    public static void beforeClass() {
        HTTPPluginControl.getConnectionDefaults().setUseCookies(true)
        homePageTest = new GTest(1, "Home page")
        topicsTest = new GTest(2, "Show topic")

        request = new HTTPRequest()
        APPLICATION_HOME = GrinderUtils.getParam()
    }

    @BeforeThread
    public void login() {
        HTTPRequest loginReq = new HTTPRequest()

        Object threadContext = HTTPPluginControl.getThreadHTTPClientContext()
        cookies = CookieModule.listAllCookies(threadContext)
        cookies.each {
            CookieModule.removeCookie(it as Cookie, threadContext)
        }

        NVPair[] loginData = [ ["userName", "myUser"], ["password", "myPassword"] ]
        loginReq.POST("${APPLICATION_HOME}/login", loginData)
        cookies = CookieModule.listAllCookies(threadContext)

        homePageTest.record(this, "homePage")
        topicTest.record(this, "topic")
    }

    @Before
    public void setup() {
        Object threadContext = HTTPPluginControl.getThreadHTTPClientContext()
        cookies.each {
            CookieModule.addCookie(it as Cookie, threadContext)
        }
    }

    @Test
    public void homePage() {
        request.GET("${APPLICATION_HOME}/forum")
    }

    @Test
    public void topic() {
        request.GET("${APPLICATION_HOME}/topic?topicId=2164")
    }
}

Cheers,
Ivan