Skip to content
This repository

Performance improvement in date formatting function. #80

Merged
merged 2 commits into from almost 2 years ago

2 participants

Serge Aleynikov Andrew Thompson
Serge Aleynikov

The micro-benchmarks show up to 20x improvement, which further
reduces logging overhead.

saleyn added some commits
Serge Aleynikov saleyn Performance improvement in date formatting function.
The micro-benchmarks show up to 20x improvement, which further
reduces logging overhead.
f52558b
Serge Aleynikov saleyn Added more test cases 3a7b235
Andrew Thompson

This seems to work well, and the speedup is significant. Thanks!

Andrew Thompson Vagabond merged commit 0b797bc into from
Serge Aleynikov

You are welcome!

Serge Aleynikov saleyn deleted the branch
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Showing 2 unique commits by 1 author.

Oct 04, 2012
Serge Aleynikov saleyn Performance improvement in date formatting function.
The micro-benchmarks show up to 20x improvement, which further
reduces logging overhead.
f52558b
Serge Aleynikov saleyn Added more test cases 3a7b235
This page is out of date. Refresh to see the latest.

Showing 1 changed file with 42 additions and 8 deletions. Show diff stats Hide diff stats

  1. +42 8 src/lager_util.erl
50 src/lager_util.erl
@@ -135,17 +135,17 @@ format_time() ->
135 135 format_time(maybe_utc(localtime_ms())).
136 136
137 137 format_time({utc, {{Y, M, D}, {H, Mi, S, Ms}}}) ->
138   - {io_lib:format("~b-~2..0b-~2..0b", [Y, M, D]),
139   - io_lib:format("~2..0b:~2..0b:~2..0b.~3..0b UTC", [H, Mi, S, Ms])};
  138 + {[integer_to_list(Y), $-, i2l(M), $-, i2l(D)],
  139 + [i2l(H), $:, i2l(Mi), $:, i2l(S), $., i3l(Ms), $ , $U, $T, $C]};
140 140 format_time({{Y, M, D}, {H, Mi, S, Ms}}) ->
141   - {io_lib:format("~b-~2..0b-~2..0b", [Y, M, D]),
142   - io_lib:format("~2..0b:~2..0b:~2..0b.~3..0b", [H, Mi, S, Ms])};
  141 + {[integer_to_list(Y), $-, i2l(M), $-, i2l(D)],
  142 + [i2l(H), $:, i2l(Mi), $:, i2l(S), $., i3l(Ms)]};
143 143 format_time({utc, {{Y, M, D}, {H, Mi, S}}}) ->
144   - {io_lib:format("~b-~2..0b-~2..0b", [Y, M, D]),
145   - io_lib:format("~2..0b:~2..0b:~2..0b UTC", [H, Mi, S])};
  144 + {[integer_to_list(Y), $-, i2l(M), $-, i2l(D)],
  145 + [i2l(H), $:, i2l(Mi), $:, i2l(S), $ , $U, $T, $C]};
146 146 format_time({{Y, M, D}, {H, Mi, S}}) ->
147   - {io_lib:format("~b-~2..0b-~2..0b", [Y, M, D]),
148   - io_lib:format("~2..0b:~2..0b:~2..0b", [H, Mi, S])}.
  147 + {[integer_to_list(Y), $-, i2l(M), $-, i2l(D)],
  148 + [i2l(H), $:, i2l(Mi), $:, i2l(S)]}.
149 149
150 150 parse_rotation_day_spec([], Res) ->
151 151 {ok, Res ++ [{hour, 0}]};
@@ -334,6 +334,11 @@ is_loggable(Msg ,SeverityThreshold,MyName) ->
334 334 lager_msg:severity_as_int(Msg) =< SeverityThreshold orelse
335 335 lists:member(MyName, lager_msg:destinations(Msg)).
336 336
  337 +i2l(I) when I < 10 -> [$0, $0+I];
  338 +i2l(I) -> integer_to_list(I).
  339 +i3l(I) when I < 100 -> [$0 | i2l(I)];
  340 +i3l(I) -> integer_to_list(I).
  341 +
337 342 -ifdef(TEST).
338 343
339 344 parse_test() ->
@@ -470,4 +475,33 @@ is_loggable_test_() ->
470 475 {"Loggable by destination overriding severity", ?_assert(is_loggable(lager_msg:new("",{"",""}, critical, [], [me]),1,me))}
471 476 ].
472 477
  478 +format_time_test_() ->
  479 + [
  480 + ?_assertEqual("2012-10-04 11:16:23.002",
  481 + begin
  482 + {D, T} = format_time({{2012,10,04},{11,16,23,2}}),
  483 + lists:flatten([D,$ ,T])
  484 + end),
  485 + ?_assertEqual("2012-10-04 11:16:23.999",
  486 + begin
  487 + {D, T} = format_time({{2012,10,04},{11,16,23,999}}),
  488 + lists:flatten([D,$ ,T])
  489 + end),
  490 + ?_assertEqual("2012-10-04 11:16:23",
  491 + begin
  492 + {D, T} = format_time({{2012,10,04},{11,16,23}}),
  493 + lists:flatten([D,$ ,T])
  494 + end),
  495 + ?_assertEqual("2012-10-04 00:16:23.092 UTC",
  496 + begin
  497 + {D, T} = format_time({utc, {{2012,10,04},{0,16,23,92}}}),
  498 + lists:flatten([D,$ ,T])
  499 + end),
  500 + ?_assertEqual("2012-10-04 11:16:23 UTC",
  501 + begin
  502 + {D, T} = format_time({utc, {{2012,10,04},{11,16,23}}}),
  503 + lists:flatten([D,$ ,T])
  504 + end)
  505 + ].
  506 +
473 507 -endif.

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.