Amphp/postgres parallel query execution

Viewed 35

Trying to add some async magic to fully synchronous php application. I'm running php-fpm, amphp/amp:2.6.3, amphp/postgres:1.4.3, using NativeDriver or EventDriver - result is the same. I have 7 queries that don't have to be done sequentially and i'm trying to run them in parallel.

  1. i make 7 promises out of 7 queries with 7 connections - no problems here, i can see it do start to execute all of it in postgres-query-log at the same time.
  2. i wrap each of that promises in call(function, $promise) functions, cuz i don't need just raw arrays of data, i want objects.
  3. then i wrap all 7 promises together in another single ```call`` function to build a context-object that accumulates all objects with data, so after that step i could continue to work with all that data in synchronous manner.
  4. finally i put that 1 promise to wait function to wait until it gets resolved.

Normally i'm getting around 300ms response time on that endpoint and i thought that i could massively speed it up, but instead, after refactoring, i got around 350ms response time . So i start digging. Added to each of 7 queries pg_sleep(5), so i thought i will get 5sec 350ms response time on that endpoint, but instead i got timeout after 1min of execution. Next i tried to left pg_sleep(1) on only 1 query and got 12sec 350ms RT. Looks like 1sec spent in query became +12sec to endpoint RT. I added another second to it and i get 24sec 350ms RT, so i guess it grows in linear manner. I did run a profiling tool and found that i spent all that time in EventDriver::dispatch() or stream_select() if NativeDriver used.

I don't have much experience in async frameworks, so i hope someone more experienced could help me solve this. Eventually i expected for the RT that roughly equals to the worst one of that 7 queries plus db connection time plus php work time.

0 Answers
Related