관리 메뉴

취미개발 블로그와 마음수양

Ngrinder 관련 정리 본문

Tools/각종성능

Ngrinder 관련 정리

아라한사 2020. 8. 10. 12:18

음 인생을 백엔드반 프론트반 짬뽕으로 살다가( -_ -) 아니.. 회사 일 하다보니 정신차리고보니 이런 포지션이더라. ㅋ 

아무튼 그래도 뭐 .. NGrinder 같은 것들도 좀 봐야겠다 싶어서 정리.. 

내가 따로 글 쓸 것은 없고 이곳저곳에서 잘 모아봐야겠다.싶다. 

 

https://nesoy.github.io/articles/2018-10/nGrinder-Start

 

nGrinder 시작하기

 

nesoy.github.io

nGrinder JSON 요청할때 참고할 만한 부분

https://www.cnblogs.com/lindows/p/9375306.html

 

nGrinder TestRunner http post json - siemens800 - 博客园

s nGrinder学习笔记 — post请求 https://blog.csdn.net/meyoung01/article/details/50435881 end

www.cnblogs.com

https://blog.csdn.net/meyoung01/article/details/50435881

 

nGrinder学习笔记 — post请求_米阳MeYoung-CSDN博客_net.grinder.中没有plugin

抵扣说明: 1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。 2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

blog.csdn.net

소스복붙 (혹시모르니)

import HTTPClient.HTTPResponse
import HTTPClient.NVPair
import ch.qos.logback.classic.Level
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 org.junit.Test
import org.junit.runner.RunWith
import org.slf4j.LoggerFactory
 
import static net.grinder.script.Grinder.grinder
import static org.hamcrest.Matchers.is
import static org.junit.Assert.assertThat
 
/**
 * Created by nd-xm-mac5 on 15/12/24.
 */
@RunWith(GrinderRunner)
class LoginDemo {
    public static GTest test
    public static HTTPRequest request
 
    @BeforeProcess
    public static void beforeProcess() {
        HTTPPluginControl.getConnectionDefaults().timeout = 6000
        test = new GTest(1, "192.168.70.206")
        request = new HTTPRequest()
        test.record(request);
        grinder.logger.info("before process.");
    }
 
    @BeforeThread
    public void beforeThread() {
//        只打印错误的log
        LoggerFactory.getLogger("worker").setLevel(Level.ERROR)
 
        grinder.statistics.delayReports = true;
        grinder.logger.info("before thread.");
    }
 
    private NVPair[] headers() {
        return [
                new NVPair("Content-type", "application/json;charset=UTF-8")
        ];
    }
 
    @Test
    public void test1() {
        // json字符串
        def json = '{"tenant_code":"XXX","user_name":"XX","password":"X","skip_duplicate_entries":true,"type":"0"}';
        // 发起请求
        HTTPResponse result = request.POST("http://192.XXX.XX.XXX:XXXX/XXX/XX/login", json.getBytes(), headers());
        grinder.logger.info(result.getText());
        grinder.logger.info(result.getHeader("Content-type"));
 
        if (result.statusCode == 301 || result.statusCode == 302) {
            grinder.logger.warn("Warning. The response may not be correct. The response code was {}.", result.statusCode);
        } else {
            assertThat(result.statusCode, is(200));
        }
    }

 

파이썬

 

# -*- coding:utf-8 -*-
#
# 一个简单的HTTP请求脚本示例
#
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
#导入对JSON格式返回值的处理函数
from org.json import JSONObject
 
 
control = HTTPPluginControl.getConnectionDefaults()
#请求重定向开关
# control.followRedirects = 1
#超时时间设置
control.timeout = 6000
 
test1 = Test(1, "Test1")
request1 = HTTPRequest()
 
# Make any method call on request1 increase TPS
test1.record(request1)
 
class TestRunner:
    #初始化,仅执行一次
    def __init__(self):
        grinder.statistics.delayReports=True
        pass
 
 
    #类似LR的action,压测时会多次执行
    # test method       
    def __call__(self):
 
                #url地址
        url = 'http://www.xxx.com/v0.93/login'
        #headers信息
        headers = [NVPair("Content-Type","application/json"),
                   NVPair("Authorization","123")
                   ]
        #JSON格式的请求内容
        submitdata = '{"login_name":"156599@ND","password":"80fba977d063a6f7262a8a9c95f61140"}'
        #URL请求
        result = request1.POST(url,submitdata,headers)
 
        #打印输出URL请求返回内容
        #grinder.logger.info(result.getText())
 
        #返回结果检查,有返回特定字符,则判断请求成功
        if result.getText().find("检查内容") != -1 :
            grinder.statistics.forLastTest.success = 1
        else :
            grinder.statistics.forLastTest.success = 0
            #请求失败,则输出失败时服务器返回的值
            grinder.logger.info(result.getText())