jmeter简单实践(九)
简单版本的httpserver
json模块可能需要下载,详细方法请百度,增加个人的能力
主要是json的,接收到非json的http请求,返回"415, "Only json data is supported."
coding: utf-8
from http.server import BaseHTTPRequestHandler,HTTPServer
import cgi
import json
def Cjson(datas):
try:
messages = json.loads(datas)
except ValueError:
return False
return True
class TodoHandler(BaseHTTPRequestHandler):
"""A simple TODO server
which can display and manage todos for you.
"""
# Global instance to store todos. You should use a database in reality.
TODOS = []
def do_GET(self):
# return all todos
if self.path != '/':
self.send_error(404, "File not found.")
return
# Just dump data to json, and return it
message = json.dumps(self.TODOS)
self.send_response(200)
self.send_header('Content-type', 'application/json')
self.end_headers()
self.wfile.write(bytes(message,'utf-8'))
def do_POST(self):
"""Add a new todo
Only json data is supported, otherwise send a 415 response back.
Append new todo to class variable, and it will be displayed
in following get request
"""
ctype, pdict = cgi.parse_header(self.headers['content-type'])
if ctype == 'application/json':
length = int(self.headers['content-length'])
dd = self.rfile.read(length)
post_values =Cjson(str(dd,'UTF-8'))
if post_values == False:
print(dd)
self.TODOS.append(str(dd, 'UTF-8'))
self.send_response(200)
self.send_header(b'Content-type', b'application/json')
self.end_headers()
jss = {
"code": 200,
"msg": "OK",
}
r = json.dumps(jss)
self.wfile.write(bytes(r, 'utf-8'))
if post_values == True:
self.TODOS.append(json.loads(str(dd,'utf-8')))
self.send_response(200)
self.send_header(b'Content-type', b'application/json')
self.end_headers()
jss = {
"code": 200,
"msg": "OK",
}
r = json.dumps(jss)
self.wfile.write(bytes(r, 'utf-8'))
else:
length = int(self.headers['content-length'])
post_values = self.rfile.read(length)
self.send_error(415, "Only json data is supported.")
return
if __name__ == '__main__':
# Start a simple server, and loop forever
server = HTTPServer(('localhost', 8888), TodoHandler)
print("Starting server, use <Ctrl-C> to stop")
server.serve_forever()
jmeter实践开始
1.在本地运行,注意不要将线程设置太大,机器抗不住,只能重启电脑了
2.主要用户简单的实战,看看自己的成果
-
运行上面的代码
-
创建线程组
- Number of threads 1
- Ramp-up Period 1
- forever 1

- 创建http请求(第一种)
- 发送表单
- 内容看下面截图

-
创建http 请求头
- 'Content-type' 'application/json'
- 内容看下图
创建聚合报告

查看结果树

创建汇总报告

开始执行
post的结果

疑问解答QQ群:群1:588402570,群2 772588688
群1 限制人数后,请申请群2
关注该公众号:持续更新Jmeter相关内容


浙公网安备 33010602011771号