Currently, I am using websockets to send an image to my server, process it, and then send it back. Specifically, I am using Ruby with Sinatra and sinatra-websocket.
On my development server, it takes ~2 seconds to send an image to the server and retrieving the exact image without processing.
On an AWS-EC2 instance, this takes ~15 seconds. The image file I am sending is ~500kb. My upload and download speeds are well above that.
How can I speed up this process? Is this a naive way of sending images back and forth?
Edit: To replicate my issue, you can clone and run my repo in an AWS-EC2 free tier instance.
This is more or a testing helper than an answer, since the question doesn't provide enough details to narrow the issue down.
I wrote a simple test application using Plezi. Since I'm plezi's author it was easier for me than to learn your stack.
It works perfectly on my computer, 79ms for a ~3Mb file.
The ~3Mb roundtrip to Heroku took my system 3 seconds and went down to ~2.5 seconds after the TCP/IP warmup was complete...
...but my internet speeds are probably effecting the test (my reception is low at the moment, so I might be slow).
I'm not sure I can replicate the issue, but you can use the code in this answer to test your server.
If the roundtrip still takes longer than 10 seconds, it might be the EC2 stack. I don't think 10 seconds would be reasonable for ~500Kb.
On the other hand, if it's a shorter roundtrip, it might be either the way you tested your application or your Ruby stack... in which case, maybe the solution is to switch to plezi (or the iodine native websocket design).
You can paste the following code into
config.ru(remember you'll also need a gem file with theplezigem and possibly aGemfile.lock):To run the code from the terminal, use the
iodinecommand (it will start theiodineserver, which Plezi requires.EDIT
From the link to the git-repo (in the comments), I realized that the JSON is parsed by the server and then it is re-emitted.
To emulate this, I updated the example code.
This should be similar to what the code in the repo seems to do and it adds some time to the roundtrip, since the JSON parsing and re-formatting create copies of the data, which requires memory allocation as well as CPU time.
The only change in the code is in the
RoundTripcontroller class, but I'm pasting the whole thing for your copy+paste convenience.Place the following code in your
app.rbfile (remember to editinstall.shto install theplezigem):To run the code from the terminal, use the
ruby app.rbcommand, same as your repo does for the existing app.While the old code simply offered and "echo" response, the new code (which looks almost the same) has a few more steps:
Using Auto-Dispatch, the Plezi framework now automatically parses the JSON and routes the event ("roundtrip") the the controller's method (
roundtrip).The method receives a Hash with the parsed data and returns that Hash back to Plezi.
The framework collects the Hash, formats a JSON object and emits back the result (non String or Hash results are ignored)...
... this is similar to the repo's behavior.