python构建一个脚本分析器,可自动检测死循环和资源占用。

以下是Python实现的脚本分析器,可以检测死循环和资源占用情况:


import ast

import sys

import time

import resource

from threading import Thread


class ScriptAnalyzer:

    def __init__(self, script_path):

        self.script_path = script_path

        self.max_memory = 0

        self.max_cpu = 0

        self.timeout = 10  # 默认超时时间(秒)

        

    def detect_infinite_loop(self, node):

        """检测AST中的潜在死循环"""

        for child in ast.walk(node):

            if isinstance(child, ast.While):

                if not child.test or isinstance(child.test, ast.Constant) and child.test.value:

                    return True

        return False

    

    def analyze_resource_usage(self):

        """分析资源使用情况"""

        start_time = time.time()

        process = Thread(target=self._run_script)

        process.start()

        

        while process.is_alive():

            if time.time() - start_time > self.timeout:

                process.join(0)

                return "Timeout: Possible infinite loop"

            

            mem = resource.getrusage(resource.RUSAGE_SELF).ru_maxrss

            cpu = resource.getrusage(resource.RUSAGE_SELF).ru_utime

            self.max_memory = max(self.max_memory, mem)

            self.max_cpu = max(self.max_cpu, cpu)

            

        return None

    

    def _run_script(self):

        """执行目标脚本"""

        with open(self.script_path) as f:

            code = f.read()

        exec(code, {'__name__': '__main__'})

    

    def analyze(self):

        """主分析方法"""

        with open(self.script_path) as f:

            tree = ast.parse(f.read())

        

        if self.detect_infinite_loop(tree):

            return "Warning: Potential infinite loop detected"

        

        result = self.analyze_resource_usage()

        if result:

            return result

            

        return f"Analysis complete. Max memory: {self.max_memory} KB, Max CPU time: {self.max_cpu}s"


if __name__ == "__main__":

    if len(sys.argv) < 2:

        print("Usage: python script_analyzer.py ")

        sys.exit(1)

    

    analyzer = ScriptAnalyzer(sys.argv[1])

    print(analyzer.analyze())

这个分析器通过AST解析检测潜在死循环,并监控内存和CPU使用情况。使用时需传入要分析的脚本路径,它会返回资源使用报告或潜在问题警告。

http://www.thedesignrepublic.com/

http://www.yxhgq.com/

http://www.jianhuotech.com/

http://www.bjyozd.com

http://www.sunny-way.cn/

http://www.cnsenkai.com/

http://www.zluren.com.cn/

http://www.cddtk119.com/

http://www.boao168.com.cn/

http://www.czs365.net/

http://www.ianvan.com/

http://www.fangzhoushidai.com

http://www.qiaosen-kj.com

http://gzhyxjj.cn

http://xatlzg.com

http://www.doushiwang.cn

http://www.sxyuanzheng.com

http://www.jwdyd.com

http://www.xaminglang.com

http://www.lboneti.cn

http://www.ahlongda.com

http://www.gdlanye.com

http://www.ktlcutter.com

http://www.hbzfjn.com

http://www.shbaimule.com

http://www.clgzkj.com

http://www.fjjhwy.cn

http://www.goodluck-lift.com

http://www.storlead.shop

http://magicians.com.cn

http://mall.yeepayer.com

http://tzkqzj.com

http://www.jinfanweilai.com

https://www.durkflex.cn/

http://www.szfmhj168.com.cn/

http://hfnz.ahaiba.com/

http://sjmzjypx010.ahaiba.com/

http://nyh.ahaiba.com/

http://thszsgs.com/

https://www.durkflex.cn/

http://www.szfmhj168.com.cn/

http://hfnz.ahaiba.com/

http://sjmzjypx010.ahaiba.com/

http://nyh.ahaiba.com/

http://thszsgs.com/

http://www.mataicomp.com/

http://www.mattei.com.cn/

http://wssyjt.com/

http://www.hbws.cn/

http://wesddc.com/

http://www.shtddaiyun.com/

http://qsyfzl.com/

请使用浏览器的分享功能分享到微信等