美文网首页
python_记录程序运行时间的工具

python_记录程序运行时间的工具

作者: Frank_8942 | 来源:发表于2019-06-21 16:39 被阅读0次
#!/usr/bin/python
# -*- coding: UTF-8 -*-

import time
import logging
import traceback

# logging.basicConfig(level=logging.INFO, filename="{}_log.txt".format(__file__[:-3]), filemode="w", format='%(asctime)s-%(filename)s[line:%(lineno)d]-%(levelname)s: %(message)s')

def init_logging(the_level=logging.INFO, the_filename="{}_log.txt".format(__file__[:-3]), the_filemode="a", the_format='%(asctime)s-%(filename)s[line:%(lineno)d]-%(levelname)s: %(message)s'):
    logging.basicConfig(level = the_level, filename= the_filename, filemode=the_filemode, format=the_format)

def record_time(fun_name):
    """记录程序执行时间工具"""
    def wrapper(fun):
        def inner(*fun_params):
            run_flag = False  # 标记作业是否正常执行
            start_time = time.time()
            logging.info("程序运行时间:{}  执行函数:{} ".format(time.strftime("%Y%m%d %H:%M:%S", time.localtime(start_time)), fun_name))
            try:
                fun(*fun_params)
                run_flag = True
            finally:
                run_result = "成功" if run_flag else "失败"
                logging.info("执行函数{}:   {}".format(fun_name, run_result) )

                # 统计单个作业运行时长
                end_time = time.time()
                run_time = end_time - start_time
                logging.info("开始时间:{}  结束时间:{}  耗时:{:.0f} 秒".format(time.strftime("%Y%m%d %H:%M:%S", time.localtime(start_time)),
                                                     time.strftime("%Y%m%d %H:%M:%S", time.localtime(end_time)), run_time))

        return inner
    return wrapper


@record_time("extract_data")
def test_fun():
    for i in range(5):
        logging.info("this is info message")
        time.sleep(1)
        if i == 1:
            1/0


if __name__ == '__main__':
    try:
        init_logging()
        test_fun()
    except Exception as e:
        logging.error("\033[35m  bug message:\n{}\n".format(traceback.format_exc()))

相关文章

  • python_记录程序运行时间的工具

  • 2018-05-18

    关于装饰器记录程序运行时间以及记录运行次数的代码 import time # def set_fun(func):...

  • 记录程序运行时间

    参考 https://www.pyget.cn/p/186930 因为我在写一个分布式系统的作业,要求计算运行时间...

  • Python笔记——记录程序运行时间

    记录一个python中的小模块 在python程序中如何记录程序运行的时间呢? 可以借助python中的time模...

  • SAS宏程序:通过CMD批量获取文件的修改时间

    公司要求QC侧程序的运行要在Source侧之后,目前没有专门的工具检查程序运行时间,只能人工核查。但人工核查不仅繁...

  • 各种调优工具

    各种调优工具 总结: 三类工具 基础工具 (NSLog的方式记录运行时间.) 性能工具.检测各个部分的性能表现,找...

  • iOS性能优化

    三类工具 基础工具(NSLog的方式记录运行时间) 性能工具,检测各个部分的性能表现,找出性能瓶颈 内存工具,检查...

  • C++项目:日志模块

    简介 日志:记录程序日常运行状态。按条记录,记录内容包括:时间、模块、日志级别(致命、出错、警告、信息、调试)、输...

  • 1.1Java基础知识

    JVM:JAVA虚拟机。是java程序的运行环境。 JDK:JAVA程序开发工具包。 JRE:是java程序的运行...

  • nodejs中的串行无关联和并行无关联

    //1:引入需要的模块var async = require("async");//为了记录这段程序运行的时间co...

网友评论

      本文标题:python_记录程序运行时间的工具

      本文链接:https://www.haomeiwen.com/subject/eshuqctx.html