din_S

din_S

Phoenix performance issues with API

I’m using Phoenix to build a simple API that takes an HTTP request (JSON) and add the IP and user-agent to it and then insert it in MongoDB

 def imp(conn, params) do
    params=Map.put(params,"ip",conn.remote_ip |> Tuple.to_list |> Enum.join("."))
    params=Map.put(params,"ua",get_req_header(conn, "user-agent"))
    Mongo.insert_one(:mongo, "impression", params)
    json(conn,  %{})
  end

and it worked well, but what really is surprising is the performance, I have implemented the same function using NodeJS and this was the responses time of both:

                 Node     Phoenix 
#1             237 ms       4s
#2             65   ms      571ms
#3             30   ms     575ms
#4             33   ms     581ms
#5             27   ms     577ms
#6             30   ms     571ms
#7             31   ms     567ms

I have read about phoenix performance and how it should respond in microseconds time.

I thought that dealing with MongoDB is the issue, so I delete the insertion code but I still get 953ms response time (the first time) and 89ms, 350ms, 69ms the other times

so why my phoenix app is not that fast?

Note: I’m using Windows and Postman

Marked As Solved Switch mode

wanton7

wanton7

code_reloader: true in dev.exs can cause this, try setting it to false. But you shouldn’t test response times in dev mode any way. Also I’m sure that almost no one uses Windows in production so it might not be up to par with with Unix/Linux. If you want to test response times in Windows I would use WSL (Windows Subsystem for Linux) and run your app there in prod mode.

Also Liked

sasajuric

sasajuric

Author of Elixir In Action

Definitely test in prod like others already suggested. Also make sure that not too much logging occurs, e.g. by setting the log level to warn. There are a few other minor tips which can improve numbers. I blogged about it here. The article is 4 years old, but hopefully most of the tips still apply :slight_smile:

din_S

din_S OP

Thank all of you guys.
Actually, your responses are relieving

I have tried to set code_reloader: false and I started to get 30 ms responses and I will benchmark the app in production mode next times

lucaong

lucaong

Consider that we’re getting in the realm of micro-optimizations here. At the point when you add a call to MongoDB, especially if synchronous, it probably does not make sense to optimize Map.put vs. Map.merge and rather use whatever is more readable, as the database IO would dominate the time.

Where Next?

Trending in Questions Top

stjefim
Hello! Suppose you are building workflow (order / task / payment) processing system with the following requirements: Each workflow con...
New
jonnycharles
I’m in search of an Elixir library that offers PDF generation capabilities similar to Ruby’s Prawn. While there have been discussions abo...
New
spammy
I’m looking to build a personal workflow to quickly deploy web applications written in elixir/phoenix, for local consumption (ie not on t...
New
silverdr
Using Phoenix.LiveView.TagEngine as an EEx.Engine is deprecated! To compile HEEx, use Phoenix.LiveView.TagEngine.compile/2 instead. Sta...
New
dli
Before I dive in myself, did anyone successfully sprinkle Hologram into their existing LiveView app? Looking for hints regarding: Addi...
New
bottlenecked
Hi all, I wanted to ask how the community is dealing with post-release steps. Today we have Ecto migrations, which make sure that the db...
New
michallepicki
I am using Oban and occasionally, shortly after a deployment, a handful of jobs can fail because of dependency on other parts of the syst...
New

Other Trending Topics Top

JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve. They are GUI (Emerge) and State management (S...
New
ausimian
Emily is an Elixir library that runs Nx computations on Apple’s MLX. Install it as the default Nx backend and Nx, defn, Axon, Nx.Serving,...
New
type1fool
I just stumbled on a newly redesigned elixir-lang.org. :tada: It looks like @Software_Mansion did the work, and I think it is generally a...
New
akoutmos
@hugobarauna and I (Alex Koutmos) have been hard at work on writing a book on Nerves that takes you from simply blinking LEDs to building...
New

We're in Beta

About us Mission Statement