Created
December 2, 2019 09:08
-
-
Save seriousManual/0cff44749465df09fc13627e6f772ca8 to your computer and use it in GitHub Desktop.
workload generator
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import * as moment from 'moment'; | |
import * as ms from 'ms'; | |
function alignToInterval(time: moment.Moment, interval: string): moment.Moment { | |
const intervalMS = ms(interval); | |
const slot = Math.floor((time.unix() * 1000) / intervalMS) * intervalMS; | |
return moment.utc(slot); | |
} | |
function getRelativeBeginEnd(instantiationInterval, executionInterval, distributionTimespan, now = moment()): [number, number] { | |
// "rounds" the time to the slot, e.g. with a slot of 5m the time 00:06:13 becomes 00:05:00 | |
const currentInstantiationSlot = alignToInterval(now, instantiationInterval); | |
const currentExecutionSlot = alignToInterval(now, executionInterval); | |
const distributionSlot = alignToInterval(now, distributionTimespan); | |
// figure out if the function should actually be instantiated by checking if the current instantiationInterval aligns with the executionInterval | |
if (!currentInstantiationSlot.isSame(currentExecutionSlot)) { | |
return null; | |
} | |
const lengthExecutionSlot = ms(executionInterval); | |
const lengthDistributionTimespan = ms(distributionTimespan); | |
// since we now know the current execution slot and its length, we can calculate the relative distribution, relating to the overall distribution | |
// example: task X should be executed every 15 minutes, generating work items for all users so that every user is handled equally over a period of 1 hour | |
// for task X then 4 executions happen every hour, with the according distribution | |
// for the hour of 0 o'clock this would look like this: | |
// 00:00:00, relativeBegin: 0, relativeEnd: 0.25 | |
// 00:15:00, relativeBegin: 0.25, relativeEnd: 0.5 | |
// 00:30:00, relativeBegin: 0.5, relativeEnd: 0.75 | |
// 00:45:00, relativeBegin: 0.75, relativeEnd: 1 | |
const diffSinceDistributionSlotBegin = currentExecutionSlot.diff(distributionSlot, 'ms'); | |
const relativeBegin = diffSinceDistributionSlotBegin / lengthDistributionTimespan; | |
const relativeEnd = relativeBegin + (lengthExecutionSlot / lengthDistributionTimespan); | |
return [relativeBegin, relativeEnd]; | |
} | |
// the workload generator is triggered every 5minutes | |
const instantiationInterval = '5m'; | |
// the current task should be executed every 15minutes | |
const executionInterval = '15m'; | |
// the workload for all users should be spread out over a time range of one day | |
const distributionTimespan = '1d'; | |
const result = getRelativeBeginEnd(instantiationInterval, executionInterval, distributionTimespan, moment('2019-12-02T00:31:05')); | |
if (result) { | |
const [relativeBegin, relativeEnd] = result; | |
console.log({ | |
relativeBegin, | |
relativeEnd, | |
}); | |
// now querying for all users that fall into that slot: | |
const query = `SELECT * FROM users WHERE distributionIdentifier >= ${relativeBegin} AND distributionIdentifier < ${relativeEnd}`; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment