Maxximiliann
defmodule Benchmark do
def main do
module_function = Foo.main
_ = module_function
end
def run(params) do
number_of_trials = Enum.at(params, 0)
total_warm_up_cycles = Enum.at(params, 1)
IO.puts("Warming up . . . initiating first trial of #{number_of_trials}.")
start_time = System.monotonic_time(:millisecond)
main = main()
_ = main
end_time = System.monotonic_time(:millisecond)
elapsed_time = (end_time - start_time) / 1000
results = %{time: [elapsed_time], acc: 1}
run(results, number_of_trials, totaL_warm_up_cycles)
end
def run(results, number_of_trials, total_warm_up_cycles) do
start_time = System.monotonic_time(:millisecond)
main = main()
_ = main
end_time = System.monotonic_time(:millisecond)
new_time = (end_time - start_time) / 1000
updated_acc = results.acc + 1
average_time =
cond do
results.acc == total_warm_up_cycles -> new_time
results.acc > total_warm_up_cycles -> Enum.sum(results.time) / (updated_acc - (total_warm_up_cycles + 1))
results.acc < total_warm_up_cycles -> []
end
results =
if results.acc >= total_warm_up_cycles do
Enum.map(results, fn _map ->
%{
time: List.insert_at(results.time, 0, new_time),
acc: updated_acc,
average_time: average_time
}
end)
|> List.first
else
Enum.map(results, fn _map ->
%{
time: [],
acc: updated_acc,
average_time: 0
}
end)
|> List.first
end
cond do
results.acc < (total_warm_up_cycles + 1) -> IO.puts("Warming up . . . Trial number #{results.acc} of #{number_of_trials}.")
run(module_function, results, number_of_trials, total_warm_up_cycles)
results.acc == (total_warm_up_cycles + 1) -> IO.puts("Average execution time after #{results.acc} trials of #{number_of_trials}: #{Float.floor(results.average_time, 3)} seconds.")
run(module_function, results, number_of_trials, total_warm_up_cycles)
results.acc < number_of_trials -> IO.puts("Average execution time after #{results.acc} trials of #{number_of_trials}: #{Float.floor(results.average_time, 3)} seconds.")
run(module_function, results, number_of_trials, total_warm_up_cycles)
results.acc == number_of_trials -> IO.puts("Final trial - Average execution time: #{Float.floor(results.average_time, 3)} seconds.")
end
end
end
This is designed to accept the following command:
iex(1)> Benchmark.run([20, 5])
Which would then output:
Warming up . . . initiating first trial of 20.
Warming up . . . Trial number 2 of 20.
Warming up . . . Trial number 3 of 20.
Warming up . . . Trial number 4 of 20.
Warming up . . . Trial number 5 of 20.
Average execution time after 6 trials of 20: 0.754 seconds.
Average execution time after 7 trials of 20: 0.754 seconds.
Average execution time after 8 trials of 20: 0.549 seconds.
Average execution time after 9 trials of 20: 0.48 seconds.
Average execution time after 10 trials of 20: 0.448 seconds.
Average execution time after 11 trials of 20: 0.496 seconds.
Average execution time after 12 trials of 20: 0.471 seconds.
Average execution time after 13 trials of 20: 0.499 seconds.
Average execution time after 14 trials of 20: 0.485 seconds.
Average execution time after 15 trials of 20: 0.47 seconds.
Average execution time after 16 trials of 20: 0.457 seconds.
Average execution time after 17 trials of 20: 0.447 seconds.
Average execution time after 18 trials of 20: 0.438 seconds.
Average execution time after 19 trials of 20: 0.431 seconds.
Final trial - Average execution time: 0.427 seconds.
:ok
When attempting to compile this module, however, it triggers the following error:
== Compilation error in file lib/Benchmark.ex ==
** (CompileError) lib/Benchmark.ex:24: undefined function totaL_warm_up_cycles/0
It looks like total_warm_up_cycles = Enum.at(params, 1) is being ignored? If so, why and how is this resolved?
Ideally I’d love to get this to run with something like iex(2)> Benchmark.run([MODULE.FUNCTION, 20, 5]) which would make this more dynamic since the particular module function to be benchmarked wouldn’t have to be hardcoded. After spending dozens of hours searching far and wide, though, I can’t find any resources that cover how exactly this is done. Is this even possible?
Thanks for all your help and opinions! ![]()
Trending in Questions
I’m aware of 2 such projects, are there others?
git: GitHub - team-alembic/realworld: A fullstack Phoenix LiveView application with back...
New
I having some trouble figuring out if I have set myself too strict of standards for my production server. Currently I can handle 75% of r...
New
Hi everyone.
My team and I have been working on a fairly modest app based around video streaming and chat, but we’ve landed a customer t...
New
I really like the adapter patterns that ecto, nebulex, waffle, etc. use and would love find something similar for a key management servic...
New
Hello folks!
So at work, we are seeing some situations where we have to define some “fixed” strings that are used across the codebase in...
New
How Can I Optimise Compile Time Dependencies
I have been building an elixir application for about 2 years now. Many modules and files ha...
New
Hi,
we run different LLM models across a cluster, with each node loading a configurable subset. Model-specific queues let nodes consume ...
New
Other Trending Topics
Hey. Is there anyone here who creates agents in their apps? Not talking about using agents, but creating them. I’m finding it pretty diff...
New
I fully migrated to my own harness from Anthropic/Gemini and I think it’s time to share it. Welcome DSH, the DeepSeek Harness, fully writ...
New
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
There has been a thread to discuss the Stack Overflow Developer Survey on this forum every year since 2018, so here’s yet another one for...
New
I love Elixir. It’s one of 2 programming languages I’ve ever fallen in love with.
But I don’t use it anymore.
Serverless was the promis...
New
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
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #ai
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #blog-post
- #elixirconf-us
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #elixirconf-eu
- #api
- #forms
- #security
- #performance











Showing Posts 1 to 8- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
Nicd
totaL_warm_up_cycleshas a bigL, it’s a typo.NobbZ
There is an uppercase L in the usage of the variable, but not in it’s definition
kokolegorille
You put a capital L in the second totaL_warm_up_cycles.
Maxximiliann
Wow . . . it’s always the little things, lol
Thanks guys!
Any thoughts on getting something like
iex(2)> Benchmark.run([MODULE.FUNCTION, 20, 5])to work? Is this even possible?Nicd
As for this part, have you tried for example
Benchmark.run(&Module.function/n, 20, 5)(substituting module, function, andnfor the respective module, function, and arity)? Then you could call the function withfunction.(args)(note the period).EDIT: For more information about function capturing, see: https://elixir-lang.org/getting-started/modules-and-functions.html#function-capturing
Maxximiliann
Thanks for your suggestion!
Here’s what my attempt garnered:
What did I miss?
Nicd
Well you would need to change the rest of your function code to match…
In my suggestion the first parameter would be the function to run, so you’d need to use another place for the results. Or put the function in another parameter.
Maxximiliann
Got it, thanks so much!