Package: perftracker
PerfTrackerPerfTracker is a erformance tracking package designed to assist developers in monitoring and tracking the execution time of their functions. It provides a straightforward and efficient way to measure function performance, enabling you to optimize your code effectively.FeaturesEase of Use: Simply add a decorator to your functions.Flexibility: Can be used without decorator for manual tracking.Detailed Statistics: PerfTracker provides comprehensive statistics about your function's performance. It allows you to get the execution times and calculate the average calls per minute over a certain period. This information can be crucial in identifying bottlenecks in your code and optimizing it.No Dependencies: PerfTracker does not require any 3rd party packages to function.Debug Logging: Logs function execution deltas in real time.[09:48:58] DEBUG [perftracker] levelup.levelup.message_handler took 0.0017ms to complete.
[09:49:18] DEBUG [perftracker] levelup.levelup.save_cache took 0.001099ms to complete.DisclaimerThis package uses a global variable_perffor the performance model. It uses a key schema
ofmodule_name.function_nameto avoid interference when multiple packages use it for tracking.InstallationTo install PerfTracker using pip, Run the following command in your terminal:pipinstallperftrackerMethodsPerfTracker provides several methods to track and retrieve function performance data:@perf(max_entries=None): A decorator to measure and record the execution time of a function. Ifmax_entriesis set, it will limit the number of records kept for the function to this value.get_stats(): Returns the current Performance instance, which contains all recorded performance data.Performance.add(function, exe_time, max_entries=None): Adds an execution time record for a function. You can use this method to manually add execution time records.Performance.get(function): Returns the execution time records for a function. This can be useful if you want to analyze the performance data further.Performance.cpm(function, time_delta): Calculates the average calls per minute (CPM) of a function over a certain period. This can give you an idea of how frequently a function is called.Performance.avg_time(function, time_delta): Calculate the average time a function takes to execute over a certain period. This can help you identify slow functions that may need optimization.UsageUsing PerfTracker is straightforward. Here are some examples:Basic Usagefromperftrackerimportperf,get_stats@perf(max_entries=100)defmy_function():# Your code here...# Get performance statisticsstats=get_stats()Retrieving Execution Time Recordsfromperftrackerimportperf,get_stats@perf(max_entries=100)defmy_function():# Your code here...# Get performance statisticsstats=get_stats()# Retrieve execution time recordsrecords=stats.get(my_function)Calculating Calls Per Minutefromperftrackerimportperf,get_statsfromdatetimeimporttimedelta@perf(max_entries=100)defmy_function():# Your code here...# Get performance statisticsstats=get_stats()# Calculate calls per minute for the last hourcpm=stats.cpm(my_function,timedelta(hours=1))Tracking Things Manuallyfromperftrackerimportget_statsfromtimeimportperf_counterdefsome_function():stats=get_stats()start_time=perf_counter()...# Some code that takes time...end_time=perf_counter()delta=(end_time-start_time)*1000# Use milliseconds# Add time manually, the 'add' method can take strings toostats.add("my_custom_function_key",delta)Full Usagefromperftrackerimportperf,get_statsfromdatetimeimporttimedeltafromtimeimportsleepimportrandom@perf(max_entries=100)defmy_function():# Your code here...# Run the code a few timesfor_inrange(10):my_function()sleep(random.random())# Get performance statisticsstats=get_stats()# Get a list of Record objects (Record represents when a function was called)records=stats.get(my_function)# Get first time the function was calledrecord=records[0]# (float) Time this particular entry took to execute in msexecution_time=record.exe_time# (datetime.datetime) in UTC timezone naive, the time when this entry was createdtimestamp=record.timestamp# Calls per minute (float)cpm=stats.cpm(my_function)# Average execution time (float)avg_exe_time=stats.avg_time(my_function)Discord Bot Cog UsagePerfTracker can also be used in a Discord bot environment to help you track down bottlenecks in your bot's performance. Here is an example of how you can use PerfTracker in a Discord bot cog:fromdiscord.extimportcommandsfromperftrackerimportperf,get_statsclassMyCog(commands.Cog):def__init__(self,bot):self.bot=bot@perf(max_entries=100)asyncdefslow_function(self,ctx):# Your sussy code here...@commands.command()asyncdefperformance(self,ctx):stats=get_stats()records=stats.get(self.slow_function)ifrecords:avg_time=stats.avg_time(self.slow_function)awaitctx.send(f"Average execution time for slow_function:{avg_time}ms")else:awaitctx.send(f"No performance records found for{function_name}")defsetup(bot):bot.add_cog(MyCog(bot))In this example,slow_functionis a function in a Discord bot cog that is tracked by PerfTracker. Theperformancecommand can be used to retrieve the average execution time of the specific command.You can replaceslow_functionwith any function in your bot that you want to track. The performance statistics are stored in memory and will be lost when the bot is restarted.Please note that this example assumes you have basic knowledge of how to create and use cogs in a Discord bot. If you are unfamiliar with this, you can read theDiscord.py Cogs guide.Performance ComparisonPerfTracker is designed to have minimal overhead, allowing you to monitor your program's performance without significantly affecting its execution time. The following graph compares the overhead of PerfTracker with cProfile, another popular performance tracking package.During this test, PerfTracker consistently has less overhead than cProfile. The code used for this comparison can be foundHereContributingWe welcome contributions to PerfTracker. If you have a feature request, bug report, or want to improve the code, please feel free to submit a Pull Request.LicensePerfTracker is licensed under the terms of the MIT license. This means you are free to use, modify, and distribute the code, as long as you include the original copyright and license notice.