Skip to content

Commit

Permalink
engine: Randomize timeline log level with math
Browse files Browse the repository at this point in the history
Simplify the code and eliminate unwanted branches from the engine loop
by drawing a random timeline level from a log-uniform distribution that
mathematically favors higher log levels over lower ones.

Plucked log5() out of the air i.e. each log level should be enabled for
5x more breaths than the one below.

Here is how the distribution of log level choice looks in practice using
this algorithm:

    > t = {0,0,0,0,0,0,0,0,0}
    > for i = 1, 1e8 do
         local n = math.max(1,math.ceil(math.log(math.random(5^9))/math.log(5)))
         t[n] = t[n]+1
      end
    > for i,n in ipairs(t) do print(i,n) end
    1       560
    2       2151
    3       10886
    4       55149
    5       273376
    6       1367410
    7       6844261
    8       34228143
    9       171120244

Note: Lua provides only natural logarithm functions but it is easy to
derive other bases from this (google "log change of base formula").
  • Loading branch information
lukego committed Feb 21, 2017
1 parent 3bb71fa commit ffb7379
Showing 1 changed file with 8 additions and 11 deletions.
19 changes: 8 additions & 11 deletions src/core/app.lua
Expand Up @@ -444,17 +444,14 @@ function breathe ()
counter.commit()
events.commited_counters()
end
-- Sample events with dynamic priorities.
-- Lower priorities are enabled 1/10th as often as the one above.
local r = math.random()
if r < 0.000001 then timeline_mod.level(timeline_log, 1)
elseif r < 0.000010 then timeline_mod.level(timeline_log, 2)
elseif r < 0.000100 then timeline_mod.level(timeline_log, 3)
elseif r < 0.001000 then timeline_mod.level(timeline_log, 4)
elseif r < 0.010000 then timeline_mod.level(timeline_log, 5)
elseif r < 0.100000 then timeline_mod.level(timeline_log, 6)
else timeline_mod.level(timeline_log, 7)
end
-- Randomize the log level. Enable each level in 5x more breaths
-- than the level below by randomly picking from log5() distribution.
-- Goal is ballpark 1000 messages per second (~15min for 1M entries.)
--
-- Could be better to reduce the log level over time to "stretch"
-- logs for long running processes? Improvements possible :-).
local level = math.max(1, math.ceil(math.log(math.random(5^9))/math.log(5)))
timeline_mod.level(timeline_log, level)
running = false
end

Expand Down

0 comments on commit ffb7379

Please sign in to comment.