Dealing with blocking API and CPU-bound operations

The audio transcoding server implementation is now feature complete: it exposes an HTTP API to encode MP3 files to FLAC, and the resulting file is uploaded on an S3 database. However, there are still several things which are needed to improve it. This part focuses on improvements to the performance of the server. Let's start with a quick analysis of the current performances of the server. The following shell script executes ten instances of curl to transcode ten files in parallel. In other words, it simulates ten simultaneous encoding requests:

#! /bin/sh

transcode_url="http://localhost:8080/api/transcode/v1/flac"
date
echo "encoding file 0"
curl -X POST --data-binary @banjo0.mp3 $transcode_url/banjo0 &
echo "encoding file 1"
curl -X POST --data-binary @banjo1.mp3 $transcode_url/banjo1 &
echo "encoding file 2"
curl -X POST --data-binary @banjo2.mp3 $transcode_url/banjo2 &
echo "encoding file 3"
curl -X POST --data-binary @banjo3.mp3 $transcode_url/banjo3 &
echo "encoding file 4"
curl -X POST --data-binary @banjo4.mp3 $transcode_url/banjo4 &
echo "encoding file 5"
curl -X POST --data-binary @banjo5.mp3 $transcode_url/banjo5 &
echo "encoding file 6"
curl -X POST --data-binary @banjo6.mp3 $transcode_url/banjo6 &
echo "encoding file 7"
curl -X POST --data-binary @banjo7.mp3 $transcode_url/banjo7 &
echo "encoding file 8"
curl -X POST --data-binary @banjo8.mp3 $transcode_url/banjo8 &
echo "encoding file 9"
curl -X POST --data-binary @banjo9.mp3 $transcode_url/banjo9 &

wait
date
echo "completed"

After the ten transcoding requests have started, the script waits for the completion of all of them to print the time. Since the time is also printed at the beginning of the script, this allows us to see how much time was required to do the transcoding. Save this script as encode-all.sh and execute it. It should provide the following result:

Lun 23 jul 2018 22:00:48 CEST
encoding file 0
encoding file 1
encoding file 2
encoding file 3
encoding file 4
encoding file 5
encoding file 6
encoding file 7
encoding file 8
encoding file 9
okokokokokokokokokokLun 23 jul 2018 22:01:19 CEST
completed

This is the result on a relatively old laptop with a dual core i5 processor (that is: with four execution engines, as the cores are hyperthreaded). The encoding lasted 31 seconds. This timing will be faster on more recent processors. However, a look at the CPU utilization via a monitor shows that something is wrong: only one core has been used, and not even at 100%.

The processor is clearly under-used. The server is composed of three main operations: the HTTP server, the audio encoder, and the S3 uploader. The HTTP server and the S3 uploader are I/O bound operations. So, if they are both implemented in an asynchronous way, they are very efficient. The audio encoder is a CPU-bound operation. But there is bad news here:

  • The S3 implementation is synchronous, so it can block the whole event loop
  • The audio encoder is CPU-bound, which must be handled with multiprocessing or multithreading

So let's make some changes to improve the performance of the encoding server.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.217.150.123