theiconic / php-ga-measurement-protocol

Send data to Google Analytics from the server using PHP. Implements GA measurement protocol.
MIT License
655 stars 136 forks source link

Considerable time added for TTFB #74

Open CodeZeno opened 5 years ago

CodeZeno commented 5 years ago

What is the expected latency increase from using this library?

I have TTFB (Time To First Byte) times of around 30ms without and around 140ms with this library. Is this expected for this library and are there ways to improve this performance?

jorgeborges commented 4 years ago

The library makes a blocking HTTPS call to the Google Analytics server, adding around 100ms~200ms to any process that is executing it. If you are using it as part of a request that renders a web page, I would assume it would cause a bit hit on performance

When I wrote this library a few years ago it was with the intention of using it in background processes (such as a queue consumer), so the concept of TTFB was not a concern there.

As for ways to improve the performance, not sure. I don't develop on PHP too much nowadays, but I'm open to suggestions.

linaspasv commented 4 years ago

@CodeZeno if you are using php-fpm, you can use fastcgi_finish_request(); to return 200 HTTP response back to the browser asap. This way you are not blocking the request and the rest of the code can finish in the background.

The better approach would be to have a background process (queue) as suggested above.