性能測試工具nGrinder介紹

安裝

以linux,這裏採用的版本是centos 6 64bit,性能測試工具不建議在Windows上部署。

下載:

https://github.com/naver/ngrinder/releases/

選擇最後面的war包。

服務器端啓動:


# java -XX:MaxPermSize=200m -jar ngrinder-controller-3.4.war --port 8058

這樣nGrinder的管理頁面就部署好,你可以簡單的把ngrinder-controller的功能理解爲性能測試展示和控制,後面會進行詳細介紹。

打開網址:

http://183.131.22.113:8058

默認用戶名和密碼都爲admin

注意:這裏的"Remember me"是短暫停留,頁面關閉之後還是需要重新登陸的。

登錄後點擊右上角的admin,選擇"下載代理"

拷貝下載的文件到agent,現在可以簡單理解爲agent的功能爲執行性能測試,通常不建議與ngrinder-controller部署在同一臺,同一臺機器也不建議部署多個agent。

客戶端啓動:


# nohup ./run_agent.sh  &

快速入門

  • 輸入網址
    http://172.30.249.160
  • 配置
  • jython腳本

點擊 REV:HEAD,可以看到jython腳本

  • 運行

點擊"複製並運行"

  • 查看結果

點擊"詳細測試結果"

參考資料

測試配置

注意agent RAM up配置是基於進程的,一般每個agent有10個進程。

腳本

首先要配置:grinder.properties,通常在用戶目錄的.ngrinder目錄下。

腳本使用Grinder腳本API,參見:http://grinder.sourceforge.net/g3/script-javadoc/index.html

腳本中的grinder對象是ScriptContext實例,這樣就可以獲取上下文信息。

Hello World


# Hello World
#
# A minimal script that tests The Grinder logging facility.
#
# This script shows the recommended style for scripts, with a
# TestRunner class. The script is executed just once by each worker
# process and defines the TestRunner class. The Grinder creates an
# instance of TestRunner for each worker thread, and repeatedly calls
# the instance for each run of that thread.

from net.grinder.script.Grinder import grinder
from net.grinder.script import Test

# A shorter alias for the grinder.logger.info() method.
log = grinder.logger.info

# Create a Test with a test number and a description. The test will be
# automatically registered with The Grinder console if you are using
# it.
test1 = Test(1, "Log method")

# Instrument the info() method with our Test.
test1.record(log)

# A TestRunner instance is created for each thread. It can be used to
# store thread-specific data.
class TestRunner:

    # This method is called for every run.
    def __call__(self):
        log("Hello World")

Simple HTTP example


# A simple example using the HTTP plugin that shows the retrieval of a
# single page via HTTP. The resulting page is written to a file.
#
# More complex HTTP scripts are best created with the TCPProxy.
 
from net.grinder.script.Grinder import grinder
from net.grinder.script import Test
from net.grinder.plugin.http import HTTPRequest
 
test1 = Test(1, "Request resource")
request1 = HTTPRequest()
test1.record(request1)
 
class TestRunner:
    def __call__(self):
        result = request1.GET("http://localhost:7001/")
 
        # result is a HTTPClient.HTTPResult. We get the message body
        # using the getText() method.
        writeToFile(result.text)
 
# Utility method that writes the given string to a uniquely named file.
def writeToFile(text):
    filename = "%s-page-%d.html" % (grinder.processName, grinder.runNumber)
 
    file = open(filename, "w")
    print >> file, text
    file.close()

更多腳本,參見: http://grinder.sourceforge.net/g3/script-gallery.html

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章