apix / log

A thin (and fast) PSR-3 logger.
Other
49 stars 3 forks source link

Huge speed boost compared to monolog #9

Open 8ctopus opened 1 year ago

8ctopus commented 1 year ago

Hey Franck,

Did some speed tests comparing Apix vs. Monolog, it turns out in deferred mode, the speed gains are huge.

-----------------------------------
platform           :      WINNT x64
php version        :          8.1.4
xdebug             :             on
memory limit       :           512M
max execution      :              0
time per iteration :           20ms
iterations         :            250
-----------------------------------
---------------------------------------------------------------
0                  : logger_monolog   logger_apix
mean               :             25          1430      +5564.1%
median             :             18          1756      +9934.3%
mode               :              3          2013     +67000.0%
minimum            :              1            28      +2700.0%
maximum            :             73          2183      +2890.4%
quartile 1         :              5           750     +14900.0%
quartile 3         :             51          1979      +3780.4%
IQ range           :             46          1229      +2571.7%
std deviation      :             21           654      +2941.1%
normality          :          30.7%         30.7%
---------------------------------------------------------------

And respective code

$file = new Apix\Log\Logger\File('log_apix.log');
$file
    // intercept logs that are >= `warning`
    ->setMinLevel('warning')
    // don't propagate to further buckets
    ->setCascading(true)
    // postpone/accumulate logs processing
    ->setDeferred(true);

$log = new Apix\Log\Logger([$file]);

$stdout = new Apix\Log\Logger\Stream('php://stdout', 'a');
$stdout
    // intercept logs that are >= `warning`
    ->setMinLevel('warning')
    // don't propagate to further buckets
    ->setCascading(true)
    // postpone/accumulate logs processing
    ->setDeferred(true);

$log->add($stdout);

while (microtime(true) < $time_limit) {
    $log->warning('test');
    ++$iterations;
}
$log = new Monolog\Logger('test');
$log->pushHandler(new Monolog\Handler\StreamHandler('log_monolog.log', Monolog\Level::Warning));

// log to stdout
$log->pushHandler(new Monolog\Handler\StreamHandler('php://stdout', Monolog\Level::Warning));

while (microtime(true) < $time_limit) {
    $log->warning('test');
    ++$iterations;
}
8ctopus commented 1 year ago

Slightly updated the test with regular flushing intervals for apix (every 200 logs). It decreases the advantage of apix over monolog, but it's still very present:

-----------------------------------
platform           :      WINNT x64
php version        :          8.1.9
xdebug             :             on
memory limit       :           512M
max execution      :              0
time per iteration :           50ms
iterations         :            250
-----------------------------------
---------------------------------------------------------------
0                  : logger_monolog   logger_apix
mean               :            107          2373      +2108.6%
median             :            111          2496      +2148.2%
mode               :             82          2600      +3070.7%
minimum            :              3           499     +16533.3%
maximum            :            175          3000      +1614.3%
quartile 1         :             82          2203      +2586.6%
quartile 3         :            136          2694      +1880.9%
IQ range           :             54           491       +809.3%
std deviation      :             36           476      +1234.2%
normality          :           3.2%          3.2%
---------------------------------------------------------------