관리 메뉴

don't stop believing

nGrinder에서 json 다루기 본문

Testing Automation/nGrinder

nGrinder에서 json 다루기

Tongchun 2018. 10. 24. 14:36

nGrinder에서 json을 다룰때는 JSONObject을 사용합니다. Python의 기본 json 을 다루는 것과는 조금 다릅니다.

-*- coding:utf-8 -*-
 
# 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
from net.grinder.script.Grinder import grinder
from net.grinder.script import Test
from net.grinder.plugin.http import HTTPRequest
from net.grinder.plugin.http import HTTPPluginControl
from HTTPClient import NVPair
from org.json import JSONObject
 
control = HTTPPluginControl.getConnectionDefaults()
# if you don't want that HTTPRequest follows the redirection, please modify the following option 0.
# control.followRedirects = 1
# if you want to increase the timeout, please modify the following option.
control.timeout = 6000
 
test1 = Test(1, "Test1")
request1 = HTTPRequest()
 
# Make any method call on request1 increase TPS
test1.record(request1)
 
class TestRunner:
    # initlialize a thread 
    def __init__(self):
        grinder.statistics.delayReports=True
        pass
 
    # test method       
    def __call__(self):
        result = request1.GET("http://google.com")
         
        message = """
        {
	"glossary": {
		"title": "example glossary",
		"GlossDiv": {
			"title": "S",
			"GlossList": {
				"GlossEntry": {
					"ID": "SGML",
					"SortAs": "SGML",
					"GlossTerm": "Standard Generalized Markup Language",
					"List": ["apple", "orange", "banana"],
					"Acronym": "SGML",
					"Abbrev": "ISO 8879:1986",
					"GlossDef": [{
						"para": "A meta-markup language, used to create markup languages such as DocBook."
					}, {
						"para": "Tests resulting in error only contribute to the Errors column."
					}, {
						"para": "registered plug-in net.grinder.plugin.http.HTTPPlugin"
					}],
					"GlossSee": "markup"
				}
			}
		}
	}
}
        """
         
        json_1 = JSONObject(message)
        json_2 = json_1.getJSONObject("glossary")
        json_3 = json_2.getJSONObject("GlossDiv")
        json_4 = json_3.getJSONObject("GlossList")
        json_5 = json_4.getJSONObject("GlossEntry")
        json_6 = json_5.getString("GlossTerm")
        json_7 = json_5.getString("List")
        json_8 = json_5.getJSONArray("GlossDef").getJSONObject(1).getString("para")
         
        print "----------> %s" % json_8
        
        for i in range(0, json_5.getJSONArray("GlossDef").length()) :
			print "para(%s)----------> %s" % (i, json_5.getJSONArray("GlossDef").getJSONObject(i).getString("para"))
        
        if result.getStatusCode() == 200 :
            grinder.statistics.forLastTest.success = 1
        elif result.getStatusCode() in (301, 302) :
            grinder.logger.warn("Warning. The response may not be correct. The response code was %d." %  result.getStatusCode()) 
            grinder.statistics.forLastTest.success = 1
        else :
            grinder.statistics.forLastTest.success = 0

만약 nGrinder안에서 Python의 samplejson을 사용하고 싶다면 samplejson 모듈을 nGrinder에 추가하고 아래와 같이 사용하면 됩니다.

simplejson 모듈을 다운로드 하고 폴더와 파일들을 nGrinder의 lib 폴더로 복사하면 됩니다.

대량의 파일을 nGrinder에 올릴때는 nGrinder에서 제공하는 svn을 사용하면 편합니다.

# -*- coding:utf-8 -*-

# 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
from net.grinder.script.Grinder import grinder
from net.grinder.script import Test
from net.grinder.plugin.http import HTTPRequest
from net.grinder.plugin.http import HTTPPluginControl
from HTTPClient import NVPair
import simplejson as json

control = HTTPPluginControl.getConnectionDefaults()
# if you don't want that HTTPRequest follows the redirection, please modify the following option 0.
# control.followRedirects = 1
# if you want to increase the timeout, please modify the following option.
control.timeout = 6000

test1 = Test(1, "Test1")
request1 = HTTPRequest()

# Make any method call on request1 increase TPS
test1.record(request1)

class TestRunner:
	# initlialize a thread 
	def __init__(self):
		grinder.statistics.delayReports=True
		pass

	# test method		
	def __call__(self):
		result = request1.GET("http://google.com")
		
		message = """
        {
	"glossary": {
		"title": "example glossary",
		"GlossDiv": {
			"title": "S",
			"GlossList": {
				"GlossEntry": {
					"ID": "SGML",
					"SortAs": "SGML",
					"GlossTerm": "Standard Generalized Markup Language",
					"List": ["apple", "orange", "banana"],
					"Acronym": "SGML",
					"Abbrev": "ISO 8879:1986",
					"GlossDef": [{
						"para": "A meta-markup language, used to create markup languages such as DocBook."
					}, {
						"para": "Tests resulting in error only contribute to the Errors column."
					}, {
						"para": "registered plug-in net.grinder.plugin.http.HTTPPlugin"
					}],
					"GlossSee": "markup"
				}
			}
		}
	}
}
        """        
        
		find_text = json.loads(message)

		print find_text["glossary"]["title"]
		print find_text["glossary"]["GlossDiv"]["title"]
		print find_text["glossary"]["GlossDiv"]["GlossList"]["GlossEntry"]["ID"]
		print find_text["glossary"]["GlossDiv"]["GlossList"]["GlossEntry"]["SortAs"]
		print find_text["glossary"]["GlossDiv"]["GlossList"]["GlossEntry"]["GlossTerm"]
		print find_text["glossary"]["GlossDiv"]["GlossList"]["GlossEntry"]["List"][0]
		print find_text["glossary"]["GlossDiv"]["GlossList"]["GlossEntry"]["List"][1]
		print find_text["glossary"]["GlossDiv"]["GlossList"]["GlossEntry"]["List"][2]
		print find_text["glossary"]["GlossDiv"]["GlossList"]["GlossEntry"]["GlossDef"][0]["para"]
		print find_text["glossary"]["GlossDiv"]["GlossList"]["GlossEntry"]["GlossDef"][1]["para"]
		
		if result.getStatusCode() == 200 :
			grinder.statistics.forLastTest.success = 1
		elif result.getStatusCode() in (301, 302) :
			grinder.logger.warn("Warning. The response may not be correct. The response code was %d." %  result.getStatusCode()) 
			grinder.statistics.forLastTest.success = 1
		else :
			grinder.statistics.forLastTest.success = 0

하지만 simplejson 모듈을 nGrinder에 업로드 하는것 보다 JSONObject를 익히는 게 더 편할 것 같습니다.


'Testing Automation > nGrinder' 카테고리의 다른 글

nGrinder에서 json 다루기  (0) 2018.10.24
CentOS에 nGrinder 설치 하기 (v3.4)  (0) 2017.10.11
nGrinder Controller 설정  (1) 2017.09.19
Ubuntu에 nGrinder 설치 하기 (v3.4)  (0) 2017.09.05
0 Comments
댓글쓰기 폼