edurange / edurange-server

cloud-based interactive security exercises
https://www.edurange.org
MIT License
14 stars 14 forks source link

Slow page loads for admin scenario list #38

Closed newjam closed 5 years ago

newjam commented 6 years ago

This is likely an issue with the SQL queries generated by the Object Relation Mapper, ActiveRecord. A common problem is the "N+1 query" problem where 1 query is used to select the list of N scenarios, then N more queries are executed to select details for each scenario. edurange-server logs each SQL query, so determine what queries is being executed for this page and then figure out what changes can be made to our object model to convince the ORM to make a sensible query.

These seem to be the offending lines:

https://github.com/edurange/edurange-server/blob/1f693bb2fdd5ab6ca29d494b87cf26c9ae73e349/app/controllers/scenarios_controller.rb#L77-L84

newjam commented 5 years ago

In the view we ask if the model instances_initialized?

https://github.com/edurange/edurange-server/blob/8b5a3c8ee9caccc6fa4d56fc05d8bb4d483292bb/app/views/scenarios/index.html.haml#L71

Which is here in the scenario model: https://github.com/edurange/edurange-server/blob/8b5a3c8ee9caccc6fa4d56fc05d8bb4d483292bb/app/models/scenario.rb#L312-L314

That method in the instance model is: https://github.com/edurange/edurange-server/blob/8b5a3c8ee9caccc6fa4d56fc05d8bb4d483292bb/app/models/instance.rb#L229-L247

So we see that there is an HTTP request made to AWS for every instance in every scenario displayed on this page. That is ludicrous. That work should be done on the background process. The background process should for each instance wait until the instance is initialized and then update the status.

newjam commented 5 years ago

commit 10dbad0 should fix the performance issues. However, we should go back and simplify code that checks for different combinations booted and initialized.