Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
#!/usr/bin/env python
import os
import sys
import math
import random
NSECONDS = 5*3600
NNODES = 4000
#random_interval_max = 3600
random_interval_max = 1800
y = [ 0 for i in range( NSECONDS ) ]
nodestates = []
#random.seed( 1 )
ALGORITHM = 'INTERVAL'
#ALGORITHM = 'HOURLY'
def updateSchedule( t, state ):
currentHour = t / 3600
currentOffset = t % 3600
if state['schedule'] is not None and state['schedule']['hour'] == currentHour:
return
nrunsperhour = 6.0
nruns = int(nrunsperhour * (3600. - float(currentOffset)) / 3600.)
offsets = [random.randint(min(3599, currentOffset + 1), 3599) for i in range(nruns)]
offsets.sort()
state['schedule'] = {'hour': currentHour,
'offsets': offsets}
def runThisTime( t, state ):
currentHour = t / 3600
currentOffset = t % 3600
nexttime = None
if len(state['schedule']['offsets']) > 0:
offset = state['schedule']['offsets'][0]
stime = 3600 * state['schedule']['hour'] + offset
if t >= stime:
nexttime = stime
if nexttime is not None:
state['schedule']['offsets'].pop(0)
return True
return False
for i in range( NNODES ):
nodestates.append( { 'next_run_time': 0,
'schedule': None } )
for i in range( NSECONDS ):
runCount = 0
if ( i + 1 ) % 60 == 1:
# Simulate cron running every minute
for j in range( NNODES ):
state = nodestates[j]
if ALGORITHM == 'INTERVAL':
if i >= state['next_run_time']:
#delay = random.randint( 0, 60 )
delay = 0
if i + delay < NSECONDS:
y[i+delay] += 1
state['next_run_time'] = i + random.randint( 1, random_interval_max )
elif ALGORITHM == 'HOURLY':
updateSchedule( i, state )
if runThisTime( i, state ):
y[i] += 1
ofile = file( "mcsentinel-out.txt", "w" )
for i in range( 1, len( y ) ):
ofile.write( "%d %d\n" % ( i, y[i] ) )
ofile.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.