Jump to content

Optimisation tips, please...


celfred
 Share

Recommended Posts

Hello,

I've been struggling ALL day on my loading time problem and I'm stuck... As I said somewhere else on the Forum, I'm discovering TracyDebugger module to try and help me (and it does help !), but I'm still in a dead-end.

Tracy told me one of my page loads about 4,500 pages and I can't find any way to make it less (and let me tell you that my website is far from having so many users/visitors or whatever, it's just a teacher's website to try to work differently in class).

I have a feeling I am mis-understanding some concepts here. The page can be seen there : http://planetalert.tuxfamily.org/players/4d (for example, but the problem is the same for all teams in my site). As you can see, there are many information on this page, but not that much either I guess for a computer, but it takes 20 seconds to load ! It feels like my page loads just about EVERYTHING that resides in the backend, but I just can't understand by looking a tmy code. For example, the little green and red circles next to the player's names show the actions that have been saved on the last day (so usually 3-5 actions at most, except for players doing online exercises who may have 10 actions). My query is :

    $lastEvent = $player->child("name=history")->children("sort=-date")->first();
    // Get all events on same day
    $prevDay = date("m/d/Y", $lastEvent->date);
    $prevDate = $prevDay.' 0:0:0'; // Select events for the whole day
    $prevEvents = $player->child("name='history'")->children("date>=$prevDate");
    $trend = '';
    foreach ($prevEvents as $event) {
      $HP = $event->task->HP;
      $title = $event->task->title;
      if ($HP < 0) {
        $trendClass = 'negativeTrend';
      } else {
        $trendClass = 'positiveTrend';
      }
      if ($event->summary !== '') {
        $summary = ' ('.$event->summary.')';
      } else {
        $summary = '';
      } 
      $trend .= '<span class="'.$trendClass.'" data-toggle="tooltip" data-html="true" title="'.strftime("%d/%m", $event->date).': '.$title.$summary.'">&nbsp;</span>';
    }

But in Tracy pages, I see that the COMPLETE history for EACH player is loaded ! I thought my code was limiting it to the events appearing on the same day as the last saved event. Do you see where I am wrong ?

What's more : I feel like I must do the same mistake for the players (they are all in althought I'm limiting to the particular team). I must say though, that I'm caching (supposively) ALL players when Admin is logged in so he can have a faster access in the top menu.

More? All places and people that players can free are loaded as well. Why is this? I just want to load the places and people that are actually freed by players and show the names (not complete info). I'm not putting an example of the code here because I have a feeling the problem resides in my way of organising the complete thing... and that's what worries me the most... Maybe it's gonna be hard for one of you to understand (and have the time) my awkward way of organizing things (even though I'm trying hard to be well-organized ;) ). So I'd rather point to the Github repository : https://github.com/celfred/PlanetAlertProfile/tree/master/site/templates

The file I'm talking about above is team.inc.php. It is included in list-all.php which includes head.inc and foot.inc as well.

Things have changed quite a bit on my local site today, but I'm getting in such a mess that I'd rather not commit to remote. I've corrected some things, but what I have just explained above is still there :(

While I'm at it. Other page, other problem detected : my adminTable.php page which you can a look at at : http://planetalert.tuxfamily.org/players/adminTable/4d (I've just noticed that it should be acess restricted, but nevermind for today, don't submit just in case I forgot other things... _ yes, I'm desperate tonight...) and which submits to submitForms.php. Well, I had to put AJAX request for that because it was running into a timeout because of the loading time. It worked (althought I ended up waiting 30 seconds to record my table !) but today, Tracy and debug ON showed me the 'Maximum 100 nested functions limit fatal error' after hours of research, I endend up... writing this post :( I know my function updateScore() (in my-functions.php) is difficult, but what?? It just updates the scores depending on the task being recorded then checks the impact on other players and if it does have an impact, it triggers itself back to update the score of that other player. But yes, I know it can be some sort of never-ending story, but how can I do differently ? I wonder if it's ok to have wire('pages') in so many of my functions. But again, how can I do in another way?

Well, sorry for such a long post, folks, but as you can understand, I'm facing a wall here and I hope someone around here will find some time to try and find his or her way into my code and give me a hint on where I could improve.

I have had so many help from this community in the past and it felt such a relief and a revelation (Ended up telling my wife : "Wow, it looks so simple for these guys ! I've spent so many hours on that and... Of course ! Now I get it !! I'm getting better ! No next time !")

Well, today is a 'next time'...

Thanks if you've read up to this point and THANKS if you go and have a look at my code !

Fred.

Link to comment
Share on other sites

Yeah, that's a lot of post :P. Let's see if we can take it bit by bit.

I believe this line will grab ALL the children (a PageArray) of history from the database, then return the first (in-memory manipulation) of these retrieved results:

$lastEvent = $player->child("name=history")->children("sort=-date")->first();

Instead, try this, to actually grab ONLY one child from the database:

$lastEvent = $player->child('name=history')->child('sort=-date');

This line doesn't make sense to me:

$prevEvents = $player->child("name='history'")->children("date>=$prevDate");

How can previous events' date be greater than the previous date?

  • Like 4
Link to comment
Share on other sites

Thanks @kongondo for the 1st tip. I get the difference between children('-date')->first() and child('-date'). Although I've just tried to change this line and nothing changed in my Tracy 'Processwire Debug' panel... still many many loaded pages... I'll dig that later when I have more time. Maybe I have the same kind of error somewhere else...

About the date>=$prevDay : this solved an issue I had because of the time appended to the date. Actually in my code, I take the last event, get the date, change the time to 0:0:0 and eventually get the >= to find all those events having the same day, but with a superior time. Maybe (I'm sure actually...) there's a better solution. I remember at the time I implemented this, I was surprised not to be able to retrieve the same day events and this was the only workaround I found :)

Link to comment
Share on other sites

In the frontend, have you checked to see how many pages are being retrieved? Something like this:

$prevEvents = $player->child("name='history'")->children("date>=$prevDate");
echo $prevEvents->count();// does this output the number of pages you are expecting?

In addition, any repeaters in the fields you are retrieving?

  • Like 1
Link to comment
Share on other sites

The $prevEvents->count() is fine. This part works as expected :)

Your post reminded me of trying to cut down through my page to see where the ALL pages request was (ou could be), and reducing things little by little, I saw my checkStreak() function in which I look over the history of each player ti find if the last 10 consecutive events are positive, in which case the player becomes an 'AMbassador' and here was my code :

  $lastEvents = $player->get("name=history")->find("template=event, sort=-date")->not("task.name=donated|donation|absent, limit=10");

I bet you can see right through it : the 'limit=10' is misplaced and useless where it is !!! The code should be (I think) :

  $lastEvents = $player->get("name=history")->find("template=event, sort=-date, limit=10")->not("task.name=donated|donation|absent");

Now my 'loaded page' is reduced to about 480. What a gain ! And this checkStreak() function is called each time I save a new event for a player (to check if he needs to become an ambassador...)

So I'll keep digging because I can imagine I have the same sort of mistake at other places, but I am already glad of this improvement.

So thanks a lot !

Link to comment
Share on other sites

// Returns 10 pages, which are then further filtered, so it could even be none
$lastEvents = $player->get("name=history")->find("template=event, sort=-date, limit=10")->not("task.name=donated|donation|absent");
// Returns 10 of the correct pages
$lastEvents = $player->get("name=history")->find("template=event, sort=-date, limit=10, task.name!=donated|donation|absent");

Not sure what behaviour you want there, but there's a difference.

  • Like 2
Link to comment
Share on other sites

I feel a little dumb once again... Your explanation is perfect and I guess I want the 2nd one. I think I was mistaken on that 'not()' filter. I'll have to check everywhere I've used it.

The 'further filtered' was the key in your explanation. Thanks !

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...