码迷,mamicode.com
首页 > 其他好文 > 详细

jmeter简单实践(九)

时间:2018-11-10 17:51:31      阅读:160      评论:0      收藏:0      [点我收藏+]

标签:parse   not found   acl   dict   dump   follow   utf-8   json模块   线程   

简单版本的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.主要用户简单的实战,看看自己的成果

  1. 运行上面的代码

  2. 创建线程组

  • Number of threads 1
  • Ramp-up Period 1
  • forever 1

技术分享图片

 

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

技术分享图片

 

  1. 创建http 请求头

    • ‘Content-type‘ ‘application/json‘
    • 内容看下图
    • 技术分享图片

       

 

    创建聚合报告

技术分享图片

 

 

    查看结果树

 技术分享图片

 

 

   创建汇总报告

 技术分享图片

 

 

 

    开始执行

 

 

post的结果

技术分享图片

疑问解答QQ群:群1:588402570,群2 772588688

 

群1 限制人数后,请申请群2

 

关注该公众号:持续更新Jmeter相关内容

 

技术分享图片

 

jmeter简单实践(九)

标签:parse   not found   acl   dict   dump   follow   utf-8   json模块   线程   

原文地址:https://www.cnblogs.com/xiaoxiao-niao/p/9939749.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!