Skip to content

Commit

Permalink
Added intermitent profiling (#541)
Browse files Browse the repository at this point in the history
* Added profiling

* Don't collect data at peak times and cleanup after 200 samples have been collected

* Cleanup profiler after we've collected the data we need.
  • Loading branch information
Askaholic committed Mar 18, 2020
1 parent 3283c89 commit 86ad4bb
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
28 changes: 28 additions & 0 deletions server.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
from server.stats.game_stats_service import (
AchievementService, EventService, GameStatsService
)
from server.timing import at_interval

if __name__ == '__main__':
args = docopt(__doc__, version='FAF Server')
Expand Down Expand Up @@ -74,6 +75,33 @@ def signal_handler(_sig, _frame):

players_online = PlayerService(database)

if config.PROFILING_INTERVAL > 0:
logger.warning("Profiling enabled! This will create additional load.")
import cProfile
pr = cProfile.Profile()
profiled_count = 0
max_count = 300

@at_interval(config.PROFILING_INTERVAL, loop=loop)
async def run_profiler():
global profiled_count
global pr

if len(players_online) > 1000:
return
elif profiled_count >= max_count:
pr = None
return

logger.info("Starting profiler")
pr.enable()
await asyncio.sleep(2)
pr.disable()
profiled_count += 1

logging.info("Done profiling %i/%i", profiled_count, max_count)
pr.dump_stats("profile.txt")

twilio_nts = None
if TWILIO_ACCOUNT_SID:
twilio_nts = TwilioNTS()
Expand Down
1 change: 1 addition & 0 deletions server/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

# Environment
LOG_LEVEL = logging.getLevelName(os.getenv('LOG_LEVEL', 'DEBUG'))
PROFILING_INTERVAL = int(os.getenv('PROFILING_INTERVAL', -1))

# Credit to Axle for parameter changes, see: http://forums.faforever.com/viewtopic.php?f=45&t=11698#p119599
# Optimum values for ladder here, using them for global as well.
Expand Down

0 comments on commit 86ad4bb

Please sign in to comment.