charlesbaynham / wurwolves

2 stars 0 forks source link

State rendering speed #126

Closed charlesbaynham closed 3 years ago

charlesbaynham commented 3 years ago

Continuing from #118

charlesbaynham commented 3 years ago

Stepping manually through the state rendering, I counted the database calls. test_backend:test_single_render counts

WARNING  root:test_backend.py:137 total_time = 1.630s of which 1.227s in 40 DB calls
charlesbaynham commented 3 years ago

I count, for the same n_player = 10 player game with n_msg = 4 messages (including startup message):

1 game select 1 player selects n_player user selects 1 message select n_msg player selects 1 actions query (this isn't needed) 1 player select 1 actions select n_players n_players player selects n_players n_actions selects

For a total of 6 + n_msg + n_player * (1 + n_player + n_actions) which is 6+4+10*(15) = 160 queries. Hmm I guess some caching must be happening already, since it's not that bad.

charlesbaynham commented 3 years ago

Regardless, there's a tonne of redundancy here. I should try and get SQLAlchemy to do the work for me and to reuse objects already loaded into the session.

charlesbaynham commented 3 years ago

Down from 40 to 23 in cc1b7b0

WARNING  root:test_backend.py:140 total_time = 1.193s of which 0.809s in 23 DB calls
charlesbaynham commented 3 years ago

Now down to 1 database call in state rendering:

WARNING  root:test_backend.py:138 total_time = 0.064s
WARNING  root:test_backend.py:140 total_time = 0.064s of which 0.003s in 1 DB calls
charlesbaynham commented 3 years ago

Using a real AWS database in 0d4db09:

WARNING  root:test_backend.py:83 time_per_join = 0.337s of which 0.281s in 11.5 DB calls
WARNING  root:test_backend.py:88 time_per_render = 0.101s of which 0.052s in 1.0 DB calls

That'll do for now: that should provide a pretty huge speedup in large games.

We've gone from https://github.com/charlesbaynham/wurwolves/issues/111 showing mean state renders of ~6000ms, and https://github.com/charlesbaynham/wurwolves/pull/118#issuecomment-761795267 showing 2700ms in a testing environment, to the current 101ms in a 10 player game. I think that's enough to mark #111 closed.