0

I am new in API development, and I just started making a POC of my Project as per my project requirements
1. I will receive a request from User based on that Request.
2. I have to request several other providers using API provided by them (at least 8 to 10) for every request I will get from user.
3. Get some information from My Database.
4. Convert all response of other server and information I fetch from My DB into JSON and return it to User who requested it.

I wrote this code in core PHP but performance is not up to the mark it is taking around 2.5 seconds in responding back to user which is really slow. Which language will be better to create such API should I chose any other langue or stick with PHP only but using specific framework and practices I should follow which can help me improving performance of my API?

  • If you have a problem with your code you should create a [minimal, complete, and verifiable example](http://stackoverflow.com/help/mcve) so we can help you with your problem. – Sam Jan 10 '16 at 01:17
  • How are you making the other requests. Php is normally not asynchronous, meaning you will have to wait for each request to come back before making the next. There are ways around this using stream selection, proc_open and it's like, or frameworks such as React PHP. – DanielM Jan 10 '16 at 01:21
  • Investigate your queries to be sure that the indexes are set up correctly; incorrect indexes can massively slow queries. – Richard Theobald Jan 10 '16 at 01:36
  • Yes for making Asyncronus calls I used cURL . if I switch to other platform like Node.js or any other similar language that can improve performance of my code ? – Abhishek Singh Jan 10 '16 at 01:47

2 Answers2

5

I'd recommend taking the three M's approach to performance: measure, measure and measure.

There's a lot you might be able to improve with a little work within this stack; its somewhat simple, logical and doesn't have too many moving parts.

Start by measuring things like SQL query runtime, how long it takes your application to format a JSON response, and how long from start to finish your app takes to return a response. You can do so with a simple PSR compliant logging package like monolog.

Knowing these measurements will help you answer questions like:

  • is my application slow or is the hosting?
  • is my database slow; should I look at column indexes?
  • is my network slow; should I host the application closer to my customers?
  • is my application router slow; should I cache the router (if its using \Reflection classes it may be particularly bad at performance)

You can get more detailed than a timestamp and log message in a file by looking into application profiling. There's a particularly good SO post that outlines how to approach this.

tldr; don't just learn a new language/stack because it might be faster. If you really want to I'd look at a language like Go (anecdotally its somewhat easy to migrate to from a PHP background). But only do so if its the right decision for your application and business; introducing new technology means you'll need to maintain it in a few months time too.

Community
  • 1
  • 1
developerjack
  • 1,173
  • 6
  • 15
0

If this is slow it's probably due to the added response time of your information providing services. So using a different language probably is of little use. Consider storing/caching the response so the network round-trip isn't taken every time.

Max
  • 2,561
  • 1
  • 24
  • 29