Coverage for scheduler/prioritization.py: 100%

22 statements  

« prev     ^ index     » next       coverage.py v7.6.10, created at 2025-01-21 13:55 +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.base.job import BaseJobType 

13from scheduler.threading.job import Job 

14 

15 

16def constant_weight_prioritization( 

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

18) -> float: 

19 r""" 

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

21 

22 Return the |Job|'s weight for overdue 

23 |Job|\ s, otherwise return zero: 

24 

25 .. math:: 

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

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

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

29 \end{cases} 

30 

31 Parameters 

32 ---------- 

33 time_delta : float 

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

35 job : Job 

36 The |Job| instance 

37 max_exec : int 

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

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

40 job_count : int 

41 Number of scheduled |Job|\ s 

42 

43 Returns 

44 ------- 

45 float 

46 The weight of a |Job| as priority. 

47 """ 

48 _ = max_exec 

49 _ = job_count 

50 if time_delta < 0: 

51 return 0 

52 return job.weight 

53 

54 

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

56 r""" 

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

58 

59 Linear |Job| prioritization such that the priority increases 

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

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

62 |Job|\ s weight. 

63 

64 The function is defined as 

65 

66 .. math:: 

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

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

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

70 \end{cases} 

71 

72 Parameters 

73 ---------- 

74 time_delta : float 

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

76 job : Job 

77 The |Job| instance 

78 max_exec : int 

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

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

81 job_count : int 

82 Number of scheduled |Job|\ s 

83 

84 Returns 

85 ------- 

86 float 

87 The time dependant priority for a |Job| 

88 """ 

89 _ = max_exec 

90 _ = job_count 

91 

92 if time_delta < 0: 

93 return 0 

94 return (time_delta + 1) * job.weight 

95 

96 

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

98 """ 

99 Generate random priority values from weights. 

100 

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

102 

103 The priority generator will return 1 if the random number 

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

105 """ 

106 _ = time 

107 _ = max_exec 

108 _ = job_count 

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

110 return 1 

111 return 0