Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extend measure to allow optional function args #7

Closed
wants to merge 3 commits into from

Conversation

CJPoll
Copy link
Contributor

@CJPoll CJPoll commented Oct 20, 2016

Erlang's :timer module already has a function for timing the duration
of a function. By using it, we can also enable passing arguments to the
passed-in function.

Examples:

measure("my_key", &Enum.map/2, [my_list, mapper_func])
measure("my_key", fn -> Enum.map(my_list, mapper_func) end) # Maintains backwards compatibility

This reports the duration of the map function, and returns the computed
value.

Optionally, we could also add a measure/4 function which takes a key,
module, function, and args.

measure("my_key", Enum, :map, [my_list, mapper_func])

See http://erlang.org/doc/man/timer.html#tc-1

Erlang's `:timer` module already has a function for timing the duration
of a function. By using it, we can also enable passing arguments to the
passed-in function.

Example:

measure("my_key", &Enum.map/2, [my_list, mapper_func])

This reports the duration of the map function, and returns the computed
value.

Optionally, we could also add a `measure/4` function which takes a key,
module, function, and args.

See http://erlang.org/doc/man/timer.html#tc-1
Copy link
Owner

@lexmag lexmag left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great catch @CJPoll.
Let's skip addition of args in that PR.
Same result can be achieved without them.
Also, since these args will be optional, addition of options, which are optional as well, could be problematic: users will need to always provide args if they pass options.

@@ -44,12 +44,12 @@ defmodule Statix do
for pipelining and easily wrapping existing code.
"""
# TODO: Use `:erlang.monotonic_time/1` when we depend on Elixir ~> 1.2
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can remove this TODO altogether.

def measure(key, fun, args \\ []) when is_function(fun) do
{time, result} = :timer.tc(fun, args)

elapsed_ms = div(time, 1000)
Copy link
Owner

@lexmag lexmag Oct 21, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's move this calculation into the timing call:

{elapsed, result} = :timer.tc(fun)
timing(key, div(elapsed, 1000))
result

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure thing

@CJPoll
Copy link
Contributor Author

CJPoll commented Oct 21, 2016

I'm a bit unsure what you mean in your overall feedback about options and args conflicting? Is that in regards to the idea of a measure/4 function?

measure/4 would probably look something like this:

def measure(key, module, function, args) when is_atom(module) and is_atom(function) and is_list(args) do
   {time, result} = :timer.tc(module, function, args)
   timing(key, div(time, 1000))
   result
end

Though you're right - measure(key, &Module.func/3, [arg1, arg2, arg3]) would allow us to do basically the same thing using only what's already been written in this PR.

Edited original post to include multiple examples.

@lexmag
Copy link
Owner

lexmag commented Oct 21, 2016

Sorry, my comment is indeed not clear enough.
I meant the following: I think we should not add measure/3 with optional args because it doesn't bring much, we can express any measure/3 call by using today's measure/2, like in your example:

measure("my_key", fn -> Enum.map(my_list, mapper_func) end)

Also it won't compose nicely with options from PR #6; call without function arguments will still require empty args provided if options is needed:

measure("my_key", fn -> ... end, [], [sample_rate: 0.5])

@CJPoll
Copy link
Contributor Author

CJPoll commented Oct 24, 2016

Ah - I wasn't aware of the other PR. That does make things difficult. Adding the args was the use case I was making this PR for. I suppose my apps can just define their own function which is equivalent to measure/3, so it's not a terrible burden on my part to implement in the application layer.

I'll push up the changes later today, then.

@CJPoll
Copy link
Contributor Author

CJPoll commented Oct 27, 2016

I believe that should remove the problem.

My apologies for the delay.

@lexmag
Copy link
Owner

lexmag commented Oct 28, 2016

Merged. Thank you @CJPoll. 💛

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants