1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
|
import os
import json
import unittest
import jc.parsers.time
THIS_DIR = os.path.dirname(os.path.abspath(__file__))
class MyTests(unittest.TestCase):
# input
with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/ubuntu-18.04/time.out'), 'r', encoding='utf-8') as f:
ubuntu_18_4_time = f.read()
with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/ubuntu-18.04/time2.out'), 'r', encoding='utf-8') as f:
ubuntu_18_4_time2 = f.read()
with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/ubuntu-18.04/time-p.out'), 'r', encoding='utf-8') as f:
ubuntu_18_4_time_p = f.read()
with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/ubuntu-18.04/time-verbose.out'), 'r', encoding='utf-8') as f:
ubuntu_18_4_time_verbose = f.read()
with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/osx-10.14.6/time.out'), 'r', encoding='utf-8') as f:
osx_10_14_6_time = f.read()
with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/osx-10.14.6/time-l.out'), 'r', encoding='utf-8') as f:
osx_10_14_6_time_l = f.read()
with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/osx-10.14.6/time-p.out'), 'r', encoding='utf-8') as f:
osx_10_14_6_time_p = f.read()
with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/osx-10.14.6/time-lp.out'), 'r', encoding='utf-8') as f:
osx_10_14_6_time_lp = f.read()
# output
with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/ubuntu-18.04/time.json'), 'r', encoding='utf-8') as f:
ubuntu_18_4_time_json = json.loads(f.read())
with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/ubuntu-18.04/time2.json'), 'r', encoding='utf-8') as f:
ubuntu_18_4_time2_json = json.loads(f.read())
with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/ubuntu-18.04/time-p.json'), 'r', encoding='utf-8') as f:
ubuntu_18_4_time_p_json = json.loads(f.read())
with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/ubuntu-18.04/time-verbose.json'), 'r', encoding='utf-8') as f:
ubuntu_18_4_time_verbose_json = json.loads(f.read())
with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/osx-10.14.6/time.json'), 'r', encoding='utf-8') as f:
osx_10_14_6_time_json = json.loads(f.read())
with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/osx-10.14.6/time-l.json'), 'r', encoding='utf-8') as f:
osx_10_14_6_time_l_json = json.loads(f.read())
with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/osx-10.14.6/time-p.json'), 'r', encoding='utf-8') as f:
osx_10_14_6_time_p_json = json.loads(f.read())
with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/osx-10.14.6/time-lp.json'), 'r', encoding='utf-8') as f:
osx_10_14_6_time_lp_json = json.loads(f.read())
def test_time_nodata(self):
"""
Test plain 'time' with no data
"""
self.assertEqual(jc.parsers.time.parse('', quiet=True), {})
def test_time_ubuntu_18_4(self):
"""
Test plain 'time' on Ubuntu 18.4
"""
self.assertEqual(jc.parsers.time.parse(self.ubuntu_18_4_time, quiet=True), self.ubuntu_18_4_time_json)
def test_time2_ubuntu_18_4(self):
"""
Test plain 'time' on Ubuntu 18.4 with ? in CPU%
"""
self.assertEqual(jc.parsers.time.parse(self.ubuntu_18_4_time2, quiet=True), self.ubuntu_18_4_time2_json)
def test_time_p_ubuntu_18_4(self):
"""
Test 'time -p' on Ubuntu 18.4
"""
self.assertEqual(jc.parsers.time.parse(self.ubuntu_18_4_time_p, quiet=True), self.ubuntu_18_4_time_p_json)
def test_time_verbose_ubuntu_18_4(self):
"""
Test 'time --verbose' on Ubuntu 18.4
"""
self.assertEqual(jc.parsers.time.parse(self.ubuntu_18_4_time_verbose, quiet=True), self.ubuntu_18_4_time_verbose_json)
def test_time_osx_10_14_6(self):
"""
Test plain 'time' on OSX 10.14.6
"""
self.assertEqual(jc.parsers.time.parse(self.osx_10_14_6_time, quiet=True), self.osx_10_14_6_time_json)
def test_time_l_osx_10_14_6(self):
"""
Test 'time -l' on OSX 10.14.6
"""
self.assertEqual(jc.parsers.time.parse(self.osx_10_14_6_time_l, quiet=True), self.osx_10_14_6_time_l_json)
def test_time_p_osx_10_14_6(self):
"""
Test 'time -p' on OSX 10.14.6
"""
self.assertEqual(jc.parsers.time.parse(self.osx_10_14_6_time_p, quiet=True), self.osx_10_14_6_time_p_json)
def test_time_lp_osx_10_14_6(self):
"""
Test 'time -lp' on OSX 10.14.6
"""
self.assertEqual(jc.parsers.time.parse(self.osx_10_14_6_time_lp, quiet=True), self.osx_10_14_6_time_lp_json)
def test_time_no_centiseconds(self):
"""
Test 'time' output with no centiseconds data
"""
data = ''' Command being timed: "echo"
User time (seconds): 5156.20
System time (seconds): 0.05
Percent of CPU this job got: 99%
Elapsed (wall clock) time (h:mm:ss or m:ss): 1:25:56
Average shared text size (kbytes): 0
Average unshared data size (kbytes): 0
Average stack size (kbytes): 0
Average total size (kbytes): 0
Maximum resident set size (kbytes): 21760
Average resident set size (kbytes): 0
Major (requiring I/O) page faults: 0
Minor (reclaiming a frame) page faults: 4975
Voluntary context switches: 1
Involuntary context switches: 8159
Swaps: 0
File system inputs: 0
File system outputs: 6272
Socket messages sent: 0
Socket messages received: 0
Signals delivered: 0
Page size (bytes): 4096
Exit status: 0'''
expected = {"command_being_timed":"echo","user_time":5156.2,"system_time":0.05,"cpu_percent":99,"elapsed_time":"1:25:56","average_shared_text_size":0,"average_unshared_data_size":0,"average_stack_size":0,"average_total_size":0,"maximum_resident_set_size":21760,"average_resident_set_size":0,"major_pagefaults":0,"minor_pagefaults":4975,"voluntary_context_switches":1,"involuntary_context_switches":8159,"swaps":0,"block_input_operations":0,"block_output_operations":6272,"messages_sent":0,"messages_received":0,"signals_delivered":0,"page_size":4096,"exit_status":0,"elapsed_time_hours":1,"elapsed_time_minutes":25,"elapsed_time_seconds":56,"elapsed_time_centiseconds":0,"elapsed_time_total_seconds":5156.0}
self.assertEqual(jc.parsers.time.parse(data, quiet=True), expected)
if __name__ == '__main__':
unittest.main()
|