1#!/usr/bin/env python3

2

3 import sys

4 import argparse

5 from statistics import mean

6

7

8 def main():

9 parser = argparse.ArgumentParser()

10 parser.add_argument(

11 "filename",

12 action="store",

13 help="The output file from sleep tests to parse",

14 )

15 parser.add_argument(

16 "--s",

17 dest="sleep_threshold",

18 action="store",

19 type=float,

20 default=10.00,

21 help=(

22 "The max time a system should have taken to "

23 "enter a sleep state. (Default: %(default)s)"

24 ),

25 )

26 parser.add_argument(

27 "--r",

28 action="store",

29 dest="resume_threshold",

30 type=float,

31 default=5.00,

32 help=(

33 "The max time a system should have taken to "

34 "resume from a sleep state. (Default: "

35 "%(default)s)"

36 ),

37 )

38 args = parser.parse_args()

39

40 try:

41 with open(args.filename) as file:

42 lines = file.readlines()

43 except IOError as e:

44 print(e)

45 return 1

46

47 sleep_time = None

48 sleep_times = []

49 resume_time = None

50 resume_times = []

51 failed = 0

52 # find our times

53 for line in lines:

54 # Warning message from fwts_test.py

55 if "Warning: Time to" in line:

56 print(line)

57 failed = 1

58 if "Average time to sleep" in line:

59 print(line)

60 try:

61 sleep_time = float(line.split(":")[1].strip())

62 sleep_times.append(sleep_time)

63 except ValueError as e:

64 print(

65 (

66 "ERROR: One or more sleep times was not reported "

67 "correctly:"

68 )

69 )

70 print(e)

71 failed = 1

72 elif "Average time to resume" in line:

73 print(line)

74 try:

75 resume_time = float(line.split(":")[1].strip())

76 resume_times.append(resume_time)

77 except ValueError as e:

78 print(

79 (

80 "ERROR: One or more resume times was not reported "

81 "correctly:"

82 )

83 )

84 print(e)

85 failed = 1

86

87 print()

88 if sleep_times:

89 print("=================================================")

90 print(

91 "Average time to enter sleep state: %.4f seconds"

92 % mean(sleep_times)

93 )

94 if max(sleep_times) > args.sleep_threshold:

95 print(

96 "System failed to suspend in less than %s seconds"

97 % args.sleep_threshold

98 )

99 failed = 1

100 if min(sleep_times) <= 0.0:

101 print("ERROR: One or more sleep times was not reported correctly")

102 failed = 1

103 print("=================================================")

104 if resume_times:

105 print("=================================================")

106 print(

107 "Average time to resume from sleep state: %.4f seconds"

108 % mean(resume_times)

109 )

110 if max(resume_times) > args.resume_threshold:

111 print(

112 "System failed to resume in less than %s seconds"

113 % args.resume_threshold

114 )

115 failed = 1

116 if min(resume_times) <= 0.0:

117 print("ERROR: One or more resume times was not reported correctly")

118 failed = 1

119 print("=================================================")

120

121 return failed

122

123

124 if __name__ == "__main__":

125 sys.exit(main())