pedromvieira

pedromvieira

FLOW - How to Enhance performance from a series of flows?

I’m migrating some functions from parallel_stream to flow.
This function loads a xlsx file and generate data to import.
The ideia is to increase performance overall. So far so good, specially with files over 50k lines.
In some tests, went from 106 seconds (parallel_stream) to 62 seconds (flow).
Any tips to increase performance or do it in a few flows?

flow pipeline

  def flow_it(path, header, accepted, id) do
    final_header =
      header_filter(header, accepted)
    final_id =
      id
      |> String.to_integer()
    {_status, _header, lines} =
      path
      |> Sheet.load_all()
    lines
    |> flow_1(final_header)
    |> flow_2()
    |> flow_3(final_id)
  end

reduce

  def flow_1(enum, final_header) do
    enum
    |> Flow.from_enumerable()
    |> Flow.partition()
    |> Flow.reduce(fn -> [] end, fn {_x, y}, acc ->
      temp =
        final_header
        |> Enum.reduce(%{}, fn {a, b}, acc ->
          %{
            b => y |> Map.get(a)
          }
          |> Map.merge(acc)
        end)
      [temp]
      |> Enum.concat(acc)
    end)
    |> Enum.to_list()
  end

only unique rows

  def flow_2(enum) do
    enum
    |> Flow.from_enumerable()
    |> Flow.partition()
    |> Flow.uniq_by(fn x ->
      x["email"]
    end)
    |> Enum.to_list()
  end

some cleaning and calculation

  def flow_3(enum, final_id) do
    enum
    |> Flow.from_enumerable()
    |> Flow.partition()
    |> Flow.reduce(fn -> [] end, fn x, acc ->
      check_email =
        x["email"]
        |> email_fix()
      temp =
        case is_nil(check_email) do
          true ->
            nil
          false ->
            data =
              x
              |> Map.pop("email")
              |> elem(1)
            domain =
              check_email
              |> domain_from_email()
            final_data =
              data
              |> Map.merge(%{"domain" => domain})
              |> Sheet.attrs_validation(["country", "phone"])
            %{
              email: check_email,
              enabled: true,
              list_id: final_id,
              data: final_data
            }
        end
      [temp]
      |> Enum.concat(acc)
    end)
    |> Enum.to_list()
  end

Most Liked

david_ex

david_ex

Check this out: Tuning Elixir GenStage/Flow pipeline processing | Tymon Tobolski

The graph stuff he explains here Measuring and visualizing GenStage/Flow with Gnuplot | Tymon Tobolski could help you visualize and tweak your setup (number of stages, demand size, etc.).

Where Next?

Popular in Questions Top

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
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod – where is this set? Thanks.
New
johnnyicon
Hi all, I’ve just started learning Elixir and Phoenix Framework, so please pardon my n00bness at this stage. I’m trying to use Postgres...
New
pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
Fl4m3Ph03n1x
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: ) Hello all, this is ...
New
chensan
I have a User schema with a :from_id field set to type :string: defmodule TweetBot.Repo.Migrations.CreateUsers do use Ecto.Migration ...
New
Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
yawaramin
In the Dialyzer docs ( dialyzer — OTP 29.0.2 (dialyzer 6.0.1) ), there is a way to turn off a specific warning for a function: -dialyzer...
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

Other popular topics Top

sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
New
lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New
lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID<0.412.0> terminating ** (Postgrex.Error) FATAL...
New
JeremM34
Hello, how can I check the Phoenix version ? Thanks !
New
jason.o
In the code below, if the create action is not set to accept “extra_key” as an input, it errors out with a message shown above. Is there ...
New
boundedvariable
I am going through the kafka architecture. All the features what the kafka is providing are already in Erlang. I would like hear your opi...
New
rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list. ...
New

We're in Beta

About us Mission Statement