php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #70490 get_browser function is very slow
Submitted: 2015-09-14 13:06 UTC Modified: 2017-01-02 22:52 UTC
Votes:6
Avg. Score:4.7 ± 0.5
Reproduced:6 of 6 (100.0%)
Same Version:1 (16.7%)
Same OS:0 (0.0%)
From: webtech at mines-telecom dot fr Assigned: nikic
Status: Closed Package: Performance problem
PHP Version: 7.0.1 OS: OS X 10.11.2
Private report: No CVE-ID:
 [2015-09-14 13:06 UTC] webtech at mines-telecom dot fr
Description:
------------
$aBrowserInfos=get_browser(null,true);

Test script:
---------------
$time_start = microtime(true);
$aBrowserInfos=get_browser(null,true);
$time_end = microtime(true);
$time = $time_end - $time_start;
echo "get_browser takes $time sec";


Expected result:
----------------
Less than 50 ms...

Actual result:
--------------
get_browser takes 3.3583149909973 sec

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2015-09-14 13:08 UTC] webtech at mines-telecom dot fr
-Operating System: OS X +Operating System: OS X 10.10.5
 [2015-09-14 13:08 UTC] webtech at mines-telecom dot fr
I've added the version number of OS X.
 [2015-09-19 09:20 UTC] yohgaki@php.net
I think you are using large browscap.ini. Although browscap.ini is cached in memory, the code searching matched entry is not so fast. If database is large, it takes time. (About 2 sec with debug build CLI server & Core i7 4770S using full_php_browscap.ini)

http://browscap.org/
Try lite_php_browscap.ini if you need performance.
 [2015-09-21 15:26 UTC] webtech at mines-telecom dot fr
-PHP Version: 7.0.0RC2 +PHP Version: 7.0.0RC3
 [2015-09-21 15:26 UTC] webtech at mines-telecom dot fr
I'm using php_browscap.ini (16,301 KB), not the full one full_php_browscap.ini (33,714 KB).

I've upgraded to PHP7.0.0RC3 and it's the same.

Is it better to use the browscap-php library (https://github.com/browscap/browscap-php) as recommended in http://browscap.org/ ?

I though it was better to use native PHP...
 [2015-11-04 15:16 UTC] David dot Gausmann at measX dot com
Hello there,

I detected the get_browser command as the lack of performance on my website today. Then I've found this bug here.

Could you please implement a hashmap or something similar for that command?
I cannot believe that searching through an ini file needs so much time.
You could for example split the search up and delegate it to multiple threads.

Here is my example:
<?php

$dTime1 = microtime(true);
$oBrowser = get_browser('Mozilla/5.0 (Windows NT 6.1; WOW64; rv:41.0) Gecko/20100101 Firefox/41.0');
$dTime2 = microtime(true);
$oBrowser2 = get_browser('DoesntExist');
$dTime3 = microtime(true);

echo sprintf('%.4f', $dTime2 - $dTime1), "<br>";
echo sprintf('%.4f', $dTime3 - $dTime2), "<br>";

?>

Searching for a non-existent entry still requires so much time like the regular search (> 1 second).

Kind Regards
David Gausmann
 [2015-11-13 13:05 UTC] dev018 at am-horizon dot com
Hi, I have tested your code but every time it's not take more that 40ms. Could you please test it again
 [2015-11-13 13:34 UTC] webtech at mines-telecom dot fr
-Operating System: OS X 10.10.5 +Operating System: OS X 10.11.1 -PHP Version: 7.0.0RC3 +PHP Version: 7.0.0RC7
 [2015-11-13 13:34 UTC] webtech at mines-telecom dot fr
It's better with RC7 : get_browser takes 1.1309630870819 sec
but it's not 40 ms ;)
 [2015-11-13 13:43 UTC] webtech at mines-telecom dot fr
Sorry, I've made a mistake :
- the result on the last comment "get_browser takes 1.1309630870819 sec" was with PHP 5.6.15

With PHP7.0RC7 :
get_browser takes 4.4257979393005 sec

The PHP browscap file is php_browscap.ini (size: 21664 kb)
 [2016-01-07 15:41 UTC] webtech at mines-telecom dot fr
-Operating System: OS X 10.11.1 +Operating System: OS X 10.11.2 -PHP Version: 7.0.0RC7 +PHP Version: 7.0.1
 [2016-01-07 15:41 UTC] webtech at mines-telecom dot fr
New test :
PHP 7.0.1 : get_browser takes 1.5893349647522 sec

PHP 5.6.16 : get_browser takes 1.4182159900665 sec

It seems to be OK now.

Thanks
 [2016-06-20 16:25 UTC] cmb@php.net
-Status: Open +Status: Closed -Assigned To: +Assigned To: cmb
 [2016-06-20 16:25 UTC] cmb@php.net
> It seems to be OK now.

Fine, closing.
 [2016-06-21 06:59 UTC] webtech at mines-telecom dot fr
Hi,

I was wrong : the performance are really bad even with last versions, so I used php_browscap (https://github.com/browscap/browscap-php).

Thanks for reply
Oli
 [2016-06-22 12:40 UTC] cmb@php.net
-Status: Closed +Status: Re-Opened -Assigned To: cmb +Assigned To:
 [2016-06-22 12:40 UTC] cmb@php.net
> I was wrong : the performance are really bad even with last
> versions, […]

Well, then the ticket should be re-opened. :)
 [2016-08-27 17:33 UTC] cmb@php.net
-Status: Re-Opened +Status: Verified
 [2016-08-27 17:33 UTC] cmb@php.net
I just did some quick measurements with php_browscap.ini (~8.5 MB)
looking for one of the first and one of the last entries. Results
(in seconds):

PHP-5.6: 0.01 resp. 0.62
master:  0.01 resp. 2.98

So, indeed, there is a huge performance *regression*. This doesn't
appear to be related to ZendMM, because without it I get similar
numbers.
 [2017-01-02 22:52 UTC] nikic@php.net
-Status: Verified +Status: Closed -Assigned To: +Assigned To: nikic
 [2017-01-02 22:52 UTC] nikic@php.net
Resolved by https://github.com/php/php-src/pull/2242. This should make get_browser() with a large browscap.ini faster by a factor of 100x or so.
 
PHP Copyright © 2001-2017 The PHP Group
All rights reserved.
Last updated: Tue Aug 29 15:01:52 2017 UTC