Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Performance improvement in date formatting function.

The micro-benchmarks show up to 20x improvement, which further
reduces logging overhead.
  • Loading branch information...
commit f52558b5589a8338b7059c3f9976fcc46a2afc87 1 parent 9981ca0
@saleyn authored
Showing with 37 additions and 8 deletions.
  1. +37 −8 src/lager_util.erl
View
45 src/lager_util.erl
@@ -135,17 +135,17 @@ format_time() ->
format_time(maybe_utc(localtime_ms())).
format_time({utc, {{Y, M, D}, {H, Mi, S, Ms}}}) ->
- {io_lib:format("~b-~2..0b-~2..0b", [Y, M, D]),
- io_lib:format("~2..0b:~2..0b:~2..0b.~3..0b UTC", [H, Mi, S, Ms])};
+ {[integer_to_list(Y), $-, i2l(M), $-, i2l(D)],
+ [i2l(H), $:, i2l(Mi), $:, i2l(S), $., i3l(Ms), $ , $U, $T, $C]};
format_time({{Y, M, D}, {H, Mi, S, Ms}}) ->
- {io_lib:format("~b-~2..0b-~2..0b", [Y, M, D]),
- io_lib:format("~2..0b:~2..0b:~2..0b.~3..0b", [H, Mi, S, Ms])};
+ {[integer_to_list(Y), $-, i2l(M), $-, i2l(D)],
+ [i2l(H), $:, i2l(Mi), $:, i2l(S), $., i3l(Ms)]};
format_time({utc, {{Y, M, D}, {H, Mi, S}}}) ->
- {io_lib:format("~b-~2..0b-~2..0b", [Y, M, D]),
- io_lib:format("~2..0b:~2..0b:~2..0b UTC", [H, Mi, S])};
+ {[integer_to_list(Y), $-, i2l(M), $-, i2l(D)],
+ [i2l(H), $:, i2l(Mi), $:, i2l(S), $ , $U, $T, $C]};
format_time({{Y, M, D}, {H, Mi, S}}) ->
- {io_lib:format("~b-~2..0b-~2..0b", [Y, M, D]),
- io_lib:format("~2..0b:~2..0b:~2..0b", [H, Mi, S])}.
+ {[integer_to_list(Y), $-, i2l(M), $-, i2l(D)],
+ [i2l(H), $:, i2l(Mi), $:, i2l(S)]}.
parse_rotation_day_spec([], Res) ->
{ok, Res ++ [{hour, 0}]};
@@ -334,6 +334,11 @@ is_loggable(Msg ,SeverityThreshold,MyName) ->
lager_msg:severity_as_int(Msg) =< SeverityThreshold orelse
lists:member(MyName, lager_msg:destinations(Msg)).
+i2l(I) when I < 10 -> [$0, $0+I];
+i2l(I) -> integer_to_list(I).
+i3l(I) when I < 100 -> [$0 | i2l(I)];
+i3l(I) -> integer_to_list(I).
+
-ifdef(TEST).
parse_test() ->
@@ -470,4 +475,28 @@ is_loggable_test_() ->
{"Loggable by destination overriding severity", ?_assert(is_loggable(lager_msg:new("",{"",""}, critical, [], [me]),1,me))}
].
+format_time_test_() ->
+ [
+ ?_assertEqual("2012-10-04 11:16:23.892",
+ begin
+ {D, T} = format_time({{2012,10,04},{11,16,23,892}}),
+ lists:flatten([D,$ ,T])
+ end),
+ ?_assertEqual("2012-10-04 11:16:23",
+ begin
+ {D, T} = format_time({{2012,10,04},{11,16,23}}),
+ lists:flatten([D,$ ,T])
+ end),
+ ?_assertEqual("2012-10-04 11:16:23.892 UTC",
+ begin
+ {D, T} = format_time({utc, {{2012,10,04},{11,16,23,892}}}),
+ lists:flatten([D,$ ,T])
+ end),
+ ?_assertEqual("2012-10-04 11:16:23 UTC",
+ begin
+ {D, T} = format_time({utc, {{2012,10,04},{11,16,23}}}),
+ lists:flatten([D,$ ,T])
+ end)
+ ].
+
-endif.
Please sign in to comment.
Something went wrong with that request. Please try again.