3python实现性能测试脚本

python实现性能测试脚本

SUSI:单用户,但迭代,用于调试代码基本功能

SUMI:单用户,多迭代,用于调试数据重复问题

MUSI:多用户,但迭代,用于调试多线程并发情况

MUMI:多用户,多迭代,用于正式的性能测试执行

woniusales_test.py

import random

import threading

import time

import requests

import re

from 性能测试.common import query_db

#用来临时保存浏览器加载资源url_list = []

#保存响应时间time_li = []

#保存响应大小response_li = []

class DEMO:

def __init__(self):

self.session = requests.session()

#获取首页-页面资源    def get_homepage(self):

        t1 = int(time.time()*1000)

        url = "http://localhost:8080/woniusales/"        res = self.session.get(url=url)

        js_li = re.findall('type="text/javascript" src="(.+?)"',res.text)

        img_li = re.findall('img src="(.+?)"',res.text)

        backage_img = re.findall('background-image:url\(\'(.+?)\'\)',res.text)

        uri_li = js_li+img_li+backage_img

for uri in uri_li:

            url_i = "http://localhost:8080" + uri

if url_i not in url_list:

self.session.get(url=url_i)

                url_list.append(url_i)

        t2 = int(time.time()*1000)

        time_li.append(t2-t1)

print(f'当前首页获取的响应时间为{t2-t1}ms')

# print(res.headers,type(res.headers))        print(f'当前首页获取的响应大小为{len(res.text+str(res.headers))/1024}KB')

        response_li.append(len(res.text+str(res.headers))/1024)

if "蜗牛进销存-首页" in res.text:

print("homepage is ok")

else:

print("homepage is ko")

#登录    def login(self,username,password):

        t1 = int(time.time() * 1000)

        url = "http://localhost:8080/woniusales/user/login"        body = {

"username":username,

"password":password,

"verifycode":"0000"        }

        res = self.session.post(url=url,data=body)

print(res.text)

        t2 = int(time.time() * 1000)

        time_li.append(t2 - t1)

print(f'登录的响应时间为{t2-t1}ms')

print(f'当前首页获取的响应大小为{len(res.text + str(res.headers)) / 1024}KB')

        response_li.append(len(res.text + str(res.headers)) / 1024)

if "login-pass" in res.text:

print("login is ok!")

else:

print("login is ko!")

#进入会员管理模块    def get_custormer(self):

        t1 = int(time.time() * 1000)

        url = "http://localhost:8080/woniusales/customer"        res = self.session.get(url = url)

        t2 = int(time.time() * 1000)

        time_li.append(t2 - t1)

print(f'进入会员管理页面的响应时间为{t2-t1}ms')

print(f'当前首页获取的响应大小为{len(res.text + str(res.headers)) / 1024}KB')

        response_li.append(len(res.text + str(res.headers)) / 1024)

if "蜗牛进销存-会员管理" in res.text:

print("custormer is ok")

else:

print("custormer is ko")

#新增会员    def add_customer(self):

        t1 = int(time.time() * 1000)

        url = "http://localhost:8080/woniusales/customer/add"        phone_num = ['130','150','180','170','190']

        custormername_li = ['小红','小黄','小绿','小青','小白']

        childsex_li = ['男','女']

        customerphone = random.choice(phone_num)+str(random.randint(11111111,99999999))

        childsex = random.choice(childsex_li)

        custormername = random.choice(custormername_li)

        body = {

"customername":custormername,

"customerphone":customerphone,

"childsex":childsex,

"childdate":"2021-06-22",

"creditkids":"0",

"creditcloth":"0"        }

        res = self.session.post(url=url,data=body)

print(res.text)

        t2 = int(time.time() * 1000)

        time_li.append(t2-t1)

print(f'新增会员的响应时间为{t2-t1}ms')

print(f'当前首页获取的响应大小为{len(res.text + str(res.headers)) / 1024}KB')

        response_li.append(len(res.text + str(res.headers)) / 1024)

#查询会员    def query_custormer(self):

        t1 = int(time.time() * 1000)

        url = "http://localhost:8080/woniusales/customer/search"        body = {

"customerphone": "18088880000",

"page": "1"        }

        res = self.session.post(url=url,data=body)

print(res.json())

        t2 = int(time.time() * 1000)

        time_li.append(t2 - t1)

print(f'查询会员的响应时间为{t2-t1}ms')

print(f'当前首页获取的响应大小为{len(res.text + str(res.headers)) / 1024}KB')

        response_li.append(len(res.text + str(res.headers)) / 1024)

#退出登录    def loginout(self):

        url = "http://localhost:8080/woniusales/user/logout"        self.session.get(url = url)

#主调方法    def main_test(self,username,password):

for i in range(3):

self.get_homepage()

# 加入思考时间            time.sleep(random.randint(1,3))

self.login(username,password)

# 加入思考时间            time.sleep(1)

self.get_custormer()

# 加入思考时间            time.sleep(5)

self.add_customer()

# 加入思考时间            time.sleep(1)

self.query_custormer()

# 加入思考时间            time.sleep(1)

self.loginout()

if __name__ == '__main__':

    WS = DEMO()

    tup = query_db()

for i in tup:

        username = i[0]

        password = i[1]

        t = threading.Thread(target=WS.main_test,args=((username,password)))

        t.start()

    t.join(timeout=500)

# WS.main_test(1,2)    print(time_li)

print(response_li)

common.py

# -*- coding: utf-8 -*-# 版本  :1.0# 当前编辑器名  :PyCharm# 作者   :XUXIAOBING# 当前编辑文件名 :common# 创建时间  :2021/6/22 17:38import random

import pymysql

#数据库新增def insert_db():

    con = pymysql.connect(host='localhost',user='root',password='123456',database='woniusales',port=3307,charset='utf8')

    cur =con.cursor()

for i in range(10):

        username = random.choice(['xiaohong','xiaowang','xiaofang','zhangsan','lisi'])+str(random.randint(1,100))

        password = random.choice(['xiaohong','xiaowang','xiaofang','zhangsan','lisi'])+str(random.randint(1,100))

        realname = random.choice(['xiaohong','xiaowang','xiaofang','zhangsan','lisi'])+str(random.randint(1,100))

        phone = random.choice(['130','140','170','180','190'])+str(random.randint(11111111,99999999))

        role = random.choice(['boss','clerk','admin'])

        cur.execute(f"insert into user(username,password,realname,phone,role,createtime) values('{username}','{password}',"                    f"'{realname}','{phone}','{role}','2017-10-01 08:18:20')")

    con.commit()

    cur.close()

    con.close()

#数据库查询def query_db():

    con = pymysql.connect(host='localhost',user='root',password='123456',database='woniusales',port=3307,charset='utf8')

    cur =con.cursor()

    cur.execute('select username,password from user')

    tup = cur.fetchall()

# print(tup)    con.commit()

    cur.close()

    con.close()

return tup

if __name__ == '__main__':

# insert_db()    query_db()

使用pstuil第三方的测试库监控服务器系统资源

pip install psutil

使用:

import psutil

#跟cpu相关的一些方法print(psutil.cpu_count())

print(psutil.cpu_freq())

print(psutil.cpu_percent())

print(psutil.cpu_stats())

#跟内存相关的方法

print(psutil.virtual_memory().percent)

#跟disk相关的方法

print(psutil.disk_usage('F:'))

#跟network相关的方法

print(psutil.net_io_counters())

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值