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

Get the time it took for AsyncTask to complete

Discussion in 'Development' started by Levi, Feb 17, 2019.

  1. Levi

    Levi Skeleton

    Messages:
    955
    GitHub:
    captainleviftw
    It is echoing zero every time. I am not sure if the query was that fast or something is wrong.
    Code;
    PHP:
    public function onRun()
        {
            
    $starttime microtime(true);
            
    $db = new Database();
            
    $uuid $this->uuid;
            
    $query "SELECT coins FROM players WHERE uuid='$uuid';";
            
    $result $db->query($coins );
            
    $coins 0;
            while (
    $row $result->fetch_assoc()) {
                
    $coins += $row["coins "];
            }
            
    $this->setResult($balance);
            
    $endtime microtime(true);
            
    $duration $endtime $starttime;
            
    $hours = (int)($duration/60/60);
            
    $minutes = (int)($duration/60)-$hours*60;
            
    $seconds = (int)$duration-$hours*60*60-$minutes*60;
            
    $ms = (int)$seconds*1000;
            echo 
    "\n".$seconds;

        }
    Btw, I have over 1000 rows created

    I am hosting locally.. is that why?
     
    Last edited: Feb 17, 2019
  2. KielKing

    KielKing Zombie

    Messages:
    245
    GitHub:
    kielking
    you have to set the starttime outside of the task, right before it is called
     
  3. Levi

    Levi Skeleton

    Messages:
    955
    GitHub:
    captainleviftw
    Did I not do that?
     
  4. dktapps

    dktapps Administrator Staff Member PMMP Team

    Messages:
    774
    GitHub:
    dktapps
    The most likely answer is that the task is executing in less than a second.

    In addition, you have a line
    PHP:
    (int) $seconds 1000
    should be replaced with
    PHP:
    (int) ($seconds 1000)
    to avoid problems with operator precedence.
     
    jasonwynn10 and SOFe like this.
  5. Levi

    Levi Skeleton

    Messages:
    955
    GitHub:
    captainleviftw
    Still returning zero.
     
  6. KielKing

    KielKing Zombie

    Messages:
    245
    GitHub:
    kielking
    no you didn't you set the starttime in the onRun method
     
  7. Levi

    Levi Skeleton

    Messages:
    955
    GitHub:
    captainleviftw
    Where is 'before the task is called'? I think I placed it where it should be; before the query.
     
  8. KielKing

    KielKing Zombie

    Messages:
    245
    GitHub:
    kielking
    in another file where you submit the async task to the async pool
     
  9. Levi

    Levi Skeleton

    Messages:
    955
    GitHub:
    captainleviftw
    Ok what about the last part where I display the duration? Do i put it there too?
     
  10. KielKing

    KielKing Zombie

    Messages:
    245
    GitHub:
    kielking
    you display the duration on the onCompletion method, current time - start time
     
  11. dktapps

    dktapps Administrator Staff Member PMMP Team

    Messages:
    774
    GitHub:
    dktapps
     
    jasonwynn10 likes this.
  12. Levi

    Levi Skeleton

    Messages:
    955
    GitHub:
    captainleviftw
    Seriously.. must be because I am hosting locally.
     
  13. GamakCZ

    GamakCZ Zombie Pigman

    Messages:
    598
    GitHub:
    GamakCZ
    try to add sleep(1); to test
     
  14. KielKing

    KielKing Zombie

    Messages:
    245
    GitHub:
    kielking
    PHP:
    //somewhere
    $task = new BalanceAsyncTask($uuidmicrotime(true));
    $this->getServer()->getAsyncPool()->submitTask($task);
    PHP:
    //on the task
    public function onCompletion(Server $server){
        echo 
    "Time Took: " microtime(true) - $this->microtime;
    }
     
  15. SOFe

    SOFe Administrator Staff Member PMMP Team Poggit Admin

    Messages:
    1,968
    GitHub:
    sof3
    He wants to measure the time for the query() function to run. It makes no sense to measure how long it took to get picked up by the async pool.
     
    jasonwynn10 likes this.
  16. SOFe

    SOFe Administrator Staff Member PMMP Team Poggit Admin

    Messages:
    1,968
    GitHub:
    sof3
    Just to be sure, have you tried dumping the value of $duration directly?
     
  17. KielKing

    KielKing Zombie

    Messages:
    245
    GitHub:
    kielking
    little bit contradicting with the thread title
     
    jasonwynn10 likes this.
  18. wolfdale

    wolfdale Zombie Pigman

    Messages:
    535
    GitHub:
    diamond-gold
    microtime(true) already returns it in seconds, i dont understand why you need to go all the way to calculate it again
    PHP:
    $seconds $endtime $starttime;
    $ms $seconds 1000;
     
    jasonwynn10 likes 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.