This is the 8th day of my participation in the November Gwen Challenge. Check out the event details: The last Gwen Challenge 2021

In order to do some code, index, redis performance optimization, the recent use of nGrinder system performance testing actual combat, there are more harvest.

First download the source code deployment environment, the nGrinder test scripts support Python, and support single-interface and multi-interface mixed performance testing, supporting a large number of concurrent users.

NGrinder is an open source B/S Web performance testing platform based on the Grinder developed by NHN company in the Java language, with a friendly and simple user interface and distributed testing functions.

You can test any Java code, including a variety of commonly used interfaces. The framework also includes the Web side.

When it comes to performance testing, JMeter is the most commonly used tool, but it is a long time ago. However, JMeter is based on the number of threads, so the number of users to simulate is limited. It cannot simulate thousands of users, and the usage scenarios are limited. The nGrinder, by contrast, can support thousands of concurrent applications, is distributed, can monitor servers under test, has open source code, is easy to platform, and comes with a TPS diagram.

Here are some test scripts.

Single interface – uses fixed parameters
from Config import test_data_list
    
    # request URL
    url = 'http://www.testDemo.com/index/page/login'# set the Header parameter
    headers.append(NVPair('TOKEN'.' '))
    headers.append(NVPair("User-Agent".'the 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'.' '))
    Set the params parameter
    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)
​
​
​
Copy the code
Single interface – Custom parameters
The key code

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])))
Copy the code
Mixed interface

The run. Py files

# -*- coding: utf-8 -*-
from java.util import Random
from Config import dicts_item_detail
​
This file does not need any changes
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()
Copy the code

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()
​
​
​
# Hybrid interface - Proportional configuration
dicts_item_detail = {
    "PostDemo":1."GetDemo":2,}Copy the code