zoukankan      html  css  js  c++  java
  • python nginx不同参数压测脚本

    Server:

     1 from flask import Flask, request, jsonify
     2 import json
     3 import re
     4 import os
     5 import subprocess
     6 
     7 conf_file_src = '/etc/nginx/nginx.conf'
     8 restart_shell = 'sudo service nginx restart'
     9 
    10 app = Flask(__name__)
    11 
    12 # 重置conf文件
    13 # conf文件中要测试的值置为 ###arg###
    14 def reset_conf(file_name, data):
    15     # 打开模板文件
    16     file = open(file_name, 'r')
    17     file_temp = file.read()
    18     for key in data:
    19         # 替换要调试的参数
    20         re_model = re.compile(r'###')
    21         args = re.findall(r'###S' + key + '###', file_temp)
    22 
    23         for arg in args:
    24             print(arg)
    25             file_temp = file_temp.replace(arg, data[key], 100)
    26 
    27     # 写入新的配置文件
    28     file_conf = open(conf_file_src, 'w+')
    29     file_conf.write(file_temp)
    30     file_conf.close()
    31 
    32 
    33 # 重启nginx服务
    34 def reset_service():
    35     # 重启service
    36     res = os.popen(restart_shell).readline()
    37     return 'Result: ' + res
    38 
    39 # url : xxx.xxx.xxx.xxx:5000/restart
    40 # 参数:
    41 # data: {'arg1':'v1','arg2':'v1']}
    42 # 返回:转发执行结果
    43 @app.route('/restart', methods=['POST'])
    44 def restart():
    45     data = json.loads(request.get_data(as_text=True))
    46     print(data)
    47     reset_conf("nginx.conf.template", data)
    48     result = reset_service()
    49     return result
    50 
    51 if __name__ == '__main__':
    52     app.run()
    View Code

    Client:

     1 import requests
     2 import json
     3 
     4 url = "http://127.0.0.1:5000/restart"
     5 field = 'sendfile'
     6 data = ["on", "off"]
     7 
     8 def reset():
     9     for s in data:
    10         #发送请求修改nginx配置文件
    11         json = '{"' + field +'":"' + s +'"}'
    12         res = requests.post(url=url, data='{"sendfile":"on"}')
    13         print(json)
    14         print(res)
    15         print(res.content)
    16 
    17         #压力测试
    18         # wrk ...
    19 
    20 if __name__ == '__main__':
    21     reset()
    View Code
  • 相关阅读:
    Maven 环境的配置
    zTree的简单例子
    plsql免安装客户端的配置
    HDU 1232 畅通工程
    HDU 5698 瞬间移动
    Codeforces 1015E1 Stars Drawing (Easy Edition)
    Codeforces 784B Santa Claus and Keyboard Check
    Codeforces 500C New Year Book Reading
    NSarray 赋值 拷贝 等问题记录
    UINavigationController 操作记录
  • 原文地址:https://www.cnblogs.com/HadesBlog/p/13388434.html
Copyright © 2011-2022 走看看