FunkLoad安装使用(二):基础使用篇

FunkLoad测试由典型的单元测试和配置文件组成。我们来看看FunkLoad示例中包含的一个简单的测试脚本。
1、首先要获得你需要运行的演示示例:
fl-install-demo
# Extract FunkLoad examples into ./funkload-demo : ...  done.
cd funkload-demo/simple

2、测试用例
以下是简单演示测试用例test_Simple.py:
##############################################################################
import unittest
from random import random
from funkload.FunkLoadTestCase import FunkLoadTestCase

class Simple(FunkLoadTestCase):
    """This test use a configuration file Simple.conf."""

    def setUp(self):
        """Setting up test."""
        self.server_url = self.conf_get('main', 'url')

    def test_simple(self):
        # The description should be set in the configuration file
        server_url = self.server_url
        # begin test ---------------------------------------------
        nb_time = self.conf_getInt('test_simple', 'nb_time')
        for i in range(nb_time):
            self.get(server_url, description='Get URL')
        # end test -----------------------------------------------


if __name__ in ('main', '__main__'):
    unittest.main()
##############################################################################
简单的测试用例扩展了FunkLoadTestCase并实现了一个名为test_simple的测试用例。这个测试用例循环获取请求。
该FunkLoadTestCase扩展unittest.TestCase生成,添加方法:
发送HTTP请求(get,post,put,delete或xmlrpc)
帮助建立断言与响应(getBody,getLastUrl,...)
通过访问配置文件(conf_getInt)自定义测试
...
目标URL和请求数量在配置文件(这里如:Simple.conf)中定义。
按照惯例,配置文件的名称是测试用例类的名称和“.conf”扩展名。在我们的例子中:Simple.conf。
3、配置文件
它是一个纯文本文件,其中包含以下部分:
# main section for the test case 测试用例主要部分
[main]
title=Simple FunkLoad tests
description=Simply testing a default static page
#url,要测试的服务器URL
url=http://www.51ste.com

# a section for each test 每个测试的一部分
[test_simple]
#请求main中的URL多少次
description=Access the main URL %(nb_time)s times
nb_time=20

<<裁剪>>
# a section to configure the test mode 一个配置测试模式的部分
[ftest]
log_to = console file
#log_path,日志完成路径
log_path = simple-test.log
#result_path,XML结果文件的完整路径
result_path = simple-test.xml
#sleep_time_min,sleep_time_max 在主机请求之间的最小(最大)睡眠时间(s)
sleep_time_min = 0
sleep_time_max = 0

# a section to configure the bench mode 部分工作台模式配置
[bench]
#cycles,循环周期中并发用户数量,下面为4次循环周期,并发数量分别为50,75,100和125
cycles = 50:75:100:125
#duration,运行周期持续时间(s)
duration = 10
#startup_delay,启动线程等待时间(s)
startup_delay = 0.01
#sleep_time,测试等待时间(s)
sleep_time = 0.01
#cycle_time,在每次循环之间等待的时间(s)
cycle_time = 1
#与[ftest]部分相同的键 - 见上面描述
log_to =
log_path = simple-bench.log
result_path = simple-bench.xml
sleep_time_min = 0
sleep_time_max = 0.5

检查[main]中的URL是否可访问,然后调用fl-run-test。它将运行在[test_simple]的所有测试:
root@ubuntu:/home/ruink/funkload-demo/simple# fl-run-test -dv test_Simple.py
test_simple (test_Simple.Simple) ... test_simple: Starting -----------------------------------
    Access the main URL 20 times
test_simple: GET: http://www.51ste.com
    Page 1: Get URL ...
test_simple:  Done in 1.373s
test_simple:  Load css and images...
test_simple:   Done in 23.995s
test_simple: GET: http://www.51ste.com
    Page 2: Get URL ...
test_simple:  Done in 1.037s
test_simple:  Load css and images...
test_simple:   Done in 0.000s
test_simple: GET: http://www.51ste.com
……
    Page 20: Get URL ...
test_simple:  Done in 1.080s
test_simple:  Load css and images...
test_simple:   Done in 0.000s
ok

----------------------------------------------------------------------
Ran 1 test in 49.771s

OK

4、运行基准
要运行基准测试,调用fl-run-bench而不是测试运行器。你还需要选择要运行的测试用例(test_Simple.py中的方法名称)
工作台的结果将保存在一个单一的XML文件 simple-bench.xml中。这个结果文件的名称在配置文件[bench]中设置。
你可以使用命令行选项覆盖配置文件。这里我们使用-c来指定3个周期,分别运行1个,10个和20个并发用户(CU)。
root@ubuntu:/home/ruink/funkload-demo/simple# fl-run-bench -c 1:10:20 test_Simple.py Simple.test_simple
No handlers could be found for logger "FunkLoad"
========================================================================
Benching Simple.test_simple
========================================================================
Access the main URL 20 times
------------------------------------------------------------------------

Configuration
=============

* Current time: 2017-04-23T20:11:32.565587
* Configuration file: /home/ruink/funkload-demo/simple/Simple.conf
* Log xml: /home/ruink/funkload-demo/simple/simple-bench.xml
* Server: http://www.51ste.com
* Cycles: [1, 10, 20]
* Cycle duration: 10s
* Sleeptime between request: from 0.0s to 0.5s
* Sleeptime between test case: 0.01s
* Startup delay between thread: 0.01s

Benching
========

* setUpBench hook: ... done.
* Getting monitoring config from localhost: ... failed, server is down.

Cycle #0 with 1 virtual users
-----------------------------

* setUpCycle hook: ... done.
* Current time: 2017-04-23T20:11:32.585717
* Starting threads: . done.
* Logging for 10s (until 2017-04-23T20:11:42.608822):  done.
* Waiting end of threads: .. done.
* Waiting cycle sleeptime 1s: ... done.
* tearDownCycle hook: ... done.
* End of cycle, 55.41s elapsed.
* Cycle result: **SUCCESSFUL**, 0 success, 0 failure, 0 errors.

Cycle #1 with 10 virtual users
------------------------------

上一页12下一页


留言