TimeGuardianは、機能の実行時間とメモリの使用を測定およびログに記録するために設計されたPythonパッケージです。このパッケージは、Pythonアプリケーションのパフォーマンス監視と最適化に特に役立ちます。
TimeGuardianをPIPから直接インストールします。
pip install timeguardianまたは、ソースコードからインストールしてください。
cd path/to/TimeGuardian
pip install . timeguardian PackageからTimeGuardianをインポートし、機能の装飾器として使用して、実行時間とメモリ使用量を測定します。
from timeguardian import TimeGuardian
# To measure execution time and/or memory usage
@ TimeGuardian . measure
def my_function ():
# function implementation
# Custom name logging
@ TimeGuardian . measure ( name = "CustomName" )
def another_function ():
# function implementation
# Custom logging with name, time and memory
@ TimeGuardian . measure ( name = "CustomName" , elapsed = True , memory = True ) #(elapsed in ms, memory in bytes)
def another_function ():
# function implementation条件付きロギングでパフォーマンスを監視します:
from timeguardian import TimeGuardian
# Monitor and log only if execution time or memory usage exceeds the specified limits (time in ms, memory in bytes)
@ TimeGuardian . monitor ( elapsed = 200 , memory = 1024 )
def monitored_function ():
# function implementation TimeGuardianパッケージを使用すると、時間とメモリの測定のためにグローバルユニットを設定できます。この機能を使用すると、実行時間とメモリの使用方法の報告方法をカスタマイズできます。
ユニットを設定します: TimeGuardian.set_time_unit(unit)およびTimeGuardian.set_memory_unit(unit)を使用して、時間とメモリ測定のためにグローバルユニットを設定します。 unitパラメーターは、目的のユニットを示す文字列でなければなりません(例:ミリ秒の「MS」、「s」は「秒」、バイトのバイト、キロバイトのkb '、メガバイトの「mb」)です。
関数の飾り: @TimeGuardian.measure Decoratorを機能に適用します。このデコレーターは、グローバルに設定されたユニットを使用して、実行時間とメモリ使用量を測定およびログに記録します。
from decorators import TimeGuardian
# Set the units for time and memory measurements
TimeGuardian . set_time_unit ( 'ms' ) # Set time unit to milliseconds
TimeGuardian . set_memory_unit ( 'MB' ) # Set memory unit to megabytes
@ TimeGuardian . measure ( elapsed = True , memory = True , name = "Sample Function" )
def sample_function ():
# Function implementation
...
# Call the decorated function
sample_function ()この例では、 sample_functionが測定され、時間のためにミリ秒で記録され、メモリ使用量のためにメガバイトが記録されます。
TimeGuardianへの貢献は大歓迎です!
このプロジェクトは、MITライセンスに基づいてライセンスされています。詳細については、ライセンスファイルを参照してください。