1. The forums will be archived and moved to a read only mode in about 2 weeks (mid march).

Solved AsyncTask takes a lot of time

Discussion in 'Development' started by Miste, Jun 17, 2017.

  1. Miste

    Miste Baby Zombie

    Messages:
    109
    GitHub:
    Misteboss
    Hello,
    I recently noticed that my AsyncTask took a lot of time to be completed.
    So i have tried to know if it was coming from stuff i was doing or just AsyncTask.
    This what i did :
    PHP:
    <?php

    namespace glorious\Task\AsyncTask;

    use 
    pocketmine\scheduler\AsyncTask;
    use 
    pocketmine\Server;
    use 
    pocketmine\Player;
    use 
    glorious\Main;

    class 
    JoinUpdateProfileTask extends AsyncTask{

    const 
    MYSQLI_KEY "MySQL";
    public 
    $clientId;
    public 
    $adress;
    public 
    $date;
    public 
    $serveur;
    public 
    $name;
    public 
    $device;
    public 
    $os;
    public 
    $version;
    public 
    $cid;

        public function 
    __construct($clientId$adress$date$serveur$name$device$os$version$cid){
          
    $this->clientId $clientId;
          
    $this->adress $adress;
          
    $this->date $date;
          
    $this->serveur $serveur;
          
    $this->name $name;
          
    $this->device $device;
          
    $this->os $os;
          
    $this->version $version;
          
    $this->cid $cid;
        }

        public function 
    onRun(){
           
    $start microtime(TRUE);
           
    //MySQL Stuff
           
    $a microtime(TRUE);
           
    $this->setResult($a);
           
    $timeMs = (microtime(true) - $start) * 1000;
           
    var_dump("".round($timeMs2). " onRun() in ms");
        }

        public function 
    onCompletion(Server $server){
            
    $start microtime(TRUE);
            
    $time $this->getResult();
            
    $timeMs = (microtime(true) - $time) * 1000;
            
    var_dump("".round($timeMs2). " Time between onRun() and onCompletion() in ms");
            
    //MainThread Stuff
            
    $timeMs = (microtime(true) - $start) * 1000;
            
    var_dump("".round($timeMs2). " onCompletion() in ms");
     
        }
    }

    This is the result after running the AsyncTask.
    Code:
    string(20) "328.33 onRun() in ms"
    string(61) "1497729265150.8 Time between onRun() and onCompletion() in ms"//It's  lot of time
    string(25) "4.61 onCompletion() in ms"
    
    I don't know if it is an issue but it's very annoying :/.

    And in pocketmine.yml I changed the number of AsyncWorkers to 8 but over time they take more and more time :/ (They can take 30 secs :/).
    This difference of time is very strange.

    @dktapps or someone else, can you tell me if it is unexceptional ?
     
  2. SalmonDE

    SalmonDE Zombie Pigman

    Messages:
    739
    GitHub:
    SalmonDE
    I feel like the long one looks like a "current" timestamp.
    This one looks like the long one: 1497731272 and that's the current timestamp around the time I posted this. So I'd say your code probably has an error somewhere.
     
    SOFe likes this.
  3. Miste

    Miste Baby Zombie

    Messages:
    109
    GitHub:
    Misteboss
    Yep this is strange @SalmonDE but this doesn't change the fact that AsyncTask take lot of time to accomplish
     
  4. SalmonDE

    SalmonDE Zombie Pigman

    Messages:
    739
    GitHub:
    SalmonDE
    You're connecting to a mysql database and think 0.328 seconds for that is a lot of time? In my oppinion that's acceptable.

    Same goes for the third value. It seems to be acceptable, too. (0,004 seconds)
     
  5. Miste

    Miste Baby Zombie

    Messages:
    109
    GitHub:
    Misteboss
    Re read my message, i am talking of the global duration of the AsyncTask which varies between 5 and 30 secs
     
  6. SalmonDE

    SalmonDE Zombie Pigman

    Messages:
    739
    GitHub:
    SalmonDE
    Edit: Actually, I don't think so anymore
     
    Last edited: Jun 17, 2017
  7. Miste

    Miste Baby Zombie

    Messages:
    109
    GitHub:
    Misteboss
    Nop values of my first message are in ms
     
    SalmonDE likes this.
  8. SalmonDE

    SalmonDE Zombie Pigman

    Messages:
    739
    GitHub:
    SalmonDE
    Do you know the timestamps when you call $scheduler->scheduleAsyncTask($yourTask) and when onRun() is called?
    The timestamps of the end of onRun() and the beginning of onCompletion() could be useful, too, I think.
     
  9. Muqsit

    Muqsit Chicken

    Messages:
    1,548
    GitHub:
    muqsit
    Is the myself database hosted on the same system you are executing that code on?
     
  10. SalmonDE

    SalmonDE Zombie Pigman

    Messages:
    739
    GitHub:
    SalmonDE
    After some confusion I think I got what he actually was trying to point out.
    He was wondering why his async tasks take around 5 - 30 seconds to finish at a certain point in time and then calculated how much time the steps inside his task take and figured out they don't need much time.

    So he asks why it still takes so long for it to finish and not how much time it takes to do the mysql queries. (I hope this is what he was trying to say)

    Perhaps he's using too many async tasks or so, because PocketMine waits until an async worker is free for a new async task. (Please help me at this one @Muqsit. I don't know too much about that.)
     
  11. SOFe

    SOFe Administrator Staff Member PMMP Team Poggit Admin

    Messages:
    1,968
    GitHub:
    sof3
    Please give the exact values of:
    - server TPS
    - time between scheduling and onRun
    - time for onRun
    - time between onRun and onCompletion
    - time for onCompletion

    Also tell us your frequency of scheduling tasks and your number of async workers as per PocketMine.yml
     
    SalmonDE and jasonwynn10 like this.
  12. Miste

    Miste Baby Zombie

    Messages:
    109
    GitHub:
    Misteboss
    So :
    Server TPS = 20. (I’m alone on the server it’s just after a restart).
    Time btw scheduling and onRun() : 0.18 ms.
    Time for onRun() : 12215.18 ms.
    Time btw onRun() and onCompletion() : I don’t know, i think i can’t passe time values through threads cause it gives me this value 1497771707623.4 (maybe the current TimeStamp ? @SalmonDE )
    Time for onCompletion() : 3.55 ms.

    I did it four-five times and the time to accomplish the AsyncTask varies between 5 and 30 seconds and i really don’t know why.

    And yes @Muqsit, my database is the same system of the server.

    PS : Sorry for my bad english I’m 14 and I’m French.
     
  13. SOFe

    SOFe Administrator Staff Member PMMP Team Poggit Admin

    Messages:
    1,968
    GitHub:
    sof3
    That means your onRun() is executing slowly. Without showing your code or a more detailed timing breakdown, we can't help.
    Also, the time between AsyncTask scheduling and execution as well as that between execution and completion handling is undefined.
     
    jasonwynn10, SalmonDE and jojoe77777 like this.
  14. Miste

    Miste Baby Zombie

    Messages:
    109
    GitHub:
    Misteboss
    IS their a way to calculate total time since the scheduling to the onCompletion() so I can calculate the time between onRun() and onCompletion() ?
    And about the onRun() code, I am going to do more tests.
     
    jasonwynn10 likes this.
  15. SOFe

    SOFe Administrator Staff Member PMMP Team Poggit Admin

    Messages:
    1,968
    GitHub:
    sof3
    Just pass it through the AsyncTask object in fields :D You can store timestamps (floats) in fields in AsyncTask since it's scalar.
     
    jasonwynn10 and Miste like this.
  16. Miste

    Miste Baby Zombie

    Messages:
    109
    GitHub:
    Misteboss
    OK so I executed the same AsyncTask, 10 times and the time taken by the onRun() function varies between 3000 ms (3sec) and 20 000 ms(20 secs).
    Before I run them I restarted the server after executed one.
    I don’t understand why there is this difference, while my database is on the same VPS.

    I checked the difference between onRun() and onCompletion() isn’t coming from it it’s just (15 ms).

    EDITED : I maybe found from where this difference is coming let me check ^^
     
  17. Miste

    Miste Baby Zombie

    Messages:
    109
    GitHub:
    Misteboss
    Ok so fortunately I didn’t open an issue !
    It was coming from me, i was getting some informations from a website to check if the player has a VPN and as the website is free, it has a long response time (who varies a lot)

    Thanks to @SOFe, @SalmonDE and @Muqsit for their answers !

    PS : Now my AsyncTask takes between 30 and 300 ms to accomplish !
     
    Jack Noordhuis and SalmonDE like this.
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.