groovy Selective instrumentation 방법??

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

groovy Selective instrumentation 방법??

hjyooooo
안녕하세요^^  지난번 질문 답변 감사드립니다.  

또 하나의 문의가 있어 글 남깁니다.

저는  로컬 이클립스에서  groovy Maven project로 script를 작성하고 있습니다.

public static GTest test
@BeforeProcess
   test = new GTest(1, "Test1")
   request = new HTTPRequest()
   test.record(request)

정의후에
 
@Test 함수에서
request.setUrl()
request.setsetHeaders()
request.setData()
request.POST(uri)

이렇게 호출을 합니다. 그런데 request객체의 어떤 함수라도 실행이 되면 TPS에 영향을 미치는 것 같습니다.

저는 GET, POST 호출할때만 성능통계가 업데이트 되기를 원합니다.

그래서 아래 Selective instrumentation 관련 reference소스를 찾았는데요... Jathon 소스인거 같은데..

아래 소스를 Groovy로 변환해서 사용할려고 하는데... 이래, 저래 변경해서 테스트를 해도 에러가

나고 잘 안되네요. Groovy에서 Selective instrumentation 사용할려면 어떻게 해야 되나요?

부탁드립니다.

===============================================================
from net.grinder.script import Test
from net.grinder.plugin.http import HTTPRequest
 
test = Test(1, "my test")
 
class GetAndPostFilter(Test.InstrumentationFilter):
  def matches(self, method):
    return method.name in ["GET", "POST"]
 
request = HTTPRequest(url="http://grinder.sourceforge.net")
test.record(request, GetAndPostFilter())
 
class TestRunner:
    def __call__(self):
        # GET() is instrumented, so call statistics are reported.
        request.GET()
 
        # getUrl() is not instrumented, no call statistics are reported.
        print "Called %s" % request.url
===============================================================

Reply | Threaded
Open this post in threaded view
|

Re: groovy Selective instrumentation 방법??

junoyoon
Administrator
        GTest test = new GTest(1,  "hello_test");
                test.record(request, new GTest.InstrumentationFilter() {
                       
                        @Override
                        public boolean matches(Object item) {
                                return (item.name == "GET" || item.name == "POST");
                        }
                });

대충 위와 같은 식으로 하시면 됩니다.
그런데 request  자체를 instrumentation 하기 보다는 test 를 instrumentation 하시는게 편할 겁니다.

아래와 같은 식으로..

        @BeforeProcess
        public static void beforeProcess() {
                test = new GTest(1, "Hello");
                request = new HTTPRequest();
        }

        @BeforeThread
        public void beforeThread() {
                grinder.statistics.delayReports=true
                grinder.getLogger().info("before thread in MyTest.");
                // 여기서 Request 가 아닌 doTest 를 Instrumentation 합니다.
                test.record(this, "doTest");
        }


        @Test
        public void testHello(){
                // 앞에서 request 가지고 일단 장난 치고
                // request.setUrl() / request.setsetHeaders() / request.setData()  같은
                // 실제 테스트로 넘깁니다.
                doTest(request);
        }
       
        /** DoTest 자체를 TPS로 처리합니다. */
        public void doTest(HTTPRequest request) {
                request.POST....
        }
       
Reply | Threaded
Open this post in threaded view
|

Re: groovy Selective instrumentation 방법??

hjyooooo
앗!!! 감사합니다~^^

request 보다 test 함수 자체에 instrumentation 하는게 낫겠네요.


답변하신내용중에 의문점이 있습니다.

1.  test.record(this, "doTest");를  @BeforeProcess 에 넣으면 안되나요?   @BeforeThread 에 넣는것과
차이점이 무엇인지요?

2. @Test가 붙은 testHello()안에 모든 내용을 구현하고 test.record(this, "testHello"); 이렇게 해도 되나요?
혹시  test.record()에 @Test가 붙은 함수가 들어가면 안되는 건가요?
Reply | Threaded
Open this post in threaded view
|

Re: groovy Selective instrumentation 방법??

junoyoon
Administrator
This post was updated on .
1. Jython 의 경우, 프로그래밍 언어 구조상 모든 메소드가 객체로 취급 되기 때문에 Instrumentation 할 수 있으나,

예) test.record(TestRunner.doTest)

Groovy는 좀 다릅니다. 직접적으로 메소드를 지정하지 못하므로, 객체 + 메소드 형태로 지정해야 합니다만, BeforeProcess 는 static 메소드이기 때문에, this 라는 키워드가 가르키는 부분이 테스트 객체가 아니게 됩니다. 따라서 this를 이해하지 못합니다. 따라서 테스트객체가 생성된 다음에 this 라고 하셔야 this가 테스트 객체를 가르키게 됩니다. 따라서 @BeforeThread 구문에서 실행하셔야 합니다.

2. 뭐 그렇게 하셔도 됩니다만.. 전체 테스트 스텝에서 일부분만 TPS로 잡으실 것 아닌가요? @Test 가 붙은 메소드건 아니건 간에 ngrinder 는 record 가 된 함수가 호출되이 완료되는 시점에 TPS를 올립니다.

Reply | Threaded
Open this post in threaded view
|

Re: groovy Selective instrumentation 방법??

hjyooooo
답변 감사드립니다^^

그런데 또 질문이 있어서 ^^;;

1. 전체 테스트 중에 일부분만 TPS로 잡도록 구현하는것이 맞겠네요ㅎ

저같은 경우 send 하는 부분을 외부 class로 만들고 import해서 구현을 했습니다.

xxx.xxx.xxx 경로에 SendRequestUtil(sendPost, sendGet 구현)을 만들고

import xxx.xxx.xxx.Util.SendRequestUtil

public static sendRequestUtil

@BeforeProcess
  sendRequestUtil = new SendRequestUtil()
  test = new GTest(1, "Test1")

@BeforeThread
  test.record(sendRequestUtil, "sendPost")
  test.record(sendRequestUtil, "sendGet")


@test
 public void test(){
   // 앞에서 request 가지고 일단 장난 치고
   // request.setUrl() / request.setsetHeaders() / request.setData()  같은
   // 실제 테스트로 넘깁니다.
   def result =  sendRequestUtil.sendPost(request);
   if(result == false)
       assertTrue(result)
       //grinder.statistics.forLastTest.setSuccess(new Boolean(false))
}

이렇게 만들었습니다. TPS는 업데이트 되는것을 확인했습니다. 그런데 정상적으로 되는것인
지 확신이 없어서요  외부 class를 호출할 경우 위와 같이 record를 등록하면 되나요?

2. 에러 기록 방법
 JUnit 기능을 사용해서  assertTrue(result)구문으로 에러처리,  테스트 후 보고서에 에러로 등록 되는것을 확인했습니다. 이렇게 사용하면 되나요? 그리고
grinder.statistics.forLastTest.setSuccess(new Boolean(false)) 이 구문으로도 에러 처리 되는것을 확인했는데요
이 구문을 사용해도 되나요?




Reply | Threaded
Open this post in threaded view
|

Re: groovy Selective instrumentation 방법??

junoyoon
Administrator
1. Instrumentation 될때.. 이미 instrumentation 된 객체를 또 insturmentation할 경우, 어떻게 되는지는 아직 확인하지 않았습니다. 말씀 하신 코드의 경우, 그런 중복 Instrumentation이 될것 같네요. 이 경우는 this 가 아닌 sendRequestUtil 객체를 Insturmentation 하셨으니, 이 코드 자체를 BeforeProcess 로 옮기는게 맞습니다.

2. ngrinder groovy 는 @Test 함수에서 Exception이 발생하여 빠져나오게 될 경우에는 grinder.statistics.forLastTest.success = false 가 자동으로 수행됩니다. 따라서 이 구문을 직접 작성해 주실 필요가 없습니다.


Reply | Threaded
Open this post in threaded view
|

Re: groovy Selective instrumentation 방법??

hjyooooo
비슷한 문제에 대해 계속 질문드려 죄송합니다~~~^.

1. TestRunner클래스안에 있는 함수는 @BeforeThread 에서 record 해야되고 외부객체일경우는 @BeforeProcess에 정의를 해야 된다는 거죠?  
그래서 test.record(sendRequestUtil, "sendPost"), test.record(sendRequestUtil, "sendGet")를  @BeforeProcess에 옮겨야 하고..... 그런데 동일 sendRequestUtil객체를 sendPost로 한번등록,   sendGet에서 또한번 등록해서 중복 Instrumentation이 되는 경우가 발생이 되는데. ...이 중복 Instrumentation 경우는 아직 확인이 안되었다는거죠? ,,

그러면 저는 이미 검증된 방법으로 테스트를 하고 싶은데요.. 아래 2가지 방법을 생각해 보았습니다.

1) sendRequestUtil, sendRequestUtil1 2개를 만들어   @BeforeProcess에 test.record(sendRequestUtil, "sendPost"),  test.record(sendRequestUtil1, "sendGet") 이렇게 따로 등록하는 방법

2) 처음 알려주신 방법대로 TestRunner클래스안에 doTest(request, GET or POST)를 만들고,  test.record(this, "doTest") 등록후 doTest()함수에서 parameter로 GET, POST구분값을 받아서 외부 객체 sendRequestUtil GET, POST함수를 따로 호출하는 방법

이 두가지 방법을 생각해 봤는데.. 가능한것인지요??

2.  @Test 함수에서 assertTrue(result) 의 result값은 Exception났을때가 아니고 제가 따로 구현을 하여 원하는 http code가 아니거나 response의 결과가 원하는 결과가 아닐때 false를 return하게 했습니다. 이경우는 assertTrue(result)를 사용하여 강제 에러 리포팅 하면 되나요??
Reply | Threaded
Open this post in threaded view
|

Re: groovy Selective instrumentation 방법??

junoyoon
Administrator
1. 아뇨 중복 Instrumentation은 @BeforeThread 구문이 각 쓰레드마다 수행되기 때문에 같은 sendRequestUtil에 여러번 recording 하기 때문에 발생합니다.

2. assertTrue(result) 는 result가 false 일때 exception이 자동 발생합니다. assertTrue의 실제 구현을 찾아들어가 보세요. 제가 뭔말 하는지 아실수 있을 겁니다.
Reply | Threaded
Open this post in threaded view
|

Re: groovy Selective instrumentation 방법??

hjyooooo
아~~ 네  ㅎ

그렇다면 sendRequestUtil 객체의 sendPost함수, sendGet함수호출할때  TPS증가하게 하고 싶으면
@BeforeProcess에 이 두개를 등록해서 사용하면 된다는거죠??
test.record(sendRequestUtil, "sendPost")
test.record(sendRequestUtil, "sendGet")

 
만약 sendRequestUtil 객체에 sendXxxxxx() 함수를 하나더 만들어서 TPS를 증가하게 하고 싶으면
test.record(sendRequestUtil, "sendPost")
test.record(sendRequestUtil, "sendGet")
test.record(sendRequestUtil, "sendXxxxxx")

이렇게 한줄씩 추가하면 가능한가요???
Reply | Threaded
Open this post in threaded view
|

Re: groovy Selective instrumentation 방법??

junoyoon
Administrator
한번 해보시면 아실테지만.. 가능합니다.

그런데.. 사실 이와 같이 실제 recording 될 넘을 별도의 클래스를 구성하는 것은 테스트 코드를 다른 코드로 숨기게 되는 형태라.. 사실 권장하지는 않습니다.

테스트를 너무 안쪽으로 숨기게 되면 Readability에 영향을 주기 때문이죠.

Reply | Threaded
Open this post in threaded view
|

Re: groovy Selective instrumentation 방법??

hjyooooo
This post was updated on .
와우~~ 감사합니다.

아 그렇군요.. 테스트 case가 좀 다양하고 request  body 변환작업등

여러 작업을 해서 유형별로 좀 구분해서 class를 만들었는데....

Readability문제가 있겠네요..다음에 만들때 참고하겠습니다.
 
덕분에 테스트 Script 1차 작성을 완료 했습니다.

감사합니다.