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

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

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?

Popular in Questions Top

gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
nobody
How to bind a phoenix app to a specific ip address? could not find anything about that, nowhere, unfortunately, but for me this is quite...
New
LegitStack
I’m trying to make a websocket server in Phoenix or raw Elixir. I heard about gun, I think I could use cowboy, but since I’m not that sma...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New
Emily
I have VueJS GUIs with the project generated using Webpack. I have Elixir modules that will need to be used by the VueJS GUIs. I forese...
New
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
RisingFromAshes
I’ve read in another post that it may be possible with a router helper - but I couldn’t find an appropriate one, and tbh, I’m still just ...
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New
script
If I have a string “1000 cfu/ml” . I want to remove the characters and / and space . So the string is like this "1000" What is the ...
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New

Other popular topics Top

aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
marius95
Hello everyone, I try to use an Javascript Event Handler in my root.html.leex file. Therefore I created a function in the app.js file: ...
New
Nvim
Anybody knows a comprehensive comparison of Django and Phoenix, thanks for the help. Where are they similar? Where do they differ the m...
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New
jaysoifer
Is there a way to rollback a specific migration and only that one (“skipping” all the other ones)? Would mix ecto.rollback -v 200809061...
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New

We're in Beta

About us Mission Statement