DEV Community

Cover image for Use Bing to convert API endpoint call-out from Sequential to Parallel
Hardik Patel
Hardik Patel

Posted on • Updated on

Use Bing to convert API endpoint call-out from Sequential to Parallel

Requirement

Retrieve content from 20 google sheets within single Google spreadsheet.

How should we approach this?

  • Retrieve list of all sheets as array
  • Iterate over the array and retrieve content for each sheet

I asked Bing to get me python code to do so.

Prompt - code to make request

Write python code to get content of range a:a from all available sheets of google spreadsheet

Bing

bing response with code to get sheet data of range from all sheets within single spreadsheet

This is okay, but what if you have 100 sheets within single spreadsheet.

Each request would be around 0.7 seconds, waiting time will be 100*0.7 = 700 seconds. around 11 minutes.

Follow up question to improve performance.

Prompt - improve performance

I am trying to retrieve result from 100 sheet_names array. is there a way you can improve performance of this code? Without giving any directions.

Bing

bing response with code to get sheet data of range from all sheets within single spreadsheet with improved performance

Looks good, it suggested me to use batch_get function of googleapis.

However, what if endpoint or API you are trying to call is simply an http request to an api or endpoint?

Simplified Problem

Let's simplify problem statement
Retrieve users details for each users with id from 1 to 100

Prompt

generate python code to make HTTP request to API https://64217eb134d6cd4ebd74a672.mockapi.io/api/v1/users/{id} for, id from 1 to 30

Bing

bing response with code to make http request to an endpoint or API

Execution time: 13 seconds

Prompt

I am trying to retrieve information for 100 users, it is slow. can you improve performance?

Bing

bing response with code to make http request to an endpoint or API with improved performance with parallel execution
Execution time: 1 seconds

Pretty cool πŸ€–, isn't it? Though now you need to know following things.

  • Handle 429, rate limiting error
  • The impact of multithreaded python script

FYI πŸ˜‰

bing response to explain impact of multithreaded python script

Typescript

Node is by default single threaded application and works on call back, since introduction of async-await, sometimes I see beginners use it to make their code synchronous.

Prompt

generate typescript code to make HTTP request using fetch to API https://64217eb134d6cd4ebd74a672.mockapi.io/api/v1/users/{id} for, id from 1 to 30

Bing

bing response with typescript code to make http request to an endpoint or API

Execution time: 8.5 seconds

Prompt

It is slow, can you make all fetch in parallel?

Bing

bing response with typescript code to make http request to an endpoint or API with improved performance using promise.all

Execution time: 0.6 seconds

Moral of the story

Start utilizing this AI based tools to enhance your productivity 😊✌️

Fun fact: cover image is created by bing.com/create

Top comments (0)