性能测试不要只会用JMeter啦,nGrinder也了解一下~

为了做一些代码、索引、redis上的性能优化,最近使用nGrinder进行系统的性能测试实战,有较多的收获。

首先下载源码部署环境,ngrinder的测试脚本支持python语言,且支持单接口和多接口混合的性能测试,支持较大的并发用户数。

简介

nGrinder是NHN公司用Java语言开发的一款的基于Grinder开发的开源B/S Web性能测试平台,具有友好简洁的用户界面和分布式测试功能。

可以测试任何java代码,包括各种常用的接口。该框架还包含了web端。

说起性能测试,所使用的工具中大家比较常用的应该是JMeter了吧,不过这个年代是有点久远了。

但是JMeter是基于线程数,模拟用户数有限,无法模拟到上千用户,使用场景有限。对比下来,nGrinder能支持到数千并发以上,并且支持分布式,还能监控被测服务器,代码开源,平台化使用方便,附上一张TPS图。

图片

测试脚本

下面介绍一下几种测试脚本。

单接口--使用固定参数

from Config import test_data_list
    
    #请求URL
    url = 'http://www.testDemo.com/index/page/login'


    #设置Header头参数
    headers.append(NVPair('TOKEN', ''))
    headers.append(NVPair("User-Agent", 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/95.0.4638.69 Safari/537.36'))
    headers.append(NVPair('Cookie',''))
    #设置params参数
    params = []
    params.append(NVPair('Id', '2'))
    params.append(NVPair("pageIndex", "1"))
    params.append(NVPair("pageSize", "15"))
    params.append(NVPair("topicId", '%s'%(test_data_list[index])))
    params.append(NVPair("topicType", "ca"))
    self.request(url,params,headers)

单接口--自定义参数

关键代码

PostDemo.py

index = (processNum * threadNum * grinder.getRunNumber() + threadNum * grinder.getProcessNumber() + grinder.getThreadNumber()) % len(test_data_list)

params.append(NVPair("topicId", '%s'%(test_data_list[index])))

混合接口

run.py文件

# -*- coding: utf-8 -*-
from java.util import Random
from Config import dicts_item_detail


# 该文件不需要任何改动
scripts = []
total_percent = 0
excute_dicts = {}
for i in dicts_item_detail:
  scripts.append(i)
  percent = dicts_item_detail[i]
  excute_dicts[i] = [total_percent, total_percent + percent]
  total_percent += percent


for script in excute_dicts:
  exec("import %s" % script)


def createTestRunner(script):
  exec("x = %s.TestRunner()" % script)
  return x

class TestRunner:
    def __call__(self):
        random_num = Random().nextInt(total_percent)


        for i in excute_dicts:
            if excute_dicts[i][0] <= random_num and random_num < excute_dicts[i][1]:
                 self.testRunner = createTestRunner(i)
                self.testRunner()

Config.py

# -*- coding: utf-8 -*-
from HTTPClient import NVPair
import os
from net.grinder.script.Grinder import grinder

test_data_list = []


test_file = open("./resources/testdata.csv", 'rb')
for line in test_file.readlines():
    test_data_list.append(line.strip())
test_file.close()


# 混合接口-比例配置
dicts_item_detail = {
    "PostDemo":1,
    "GetDemo":2,
    }

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

酔清风

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值