Skip to content

Improve performance by skipping device parse if useragent matches desktop pattern #7151

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Jul 11, 2022

Conversation

sanchezzzhak
Copy link
Collaborator

@sanchezzzhak sanchezzzhak commented Jun 24, 2022

Most of the traffic is a desktop, this change helps to reduce the time for meaningless search of regular expressions in devices

in total, there are about ~13400 regular expressions in the project 💪

before the detection was changed, this script detected the agent in ~0.417ms without cache and code warm-up.
after 0.078ms

user_agent: 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/102.0.0.0 Safari/537.36'
os:
    name: Windows
    short_name: WIN
    version: '10'
    platform: x64
    family: Windows
client:
    type: browser
    name: Chrome
    short_name: CH
    version: '102.0'
    engine: Blink
    engine_version: 102.0.0.0
    family: Chrome
device:
    type: desktop
    brand: ''
    model: ''
<?php

require_once ('vendor/autoload.php');

use DeviceDetector\DeviceDetector;
use Symfony\Component\Yaml\Yaml;

$detector = new DeviceDetector;

//$useagent = 'Mozilla/5.0 (Linux; Android 5.0; NX505J Build/KVT49L) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/43.0.2357.78 Mobile Safari/537.36';
$useagent = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/102.0.0.0 Safari/537.36';

$start = microtime(true);
$detector->setUserAgent($useagent);
$detector->parse();

$os = $detector->getOs();
$client = $detector->getClient();

$result = [
    'user_agent' => $useagent,
    'os'  => $os,
    'client' => $client,
    'device'  => [
        'type' => $detector->getDeviceName(),
        'brand' => $detector->getBrandName(),
        'model' => $detector->getModel(),
    ],
];

echo number_format(microtime(true) - $start, 3);
echo PHP_EOL;
echo PHP_EOL;
echo Yaml::dump($result, 2);

@sanchezzzhak sanchezzzhak requested a review from sgiehl July 3, 2022 12:17
@sgiehl
Copy link
Member

sgiehl commented Jul 4, 2022

@sanchezzzhak looks good so far. Would you mind adding a simple test case, where the useragent is a desktop one, but a model is provided through client hints, so we can ensure the model is still detected?

@sgiehl sgiehl merged commit a0b05d2 into matomo-org:master Jul 11, 2022
@sanchezzzhak sanchezzzhak deleted the pr_desktop branch July 11, 2022 14:58
@justinvelluppillai justinvelluppillai changed the title Skip device parse if useragent matches desktop pattern Improve performance by skipping device parse if useragent matches desktop pattern Sep 29, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

2 participants