Skip to content
This repository has been archived by the owner on Jan 21, 2022. It is now read-only.

Performance issue with big payload. #8

Closed
Reasno opened this issue Mar 3, 2019 · 4 comments
Closed

Performance issue with big payload. #8

Reasno opened this issue Mar 3, 2019 · 4 comments

Comments

@Reasno
Copy link

Reasno commented Mar 3, 2019

rr-grpc takes around 1.5 seconds to return a response with a big payload ( a couple thousand of rows from database). Using plain old JSON over http it usually takes 200-300 milliseconds.

I am using STDOUT/STDIN stream relay, and anything else is copied from the example in the project. I suspect the latency is caused by the transport from php to golang. Can you share some insights on how to improve performance?

@wolfy-j
Copy link
Member

wolfy-j commented Mar 3, 2019

Can you try to profile how long it takes to pack the message without sending it? Google protofbuf have php fallback which is installed by default, you can speed up serializing by installing native C extension.

You can check the serialization speed by calling message like here: https://github.com/spiral/php-grpc/blob/master/src/Invoker.php#L32

@Reasno
Copy link
Author

Reasno commented Mar 3, 2019

It turned out exactly same as you have guessed.

        $then = microtime(true);
        $out->serializeToString();
        $now = microtime(true);
        error_log(sprintf("Elapsed:  %f", $now-$then));
WARN[0178] Elapsed:  1.171713     

This was about the right amount time I lost when compared to json serializer. Thanks for the big help. I am going to try the C library and post the result back.

@Reasno
Copy link
Author

Reasno commented Mar 4, 2019

C implemention is much more performant than the php implementation. After Switching to C, my handler only consumes 300ish ms. In the end I extended the Invoker class so that I could cache the serialized result based on my need.

class CachedInvoker extends Invoker implements InvokerInterface {
	/**
	 * @inheritdoc
	 */
	public function invoke(
	    ServiceInterface $service,
	    Method $method,
	    ContextInterface $context,
	    ?string $input
	): string {
		return Cache::remember(cacheKey(), 60, function () use ($service, $method, $context, $input) {
		    return parent::invoke($service, $method, $context, $input);
		});
	}
}

@Reasno Reasno closed this as completed Mar 4, 2019
@wolfy-j
Copy link
Member

wolfy-j commented Mar 4, 2019

Please do not extend Invoker but rather composite parent into new one (like middleware). It will help you with BC if this implementation will ever change.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants