Coverage for scheduler/prioritization.py: 100%

21 statements  

« prev     ^ index     » next       coverage.py v7.0.4, created at 2023-03-19 22:02 +0000

1""" 

2Collection of prioritization functions. 

3 

4For compatibility with the |Scheduler|, the prioritization 

5functions have to be of type ``Callable[[float, Job, int, int], float]``. 

6 

7Author: Jendrik A. Potyka, Fabian A. Preiss 

8""" 

9 

10import random 

11 

12from scheduler.threading.job import Job 

13 

14 

15def constant_weight_prioritization( 

16 time_delta: float, job: Job, max_exec: int, job_count: int 

17) -> float: 

18 r""" 

19 Interprets the `Job`'s weight as its priority. 

20 

21 Return the |Job|'s weight for overdue 

22 |Job|\ s, otherwise return zero: 

23 

24 .. math:: 

25 \left(\mathtt{time\_delta},\mathtt{weight}\right)\ {\mapsto}\begin{cases} 

26 0 & :\ \mathtt{time\_delta}<0\\ 

27 \mathtt{weight} & :\ \mathtt{time\_delta}\geq0 

28 \end{cases} 

29 

30 Parameters 

31 ---------- 

32 time_delta : float 

33 The time in seconds that a |Job| is overdue. 

34 job : Job 

35 The |Job| instance 

36 max_exec : int 

37 Limits the number of overdue |Job|\ s that can be executed 

38 by calling function `Scheduler.exec_jobs()`. 

39 job_count : int 

40 Number of scheduled |Job|\ s 

41 

42 Returns 

43 ------- 

44 float 

45 The weight of a |Job| as priority. 

46 """ 

47 _ = max_exec 

48 _ = job_count 

49 if time_delta < 0: 

50 return 0 

51 return job.weight 

52 

53 

54def linear_priority_function(time_delta: float, job: Job, max_exec: int, job_count: int) -> float: 

55 r""" 

56 Compute the |Job|\ s default linear priority. 

57 

58 Linear |Job| prioritization such that the priority increases 

59 linearly with the amount of time that a |Job| is overdue. 

60 At the exact time of the scheduled execution, the priority is equal to the 

61 |Job|\ s weight. 

62 

63 The function is defined as 

64 

65 .. math:: 

66 \left(\mathtt{time\_delta},\mathtt{weight}\right)\ {\mapsto}\begin{cases} 

67 0 & :\ \mathtt{time\_delta}<0\\ 

68 {\left(\mathtt{time\_delta}+1\right)}\cdot\mathtt{weight} & :\ \mathtt{time\_delta}\geq0 

69 \end{cases} 

70 

71 Parameters 

72 ---------- 

73 time_delta : float 

74 The time in seconds that a |Job| is overdue. 

75 job : Job 

76 The |Job| instance 

77 max_exec : int 

78 Limits the number of overdue |Job|\ s that can be executed 

79 by calling function `Scheduler.exec_jobs()`. 

80 job_count : int 

81 Number of scheduled |Job|\ s 

82 

83 Returns 

84 ------- 

85 float 

86 The time dependant priority for a |Job| 

87 """ 

88 _ = max_exec 

89 _ = job_count 

90 

91 if time_delta < 0: 

92 return 0 

93 return (time_delta + 1) * job.weight 

94 

95 

96def random_priority_function(time: float, job: Job, max_exec: int, job_count: int) -> float: 

97 """ 

98 Generate random priority values from weights. 

99 

100 .. warning:: Not suitable for security relevant purposes. 

101 

102 The priority generator will return 1 if the random number 

103 is lower then the |Job|'s weight, otherwise it will return 0. 

104 """ 

105 _ = time 

106 _ = max_exec 

107 _ = job_count 

108 if random.random() < job.weight: # nosec 

109 return 1 

110 return 0