din_S

din_S

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

Showing Posts 1 to 10

outlog

outlog

are you running phoenix in dev or in prod?

din_S

din_S OP

I’m running phoenix in dev, but really is it that much slow in dev?, I’m running Node in dev as well and it is not that slow

outlog

outlog

yep, much slower - also why the first request is so slow (it compiles on first request)..

for any benchmarking run in prod..

lucaong

lucaong

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.

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.

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

outlog

outlog

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)

 def imp(conn, params) do
    insert=
    params
    |> Map.put("ip",conn.remote_ip |> Tuple.to_list |> Enum.join("."))
    |> Map.put("ua",get_req_header(conn, "user-agent"))

    Mongo.insert_one(:mongo, "impression", insert)

    conn
    |> put_resp_content_type("application/json", nil) #necessary?
    |> send_resp(200, "{}")
  end

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..

Task.start(fn -> 
  Mongo.insert_one(:mongo, "impression", 
    params
    |> Map.put("ip",conn.remote_ip |> Tuple.to_list |> Enum.join("."))
    |> Map.put("ua",get_req_header(conn, "user-agent"))
   )
end)
din_S

din_S OP

Thanks for this amazing reply, but could I ask about what are the other effective ways should I use because I’m using Map.put all over my code

outlog

outlog

personally I didn’t know any, but a bit of googling shows me to use map.merge instead of multiple “map.put” see/read Working with maps - Joy of Elixir

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? Top

Trending in Questions Top

RSP87
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
kszambelanczyk
Hello! Could someone please give me a help/sample code, how to delete a file from s3 using waffle/waffle_ecto from Phoenix app. I creat...
New
RemyXRenard
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
velrest
So my question is quite simple and i have found no conclusive answer on forum, google or AI. Should we use :erlang.float for Integer to ...
New
samoloth
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
FlyingNoodle
If a change or preparation module uses Ash.Changeset.get_argument/2 or Ash.Query.get_argument/2 (or any of the other get_argument functio...
New
ryanwinchester
apply_graft/2 doesn’t rewrite an add_many sub-workflow’s deps on an add step. Grafted jobs cancel with “upstream job was deleted” Version...
New

Other Trending Topics Top

mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
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
Dmk
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
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

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews