Y
Y
YariKartoshe4ka2019-10-20 11:24:46
PHP
YariKartoshe4ka, 2019-10-20 11:24:46

How to optimize the VK bot?

I have a bot on VK API. He knows how to make memes from text and photos. Its logic is:
1. Upload the user's photo to yourself
2. Process it (overlay text on it)
3. Delete the original photo
4. Upload the finished photo to the VK server
5. Send the finished photo to the user
6. Delete the finished photo (end of the scenario)
Execution results code:
1.2 times - 7000-5000 ms (milliseconds)
3.4 times - 5000-4000 ms
Others from 4000 to 3500 ms
Even from simultaneous use by five people, the server will start to push.
It seems to me that this is pretty bad. What are the optimization options?
I thought to cache:
1. Check if there is a finished picture with such text.
2. If there is, then we send it to him.
3. If not, then:
1. Upload the user's photo to yourself
2. Process it (overlay text on it)
3. Delete the original photo
4. Upload the finished photo to the VK server
5. Send the finished photo to the user
6. Add the finished photo to the cache .
But imagine this situation: the user sent a picture with text. The bot made a meme, sent it and added it to the cache. But if the user sends another picture with the SAME text, the bot will send the previous picture. This is very bad.
It is also possible to flood all free space on the server (server hard disk) with memes and the bot will become unavailable. What are the options?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Anton Neverov, 2019-10-20
@YariKartoshe4ka

1) You can set up caching, it depends on the number of repetitions. If they are at least 10-20 percent, then you should think about it. The caching process needs to be simplified: save the ID of the picture from VK in the database (you can also save the md5 hash of the picture), the text of the picture and the ID of the finished picture after downloading from VK. Delete the image itself from the server. In the future, if the text and the picture ID match, send the old, already sent picture by its ID from VK.
2) Think about image resizing. Perhaps they work for a long time due to the wild resolution of the incoming material.
3) Find out at what stage the bot lingers the most. What eats the most resources.
4) Well, hosting, perhaps yours is so-so. Now, until now, some providers are sitting on the HDD, which significantly slows everything down. What can we say about the performance of the cores.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question