You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@couchdb.apache.org by chewbranca <gi...@git.apache.org> on 2015/08/17 22:22:30 UTC

[GitHub] couchdb-couch-stats pull request: We already know the metric type,...

GitHub user chewbranca opened a pull request:

    https://github.com/apache/couchdb-couch-stats/pull/7

    We already know the metric type, so supply it directly

    When we don't supply the metric type Folsom has to go and look it up every time, but we already know what type it is, so we can avoid a lot of unnecessary computation by supplying it directly.

You can merge this pull request into a Git repository by running:

    $ git pull https://github.com/apache/couchdb-couch-stats use-notify-existing-metric

Alternatively you can review and apply these changes as the patch at:

    https://github.com/apache/couchdb-couch-stats/pull/7.patch

To close this pull request, make a commit to your master/trunk branch
with (at least) the following in the commit message:

    This closes #7
    
----
commit 9ba5201399d8b277940d049c3d712e0f5de83c70
Author: Russell Branca <ch...@apache.org>
Date:   2015-08-14T23:17:46Z

    We already know the metric type, so supply it directly

----


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] couchdb-couch-stats pull request: We already know the metric type,...

Posted by asfgit <gi...@git.apache.org>.
Github user asfgit closed the pull request at:

    https://github.com/apache/couchdb-couch-stats/pull/7


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] couchdb-couch-stats pull request: We already know the metric type,...

Posted by chewbranca <gi...@git.apache.org>.
Github user chewbranca commented on a diff in the pull request:

    https://github.com/apache/couchdb-couch-stats/pull/7#discussion_r37247683
  
    --- Diff: src/couch_stats.erl ---
    @@ -97,27 +97,26 @@ update_histogram(Name, Fun) when is_function(Fun, 0) ->
         Begin = os:timestamp(),
         Result = Fun(),
         Duration = timer:now_diff(os:timestamp(), Begin) div 1000,
    -    case notify(Name, Duration) of
    +    case notify_existing_metric(Name, Duration, histogram) of
             ok ->
                 Result;
             {error, unknown_metric} ->
                 throw({unknown_metric, Name})
         end;
     update_histogram(Name, Value) when is_number(Value) ->
    -    notify(Name, Value).
    +    notify_existing_metric(Name, Value, histogram).
     
     -spec update_gauge(any(), number()) -> response().
     update_gauge(Name, Value) ->
    -    notify(Name, Value).
    -
    --spec notify(any(), any()) -> response().
    -notify(Name, Op) ->
    -    case folsom_metrics:notify(Name, Op) of
    -        ok ->
    -            ok;
    -        _ ->
    -            couch_log:notice("unknown metric: ~p", [Name]),
    -            {error, unknown_metric}
    +    notify_existing_metric(Name, Value, gauge).
    +
    +-spec notify_existing_metric(any(), any(), any()) -> response().
    +notify_existing_metric(Name, Op, Type) ->
    +    try
    +        ok = folsom_metrics:notify_existing_metric(Name, Op, Type)
    +    catch _:_ ->
    --- End diff --
    
    See my details below, the problem is we're not doing a simple check, we're doing an inefficient construction of an intermediary data structure only to then search back through it.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] couchdb-couch-stats pull request: We already know the metric type,...

Posted by rnewson <gi...@git.apache.org>.
Github user rnewson commented on the pull request:

    https://github.com/apache/couchdb-couch-stats/pull/7#issuecomment-133013957
  
    +1


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] couchdb-couch-stats pull request: We already know the metric type,...

Posted by chewbranca <gi...@git.apache.org>.
Github user chewbranca commented on the pull request:

    https://github.com/apache/couchdb-couch-stats/pull/7#issuecomment-131987851
  
    We do a lot of unnecessary computation, for instance https://github.com/apache/couchdb-folsom/blob/master/src/folsom_ets.erl#L95-L96 is called twice and even worse we construct a proplist in https://github.com/apache/couchdb-folsom/blob/master/src/folsom_ets.erl#L140-L141 to then immediately after search through the proplist to get the value: https://github.com/apache/couchdb-folsom/blob/master/src/folsom_ets.erl#L107-L108.
    
    Granted, we could make `folsom_ets.erl` more efficient, but not doing the work in the first place seems like the best approach.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] couchdb-couch-stats pull request: We already know the metric type,...

Posted by kxepal <gi...@git.apache.org>.
Github user kxepal commented on a diff in the pull request:

    https://github.com/apache/couchdb-couch-stats/pull/7#discussion_r37242598
  
    --- Diff: src/couch_stats.erl ---
    @@ -97,27 +97,26 @@ update_histogram(Name, Fun) when is_function(Fun, 0) ->
         Begin = os:timestamp(),
         Result = Fun(),
         Duration = timer:now_diff(os:timestamp(), Begin) div 1000,
    -    case notify(Name, Duration) of
    +    case notify_existing_metric(Name, Duration, histogram) of
             ok ->
                 Result;
             {error, unknown_metric} ->
                 throw({unknown_metric, Name})
         end;
     update_histogram(Name, Value) when is_number(Value) ->
    -    notify(Name, Value).
    +    notify_existing_metric(Name, Value, histogram).
     
     -spec update_gauge(any(), number()) -> response().
     update_gauge(Name, Value) ->
    -    notify(Name, Value).
    -
    --spec notify(any(), any()) -> response().
    -notify(Name, Op) ->
    -    case folsom_metrics:notify(Name, Op) of
    -        ok ->
    -            ok;
    -        _ ->
    -            couch_log:notice("unknown metric: ~p", [Name]),
    -            {error, unknown_metric}
    +    notify_existing_metric(Name, Value, gauge).
    +
    +-spec notify_existing_metric(any(), any(), any()) -> response().
    +notify_existing_metric(Name, Op, Type) ->
    +    try
    +        ok = folsom_metrics:notify_existing_metric(Name, Op, Type)
    +    catch _:_ ->
    --- End diff --
    
    Ah, I see. I actually worry about that try/catch will cause more overhead than simple check.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] couchdb-couch-stats pull request: We already know the metric type,...

Posted by kxepal <gi...@git.apache.org>.
Github user kxepal commented on a diff in the pull request:

    https://github.com/apache/couchdb-couch-stats/pull/7#discussion_r37232899
  
    --- Diff: src/couch_stats.erl ---
    @@ -97,27 +97,26 @@ update_histogram(Name, Fun) when is_function(Fun, 0) ->
         Begin = os:timestamp(),
         Result = Fun(),
         Duration = timer:now_diff(os:timestamp(), Begin) div 1000,
    -    case notify(Name, Duration) of
    +    case notify_existing_metric(Name, Duration, histogram) of
             ok ->
                 Result;
             {error, unknown_metric} ->
                 throw({unknown_metric, Name})
         end;
     update_histogram(Name, Value) when is_number(Value) ->
    -    notify(Name, Value).
    +    notify_existing_metric(Name, Value, histogram).
     
     -spec update_gauge(any(), number()) -> response().
     update_gauge(Name, Value) ->
    -    notify(Name, Value).
    -
    --spec notify(any(), any()) -> response().
    -notify(Name, Op) ->
    -    case folsom_metrics:notify(Name, Op) of
    -        ok ->
    -            ok;
    -        _ ->
    -            couch_log:notice("unknown metric: ~p", [Name]),
    -            {error, unknown_metric}
    +    notify_existing_metric(Name, Value, gauge).
    +
    +-spec notify_existing_metric(any(), any(), any()) -> response().
    +notify_existing_metric(Name, Op, Type) ->
    +    try
    +        ok = folsom_metrics:notify_existing_metric(Name, Op, Type)
    +    catch _:_ ->
    --- End diff --
    
    Why try/catch? `notify_existing_metric` doesn't throws anything, but returns useful `{error, Type, unsupported_metric_type}` in case of unknown metric.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] couchdb-couch-stats pull request: We already know the metric type,...

Posted by chewbranca <gi...@git.apache.org>.
Github user chewbranca commented on a diff in the pull request:

    https://github.com/apache/couchdb-couch-stats/pull/7#discussion_r37242358
  
    --- Diff: src/couch_stats.erl ---
    @@ -97,27 +97,26 @@ update_histogram(Name, Fun) when is_function(Fun, 0) ->
         Begin = os:timestamp(),
         Result = Fun(),
         Duration = timer:now_diff(os:timestamp(), Begin) div 1000,
    -    case notify(Name, Duration) of
    +    case notify_existing_metric(Name, Duration, histogram) of
             ok ->
                 Result;
             {error, unknown_metric} ->
                 throw({unknown_metric, Name})
         end;
     update_histogram(Name, Value) when is_number(Value) ->
    -    notify(Name, Value).
    +    notify_existing_metric(Name, Value, histogram).
     
     -spec update_gauge(any(), number()) -> response().
     update_gauge(Name, Value) ->
    -    notify(Name, Value).
    -
    --spec notify(any(), any()) -> response().
    -notify(Name, Op) ->
    -    case folsom_metrics:notify(Name, Op) of
    -        ok ->
    -            ok;
    -        _ ->
    -            couch_log:notice("unknown metric: ~p", [Name]),
    -            {error, unknown_metric}
    +    notify_existing_metric(Name, Value, gauge).
    +
    +-spec notify_existing_metric(any(), any(), any()) -> response().
    +notify_existing_metric(Name, Op, Type) ->
    +    try
    +        ok = folsom_metrics:notify_existing_metric(Name, Op, Type)
    +    catch _:_ ->
    --- End diff --
    
    Oh it definitely can throw things:
    
    ```
    (node1@127.0.0.1)12> folsom_metrics:notify_existing_metric([ddoc_cachedd, miss], {inc, 1}, counter).
    ** exception error: bad argument
         in function  ets:update_counter/3
            called as ets:update_counter(folsom_counters,{[ddoc_cachedd,miss],2},1)
         in call from folsom_metrics_counter:inc/2 (src/folsom_metrics_counter.erl, line 48)
         in call from folsom_ets:notify/4 (src/folsom_ets.erl, line 362)
    (node1@127.0.0.1)13> folsom_metrics:notify_existing_metric([ddoc_cache, miss], {inc, 1}, counter).
    ok
    ```
    
    Basically we're skipping this check because it *should* exist: https://github.com/apache/couchdb-folsom/blob/master/src/folsom_ets.erl#L105-L112


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] couchdb-couch-stats pull request: We already know the metric type,...

Posted by rnewson <gi...@git.apache.org>.
Github user rnewson commented on the pull request:

    https://github.com/apache/couchdb-couch-stats/pull/7#issuecomment-131975236
  
    how much does not doing passing that info cost us?


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---