共三个文件:

1. settings.py:
#-*- coding: utf-8 -*-

SETTINGS 
= {
            
'SITE''http://xx.xxxx.com',
            
'USERS_COUNT'10,
            
'TIMES_PER_USER'1000,
            
'channels':['ring''wml''video''image''game''theme','sms''soft''read',],
            }


2.VUser.py:
#-*- coding: utf-8 -*-
import urllib2, logging, re

__author__ = "donne.cn@gmail.com"
__version__ = "0.1.0"
__copyright__ = "Copyright (c) 2008 donne.cn"
__license__ = "donne.cn"
__doc__ = '''
        模拟用户访问目标站点,可以模拟主页访问,搜索,详情,下载一连串行为
'''

logging.basicConfig(level
=logging.DEBUG,
                    format
='%(asctime)s %(levelname)s %(message)s',
                    
#filename='./FluxStatLoadTest.log',
                    filemode='w')


class VUser(object):
    
def __init__(self, url, channel = None):
        self._channels 
= {
                          
'ring''Sring.wml',   'wml''Search.wml',  'video''Svideo.wml',
                          
'image''Simage.wml''game''Sgame.wml',  'theme''Stheme.wml',
                          
'sms''Sms.wml',      'soft''Ssoft.wml',  'read''Sread.wml',
                          }
        
        self._url 
= url
        
        self._channel 
= 'Sring.wml'
        
if channel:
            self._channel 
= self._channels[channel]
        
        self.id 
= ''
        
    
def getDetailUrl(self, content):
        _url 
= re.compile('<br/><a href="(.*?)">1.').\
        search(content, 
1).group(1).split('"')[-1]               
        
return self.urlFormat(_url)
    
    
def getDownloadUrl(self, content):
        
if self._channel in ['Search.wml''Sms.wml''Sread.wml',]:
            
return ''
        _url 
= re.compile('/><a href="(.*?)">免费下载').\
        search(content).group(
1)        
        
return self.urlFormat(self._url + '/' + _url)       
        
    
def getSearchUrl(self): 
        self.id 
= self._getSessionId(self.getPage(self._url))
        
return self.urlFormat(self._url + '/' + self._channel + ';jsessionid=' + self.id + '?q=mm')
        
    
def _getSessionId(self, content):
        
return re.compile(';jsessionid=(.*?)"><').\
        search(content).group(
1)
        
    
def urlFormat(self, url):
        
return str(url).replace('&amp;''&').replace('./', self._url + '/')
        
        
    
def getPage(self, url):
        
if url:
            req 
= urllib2.Request(url)
            req.add_header(
"User-Agent""Roboo-QA-FluxLoadTester 0.1.0 by Don.Li")
            
return urllib2.urlopen(req).read()
    
    
def search(self, url):
        
return self.getDetailUrl(self.getPage(url))
    
if __name__ == '__main__':        
    user 
= VUser('http://xxx.xxxx.com', channel='theme')
    logging.info(user.id)
    
    url 
= user.getSearchUrl()
    url 
= user.getDetailUrl(user.getPage(url))
    logging.info(
"Detail URL: "+ url)
    
    url 
= user.getDownloadUrl(user.getPage(url))
    logging.info(
"Download URL: " + url)
    user.getPage(url)

3. scenices.py:
#-*- coding: utf-8 -*-
import logging, threading
from time import sleep
from VUser import VUser
from settings import SETTINGS

__author__ = "donne.cn@gmail.com"
__version__ = "0.1.0"
__copyright__ = "Copyright (c) 2008 donne.cn"
__license__ = "donne.cn"
__doc__ = '''
        根据参数控制虚拟用户的行为
'''

logging.basicConfig(level
=logging.DEBUG,
                    format
='%(asctime)s %(levelname)s %(message)s',
                    
#filename='./FluxStatLoadTest.log',
                    filemode='w')

class threadUser(threading.Thread):
    
def __init__(self, name, channel = None):  
        self._name 
= name      
        self._user 
= VUser(SETTINGS['SITE'], channel)
        self.vp 
= 0
        threading.Thread.
__init__(self, name = name)
        
    
def run(self):
        
for i in range(SETTINGS['TIMES_PER_USER']):
            _url 
= self._user.getSearchUrl()
            
            _url 
= self._user.getDetailUrl(self._user.getPage(_url))
            logging.info(
"Detail URL: "+ _url)
            sleep(
3)
            
            _url 
= self._user.getDownloadUrl(self._user.getPage(_url))
            logging.info(
"Download URL: " + _url)
            sleep(
3)
            
            self._user.getPage(_url)
            
            self.vp 
+= 4
            logging.info(
"The User " + self._name + " VP is: " + str(self.vp) + ' Now.')
            
            
            
if __name__ == '__main__':
    users 
= []
    
for i in range(9):
        channel 
= SETTINGS['channels'][i]
        
for j in range(SETTINGS['USERS_COUNT']):
            user 
= threadUser(channel + ' Thread ' + str(j + 1), channel = channel)
            users.append(user)
            
    
for user in users:
        user.start()
        
    
for user in users:
        user.join()


修改settings.py里的参数,运行scenices.py, 可以实现指定类型的指定数目的虚拟用户,并发访问目标网站,生成可以预估的log文件,对网站log生成功能实现负载测试,以期发现其中缺陷。