grinder安装(Eclipse下的源码安装与配置)

2016-12-23  本文已影响837人  龙猫六六

grinder是用来测试服务器性能的工具之一
下面简单介绍下如何在本地安装grinder,及其简单的适用方法。
首先说明下我的

本地环境:

Eclipse Type:Eclipse Java EE IDE for Web Developers.
Eclipse Version: Mars.1 Release (4.5.1)
java version: "1.7.0_79"

grinder 安装版本:Grinder-3.11

url:https://git.hz.netease.com/qaperf/grinder/tree/ptp/3.11/master
安装步骤:

7.jpg

grinder脚本调试

调试脚本:xxx.py

from net.grinder.script import Test
from net.grinder.script.Grinder import grinder
from net.grinder.plugin.http import HTTPPluginControl
from net.grinder.plugin.http import HTTPRequest
from HTTPClient import NVPair
import time
//从配置文件读取属性
process_num = int(grinder.getProperties().get('grinder.processes'))
thread_num = int(grinder.getProperties().get('grinder.threads'))
 //http heaser信息
connectionDefaults = HTTPPluginControl.getConnectionDefaults()
httpUtilities = HTTPPluginControl.getHTTPUtilities()
cookie = "11111"
url = "https://baidu.com"    
method = "POST"  
body = "ewewe"    
connectionDefaults.defaultHeaders = [ 
    NVPair('Accept-Language', 'zh-cn'),
    NVPair('User-Agent', 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/37.0.2062.103 Safari/537.36')]
headers = [NVPair('Accept', 'application/json'),
            NVPair('Connection', 'keep-alive'),
            NVPair('Content-Type','application/json'),
            NVPair('Cookie',cookie)]
 //下面为函数定义
class TestRunner: //关键字  
    def api_post(self, body): //post请求
        result = HTTPRequest(url=url, headers=headers).POST(url, body)
        data = result.getText().encode('utf-8')
        print data
        if(result.getStatusCode() != 200): //如果code不等于200,表明请求失败
            grinder.getStatistics().getForCurrentTest().setSuccess(False)
    def api_get(self, body): //get请求
        result = HTTPRequest(url=url, headers=headers).GET(url1 % body)
        data = result.getText().encode('utf-8')
        if(result.getStatusCode() != 200 or data.find('"code":200') == -1):
            grinder.getStatistics().getForCurrentTest().setSuccess(False)
            err(data)      
    request101 = Test(101, 'case_post').wrap(api_post) //声明函数,且用wrap表明需要进行统计的关键字
    request102 = Test(102, 'case_get').wrap(api_get)
    def initialSleep(self):
        sleepTime = grinder.threadNumber * 100
        grinder.sleep(sleepTime, 0)
    def __call__(self):
        if grinder.runNumber == 0:
            self.initialSleep()    
        if (method == 'POST'):
            self.request101(body)   //执行post请求
        if (method == 'GET'):
            self.request102(body)   //执行get请求

grinder配置文件:grinder.properties

grinder.script = scripts/perfTest2.py //脚本路径
grinder.useConsole = false
grinder.processes = 1  //进程数
grinder.threads = 1  //线程数
grinder.duration =0  //运行时间
grinder.runs = 5  //运行次数
grinder.logDirectory = logs
grinder.numberOfOldLogs = 2
grinder.debug = true

grinder运行

1.调用src下面的net.grinder.Grinder运行(run as->java application)


7.jpg

2.运行的日志在工程的log目录下,一般有三个文件,data,out和error。data中是每次执行的记录,out文件是grinder的运行情况和统计信息,error中为错误信息


7.jpg
  1. 如果你的测试脚本需要读写的文件,需要将其加到Grinder的path下面,如果测试代码使用java代码实现,可在Grinder的java bulid path下添加测试代码的java工程,即可进行调试。
Paste_Image.png
上一篇下一篇

猜你喜欢

热点阅读