需要技巧来提高 Python 程序的性能

时间:2021-03-31 04:51:04

标签: python performance pyrad

我正在用 python 创建一个模拟器工具,它将使用 pyrad 模块发送半径请求。

但我面临一些性能问题,性能只有 1500 tps。 请分享一些想法/建议,以提高模拟器的性能。

class MyThread(threading.Thread):

    def __init__(self, auth_obj):
        threading.Thread.__init__(self)
        self.auth_obj = auth_obj

    def run(self):
        self.auth_obj.create_threads()


class Authenticator:
    def __init__(self):
        self.iterations = 0
        self.concurrency = 0
        self.secret_pwd = ""
        self.server_ip = ""

        self.read_xml()

        self.user_idx_list = range(self.concurrency)
        if self.concurrency > MAX_CONC_THREADS:
            self.concurrency = MAX_CONC_THREADS

        print("Main:Starting Time: {}".format((datetime.now()).strftime("%B %d, %H:%M:%S %Y")))

        # Using Threads
        thread_list = list()

        for index in range(self.iterations):
            thread = MyThread(self)
            thread_list.append(thread)
            thread.start()
            time.sleep(1)

            if index % PRINT_OUTPUT_SEC == 0:
                print_table_output()

        for cur_t in thread_list:
            cur_t.join()

        print_table_output()
        str_out = " Final Stats "
        print(str_out.center(63, '#'))
        input("Load Successfully Sent...")


    def send_conc_request(self, username_idx):
        TotalReq.increment()
        username = user_list[username_idx % LIST_SIZE]

        srv = Client(server=self.server_ip, secret=bytes(self.secret_pwd, encoding="ascii"),
                     dict=dict_obj)

        # create request
        req = srv.CreateAuthPacket(code=pyrad.packet.AccessRequest, User_Name=username,
                                   NAS_Identifier="Creator-VM", NAS_IP_Address="10.212.10.211")

        req["User-Password"] = req.PwCrypt("test")

        reply = srv.SendPacket(req)

        AuthReqSent.increment()

        if reply is None:
            AuthReqError.increment()
            return

        if reply.code == pyrad.packet.AccessAccept:
            AuthReqAccept.increment()
            return

        if reply.code == pyrad.packet.AccessTimeout:
            AuthReqTimeout.increment()
            return

        AuthReqReject.increment()
        return

    def create_threads(self):
        try:
            with ThreadPoolExecutor(max_workers=self.concurrency) as executor:
                executor.map(self.send_conc_request, self.user_idx_list, timeout=12)

        except Exception as exc:
            print("Unable to Create Threads Broken ThreadPool", exc)

1 个答案:

答案 0 :(得分:0)

首先,线程对 CPU 密集型进程没有太大帮助。我认为多处理可以更有效。 为了解决您的问题,我建议采用“分而治之”的方法,如果您将代码拆分成更小的部分,您可以衡量每个部分的性能并查看任何一段代码的有效性。有很多工具可以帮助你,比如 profiler、line_profiler 或 perf_tool(我是作者),它们就是为解决这类问题而生的