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
Benchmarks should always be executed against a production mode. Development mode is very different, and it is setup for development convenience: when writing code you want changes to appear in your app without having to restart it, and other convenient things, and you normally don’t care about microsecond response time.
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.
code variations for benchmarking:
pipe the map.puts (there might more effective ways than map.put)
avoid json encoding since you are not returning anything
try other “ip to string” method (which btw also works for ipv6 down the line)
also try conn.remote_ip |> :inet.ntoa() |> to_string() and see if it’s faster, also your current code will break on ipv6 afaik…
since this seems to be a stats endpoint consider wrapping the db call in task.start, which will of course void the benchmark as endpoint will now return crazy fast - but this is what I do and would somewhat recommend for this kind of endpoint…
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.
Back when I still used Windows 10 for work (stopped September 2019) Erlang and thus Elixir were definitely slower than on Linux and macOS. Just one extra data point.
Even on a pretty pathetic laptop (i3 CPU with 2 cores) my dev mode apps with code reloading still respond in 5 to 50 ms in Linux. And on my main workstation (macOS) I often see response times below 1ms.
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