Skip to content

模块导入有性能问题 #17

Open
@callmexss

Description

模块的导入时间大概有1.2s,每次程序运行的时候都有一个很明显的延迟,上面两次是 import general agent,后面两次是注释掉后的结果。

import click
import rich

# from aicli.agent import agent


@click.command()
@click.argument("query", required=False)
def cli(query):
    if not query:
        print(">>> :")
        query = input()

    # result = agent.user_input(query)
    # print("\n" * 3)
    # rich.print(result)
    print(query)


if __name__ == "__main__":
    cli()

image

下面是分析 import 时间的一些数据,看起来问题主要出在 llama_index 上面。

aicli(base) ➜  aicli git:(master) python -X importtime -m aicli.cli hi > output.log 2>&1  
aicli(base) ➜  aicli git:(master) ✗ sort -k5 -nr output.log | head -n 20
import time: self [us] | cumulative | imported package
import time:       110 |    1518184 | aicli
import time:      2076 |    1518075 |   aicli.cli
import time:      2446 |    1495640 |     aicli.agent
import time:       525 |    1481713 |       GeneralAgent
import time:       509 |     871396 |         GeneralAgent.agent.agent
import time:       380 |     865092 |           GeneralAgent.interpreter
import time:       230 |     844560 |             GeneralAgent.interpreter.knowledge_interpreter
import time:      1614 |     844330 |               GeneralAgent.llamaindex
import time:       673 |     842258 |                 llama_index.core
import time:      2752 |     609793 |         GeneralAgent.skills
import time:       391 |     583731 |                   llama_index.core.indices
import time:       801 |     431713 |           openai
import time:       308 |     323907 |                     llama_index.core.indices.loading
import time:       343 |     323600 |                       llama_index.core.indices.registry
import time:       276 |     304846 |                         llama_index.core.indices.multi_modal
import time:       762 |     304571 |                           llama_index.core.indices.multi_modal.base
import time:      1745 |     299267 |             openai.types
import time:        77 |     276197 |                             llama_index.core.query_engine.multi_modal
import time:      1068 |     276121 |                               llama_index.core.query_engine
import time:      1289 |     259064 |               openai.types.batch

单独执行导入语句:

aicli(base) ➜  aicli git:(master) time python -X importtime -c "from GeneralAgent import Agent" > output.log 2>&1
python3 -X importtime -c "from GeneralAgent import Agent" > output.log 2>&1  1.37s user 1.12s system 207% cpu 1.194 total
aicli(base) ➜  aicli git:(master) time python -X importtime -c "from GeneralAgent import Agent" > output.log 2>&1
python3 -X importtime -c "from GeneralAgent import Agent" > output.log 2>&1  1.40s user 1.17s system 222% cpu 1.154 total
aicli(base) ➜  aicli git:(master) sort -k5 -nr output.log | head -n 20
import time:       147 |     954636 | GeneralAgent
import time:       262 |     585005 |   GeneralAgent.agent.agent
import time:       249 |     581260 |     GeneralAgent.interpreter
import time:       120 |     565159 |       GeneralAgent.interpreter.knowledge_interpreter
import time:      1529 |     565040 |         GeneralAgent.llamaindex
import time:       358 |     563310 |           llama_index.core
import time:       140 |     400637 |             llama_index.core.indices
import time:      1080 |     369486 |   GeneralAgent.skills
import time:       511 |     287941 |     openai
import time:       110 |     210006 |               llama_index.core.indices.loading
import time:       114 |     209896 |                 llama_index.core.indices.registry
import time:      1014 |     209036 |       openai.types
import time:        92 |     198461 |                   llama_index.core.indices.multi_modal
import time:       396 |     198370 |                     llama_index.core.indices.multi_modal.base
import time:        13 |     185893 |               llama_index.core.indices.composability.graph
import time:       114 |     185881 |                 llama_index.core.indices.composability
import time:       374 |     185767 |                   llama_index.core.indices.composability.graph
import time:        14 |     182897 |                       llama_index.core.query_engine.multi_modal
import time:       630 |     182883 |                         llama_index.core.query_engine
import time:       763 |     182732 |         openai.types.batch

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions