Last active
May 22, 2018 16:28
-
-
Save nitinbhojwani/42b6ac94245f50f5bb21d2dec303d836 to your computer and use it in GitHub Desktop.
Schedule any python script or steps continuously & Example of Measure Disk Speed
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 os, time, sched | |
| MB = 1048576 | |
| def measure_write_speed(file_path): | |
| string = "Let's test write speed..." | |
| n = (100 * MB) // len(string) | |
| start = time.time() | |
| file = open(file_path, 'w') | |
| for i in range(n): | |
| file.write(string) | |
| end = time.time() | |
| print(n*len(string)) | |
| print(start) | |
| print(end) | |
| speed = (n * len(string)) / (MB * (end - start)) | |
| return speed | |
| def measure_read_speed(file_path): | |
| start = time.time() | |
| print(start) | |
| with open(file_path, 'r') as content_file: | |
| content = content_file.read() | |
| end = time.time() | |
| print(end) | |
| bytes_read = len(content) | |
| speed = bytes_read / (MB * (end - start)) | |
| return speed | |
| def delete_file(file_path): | |
| print(file_path) | |
| print('Deleting the file at {}'.format(file_path)) | |
| os.remove(file_path) | |
| def main(sc): | |
| try: | |
| path = "/tmp/" | |
| file_name = "test-write-speed-%s.txt" % int(time.time() * 1000) | |
| file_path = os.path.join(path, file_name) | |
| print("starting disk measurement... at time:", time.time()) | |
| write_speed = measure_write_speed(file_path) | |
| print('Write speed: {} mbps'.format(write_speed)) | |
| read_speed = measure_write_speed(file_path) | |
| print('Read speed: {} mbps'.format(read_speed)) | |
| delete_file(file_path) | |
| except Exception as e: | |
| print('Exception Occured', e) | |
| s.enter(30, 1, main, (sc,)) | |
| if __name__ == '__main__': | |
| s = sched.scheduler(time.time, time.sleep) | |
| s.enter(30, 1, main, (s,)) | |
| s.run() |
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 time, sched | |
| # | |
| # Define steps functions here | |
| # | |
| # main function | |
| def main(sc): | |
| try: | |
| # Call steps functions here | |
| except Exception as e: | |
| print('Exception Occured', e) | |
| s.enter(30, 1, main, (sc,)) | |
| if __name__ == '__main__': | |
| s = sched.scheduler(time.time, time.sleep) | |
| s.enter(30, 1, main, (s,)) | |
| s.run() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment