grammY: Slower response time compared to telegraf

Hi there! Firstly, I’d like to thank you for your remarkable work! I’m pretty new to the world of Telegram bots and before stumbling into grammY, I had been playing with telegraf for a few days and the lack of documentation and so on was really starting to drive me insane, but then I was absolutely delighted to come across grammY! I’ve found it to be a much nicer and overall far superior framework to both of the current big names, namely telegraf and node-telegram-bot-api. I think it’s criminally underrated at the moment, I sincerely hope it gets more and more popular over time and hopefully at some point overtakes the big two 😃 Keep up the great work!

Okay, now regarding the issue I’ve been facing: The only thing I’ve been slightly disappointed about in grammY has been the noticeably slower response time I consistently experience compared to telegraf (and also node-telegram-bot-api, for that matter). I’m not sure why this happens though, it’s strange. I’ve created a dead-simple repro (you can find it in this repo), you can just run either npm run grammy or npm run telegraf and go to http://t.me/telegraf_vs_grammy_bot to take a look at the result.

This is grammY:

https://user-images.githubusercontent.com/26527405/168333909-09eb2f9d-19d7-44a0-9ec5-6e5e832ac423.mp4

This doesn’t look terribly slow if you look at it alone, but it’s visibly (much) slower than telegraf — see below — and based on my experience this slowness gets more and more noticeable as the bot grows in complexity.

Now, here’s telegraf:

https://user-images.githubusercontent.com/26527405/168334591-4da72297-f13f-4bbe-b59c-47fac59b7035.mp4

I think the difference is clear 😃

Note that this is all under the same network connection and everything (For what it’s worth, I also have a VPN enabled as I’m living in Iran and the government has unfortunately banned Telegram here.)

Not sure why this is happening, as it seems to me that there shouldn’t be a fundamental difference between how grammY and telegraf (or ntba) retrieve updates (this is the default polling technique btw, I’ve also tried grammY runner — included in the repo — but it doesn’t improve the performance in this particular case at all).

Thank you in advance.

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Reactions: 1
  • Comments: 27 (14 by maintainers)

Most upvoted comments

Hi there! Firstly, I’d like to thank you for your remarkable work! I’m pretty new to the world of Telegram bots and before stumbling into grammY, I had been playing with telegraf for a few days and the lack of documentation and so on was really starting to drive me insane, but then I was absolutely delighted to come across grammY! I’ve found it to be a much nicer and overall far superior framework to both of the current big names, namely telegraf and node-telegram-bot-api. I think it’s criminally underrated at the moment, I sincerely hope it gets more and more popular over time and hopefully at some point overtakes the big two 😃 Keep up the great work!

Okay, now regarding the issue I’ve been facing: The only thing I’ve been slightly disappointed about in grammY has been the noticeably slower response time I consistently experience compared to telegraf (and also node-telegram-bot-api, for that matter). I’m not sure why this happens though, it’s strange. I’ve created a dead-simple repro (you can find it in this repo), you can just run either npm run grammy or npm run telegraf and go to http://t.me/telegraf_vs_grammy_bot to take a look at the result.

This is grammY:

grammy.screen.2.mp4 This doesn’t look terribly slow if you look at it alone, but it’s visibly (much) slower than telegraf — see below — and based on my experience this slowness gets more and more noticeable as the bot grows in complexity.

Now, here’s telegraf:

telegraf.screen.mp4 I think the difference is clear 😃

Note that this is all under the same network connection and everything (For what it’s worth, I also have a VPN enabled as I’m living in Iran and the government has unfortunately banned Telegram here.)

Not sure why this is happening, as it seems to me that there shouldn’t be a fundamental difference between how grammY and telegraf (or ntba) retrieve updates (this is the default polling technique btw, I’ve also tried grammY runner — included in the repo — but it doesn’t improve the performance in this particular case at all).

Thank you in advance.

Hi Arad I had same issue on my local device, but in production mode on one of ParsPack servers the issue seems to be disappeared and it was so efficient and fast. Don’t worry about it and enjoy the library. If you had any questions I’m available for you to help on social media with my id hayyaun.

Just for the record, I did a little test on a VPS (located in Germany), and it confirmed what @hayyaun alluded to.

Here’s when the bot’s running on my local machine (with a VPN enabled, of course):

https://user-images.githubusercontent.com/26527405/172062787-27b57c55-2d64-4327-99e0-6a4486be6c65.mp4

And here’s when it’s running on the VPS:

https://user-images.githubusercontent.com/26527405/172062802-3d030f6e-6a3b-4add-a8b1-c56280e61016.mp4

As is clear from the video above, the delay disappears on the VPS and the performance is perfectly optimal.

My location is in the Philippines and i reproduce the same issues,then I tried running the same code with a friend who lives in Singapore, but unfortunately the situation didn’t change…😥

Sure, definitely 😃 Thank you!

This is indeed a very peculiar problem and I have literally zero clue as to what could be causing it, and of course the fact that it’s not easily reproducible for you guys means debugging it would be a total pain. So, I understand what you’re saying, thanks a lot for your efforts so far 😃

I’m not worrying too much about it at the moment though to be honest.

Because as @hayyaun said in this comment this problem apparently disappears when the bot’s running on a VPS outside of Iran, for instance, which means in production the users aren’t going to suffer from it, so it’s not that big of a deal ultimately if that’s the case. I can definitely live with the slower response times during development, as long as it’s not an issue هد production 😃

Hi @KnorpelSenf! I just tested with grammyjs/grammY#longer-keepalive-times and unfortunately I’m not seeing any noticeable difference in terms of response time.

But I very much appreciate your efforts to fix this ❤️

Hi and welcome! Thanks for the kind words, glad you like it 😃

I don’t have access to a VPS in Iran so this is hard to reproduce. There is no speed difference for me in various data centres across Europe.

We’ve had a similar discussion in #189 not long ago. Perhaps @hayyaun knows any advice?

It’s peculiar that there can be a difference in response time. Both libraries use the identical HTTP client library under the hood, so they’re fundamentally identical when it comes to how requests are performed.

I know also that grammY generates requests which are identical to the byte level for most requests when compared to curl.

In a few days I can take some time to inspect more thoroughly if there are any differences between the libraries. However, I have little confidence that this will yield any results, since that’s not how the library behaves for me.

Note also that grammY is powering large bots with tens of millions of requests every day (several hundred per second) and there are no issues with responsiveness.

Alright, that’s good to hear. Debugging it will likely take several hours, I’d have set aside a day. Maybe it’s the easiest option to just not investigate it.

Should we close this issue?

Odd stuff. There is no known difference between grammY and Telegraf anymore, so it is unexpected that there still is an observable time difference.

Can you try to run these three scripts using time and with the same versions as in the image? Please run

export BOT_TOKEN=.....

with your bot token before.

grammY test script

// file: grammy.js
const { Api } = require('grammy')

const api = new Api(process.env.BOT_TOKEN)

api.getMe().then(console.log.bind(console))

Run it using

time node grammy.js

Telegraf test script

// file: telegraf.js
const { Telegram } = require('telegraf')

const api = new Telegram(process.env.BOT_TOKEN)

api.getMe().then(console.log.bind(console))

Run it using

time node telegraf.js

bash test script

time curl https://api.telegram.org/bot$BOT_TOKEN/getMe

Roger that