From ffb7379c7e5463084da817d3a1e4976e51c0088b Mon Sep 17 00:00:00 2001 From: Luke Gorrie Date: Tue, 21 Feb 2017 08:50:08 +0000 Subject: [PATCH] engine: Randomize timeline log level with math 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"). --- src/core/app.lua | 19 ++++++++----------- 1 file changed, 8 insertions(+), 11 deletions(-) diff --git a/src/core/app.lua b/src/core/app.lua index 11a8e5fea3..e07f6e98bc 100644 --- a/src/core/app.lua +++ b/src/core/app.lua @@ -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