Skip to content

Commit

Permalink
Replace time.clock() with time.process_time()
Browse files Browse the repository at this point in the history
  • Loading branch information
fabaff committed Jan 7, 2021
1 parent bc4c2bf commit d755745
Show file tree
Hide file tree
Showing 10 changed files with 35 additions and 35 deletions.
6 changes: 3 additions & 3 deletions cantoolz/modules/fuzz.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ def fuzz(self, fuzz_list, idf, data, bytes_to_fuzz, level, iso_mode):

def do_start(self, args):
self._i = 0
self.last = time.clock()
self.last = time.process_time()
self.queue_messages = []
iso_mode = 1 if args.get('mode') in ['ISO', 'iso', 'ISOTP', 'isotp'] else 0
if 'id' in args:
Expand Down Expand Up @@ -100,8 +100,8 @@ def do_effect(self, can_msg, args):
elif not can_msg.CANData:
d_time = float(args.get('delay', 0))
if d_time > 0:
if time.clock() - self.last >= d_time:
self.last = time.clock()
if time.process_time() - self.last >= d_time:
self.last = time.process_time()
can_msg.CANFrame = self.queue_messages.pop()
can_msg.CANData = True
can_msg.bus = self._bus
Expand Down
12 changes: 6 additions & 6 deletions cantoolz/modules/io/hw_USBtin.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ def do_start(self, params): # enable reading
self.dev_write("l")
self._run = True
self.wait_for = False
self.last = time.clock()
self.last = time.process_time()
time.sleep(1)

def init_port(self):
Expand Down Expand Up @@ -203,7 +203,7 @@ def do_init(self, params): # Get device and open serial port
self._usbtin_loop = bool(params.get('usbtin_loop', False))
self._restart = bool(params.get('auto_activate', False))
self.act_time = float(params.get('auto_activate', 5.0))
self.last = time.clock()
self.last = time.process_time()
self.wait_for = False
self._run = True
self.do_stop({})
Expand Down Expand Up @@ -249,9 +249,9 @@ def do_effect(self, can_msg, args): # read full packet from serial port
can_msg = self.do_read(can_msg)
elif args.get('action') == 'write':
# KOSTYL: workaround for BMW f10 bus
# if self._restart and self._run and (time.clock() - self.last) >= self.act_time:
# if self._restart and self._run and (time.process_time() - self.last) >= self.act_time:
# self.dev_write("O")
# self.last = time.clock()
# self.last = time.process_time()
self.do_write(can_msg)
else:
self.dprint(1, 'Command ' + args.get('action', 'NONE') + ' not implemented 8(')
Expand All @@ -271,10 +271,10 @@ def do_effect(self, can_msg, args): # read full packet from serial port
can_msg.debugText['please_send'] = True
self.wait_for = False
elif time.clock() - self.last >= self.act_time and not self.wait_for:
elif time.process_time() - self.last >= self.act_time and not self.wait_for:
self.dev_write("F")
self.wait_for = True
self.last = time.clock()
self.last = time.process_time()
"""

return can_msg
Expand Down
6 changes: 3 additions & 3 deletions cantoolz/modules/ping.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ def _get_range(data):

def do_start(self, args):
self.queue_messages = []
self.last = time.clock()
self.last = time.process_time()

data = [0, 0, 0, 0, 0, 0, 0, 0]
if 'body' in args:
Expand Down Expand Up @@ -127,8 +127,8 @@ def do_effect(self, can_msg, args):
d_time = float(args.get('delay', 0))
if not can_msg.CANData:
if d_time > 0:
if time.clock() - self.last >= d_time:
self.last = time.clock()
if time.process_time() - self.last >= d_time:
self.last = time.process_time()
can_msg.CANFrame = self.do_ping(args)

else:
Expand Down
2 changes: 1 addition & 1 deletion cantoolz/modules/replay.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def cmd_load(self, name):

def do_init(self, params):
self.CANList = Replay()
self.last = time.clock()
self.last = time.process_time()
self._last = 0
self._full = 1
self._num1 = 0
Expand Down
6 changes: 3 additions & 3 deletions cantoolz/modules/vircar/anti_theft_2.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def do_init(self, params):
cmds = self._status2['stop_engine'].split(':')
self.can_stop = CANMessage(int(cmds[0], 16) if cmds[0][0:2] == '0x' else int(cmds[0]), int(cmds[1]), bytes.fromhex(cmds[2]), False, CANMessage.DataFrame)
self._try = 0
self.last = time.clock()
self.last = time.process_time()

def do_stop(self, params):
self._current = 1 # 1 - blocked, 2 - ALERT, 3 - authenticated
Expand All @@ -45,12 +45,12 @@ def do_stop(self, params):
# Effect (could be fuzz operation, sniff, filter or whatever)
def do_effect(self, can_msg, args):

if self.stop and not can_msg.CANData and time.clock() - self.last > 1.5 and args.get('action', '') == 'write' and self._current != 3:
if self.stop and not can_msg.CANData and time.process_time() - self.last > 1.5 and args.get('action', '') == 'write' and self._current != 3:
can_msg.CANData = True
can_msg.CANFrame = self.can_stop
if self._current != 2:
self.stop = False
self.last = time.clock()
self.last = time.process_time()

# FILTER
elif can_msg.CANData and self._current != 3 and args.get('action', '') == 'read':
Expand Down
6 changes: 3 additions & 3 deletions cantoolz/modules/vircar/ecu_door.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def do_init(self, params):
self._params = params
self._params['status'] = 'Locked'
self._frames = []
self._last_sent = time.clock()
self._last_sent = time.process_time()

def do_start(self, params):
self._params['status'] = 'Locked'
Expand All @@ -57,7 +57,7 @@ def do_effect(self, can_msg, args):
self._last_sent -= self._params.get('reports_delay', 1.0)

elif args['action'] == 'write' and not can_msg.CANData and self._params['status'] != 'Turned off': # Write
if (time.clock() - self._last_sent) >= self._params.get('reports_delay', 1.0):
if (time.process_time() - self._last_sent) >= self._params.get('reports_delay', 1.0):
can_msg.CANFrame = CANMessage(
self._params.get('id_report', 0xffff),
len(self._params.get('reports', {}).get(self._params['status'], '')) / 2,
Expand All @@ -66,6 +66,6 @@ def do_effect(self, can_msg, args):
)
can_msg.CANData = True
can_msg.bus = self._bus
self._last_sent = time.clock()
self._last_sent = time.process_time()

return can_msg
10 changes: 5 additions & 5 deletions cantoolz/modules/vircar/ecu_engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ def do_init(self, params):
self.frames = []
self.init_sess = None
self.init_sess2 = None
self._last_sent = time.clock()
self.vin_gen = [time.clock(), time.clock(), 0.7, 2.9]
self._last_sent = time.process_time()
self.vin_gen = [time.process_time(), time.process_time(), 0.7, 2.9]

left = len(self._vin)
count = int(left / 7) + 1
Expand Down Expand Up @@ -93,17 +93,17 @@ def generate_rpm(self):
self.rpm_down = 0

if self._status2['status'] in [2, 3, 1]:
if (time.clock() - self._last_sent) >= self._status2.get('reports_delay', 0.5):
if (time.process_time() - self._last_sent) >= self._status2.get('reports_delay', 0.5):
self.frames.append(
CANMessage(
self._status2.get('id_report', 0xffff),
3,
self._status2['status'].to_bytes(1, byteorder='big') + self._status2['rpm'].to_bytes(2, byteorder='big'), False,
CANMessage.DataFrame))
self._last_sent = time.clock()
self._last_sent = time.process_time()

def generate_vin(self):
curr_t = time.clock()
curr_t = time.process_time()
if self.vin_gen[0] == 0 or curr_t - self.vin_gen[0] > self.vin_gen[2]:
if self.vin_gen[1] == 0 or curr_t - self.vin_gen[1] > self.vin_gen[3]:
self.frames.append(CANMessage.init_data(self._status2.get('vin_id', 1), len(self.vin[self.count_part]), self.vin[self.count_part]))
Expand Down
6 changes: 3 additions & 3 deletions cantoolz/modules/vircar/ecu_light.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def do_init(self, params):
self._params = params
self._params['status'] = 'Lights OFF'
self._frames = []
self._last_sent = time.clock()
self._last_sent = time.process_time()

def do_start(self, params):
self._params['status'] = 'Lights OFF'
Expand All @@ -61,14 +61,14 @@ def do_effect(self, can_msg, args):
self._last_sent -= self._params.get('reports_delay', 1.0)

elif args['action'] == 'write' and not can_msg.CANData and self._params['status'] != 'Turned off': # Write
if (time.clock() - self._last_sent) >= self._params.get('reports_delay', 1.0):
if (time.process_time() - self._last_sent) >= self._params.get('reports_delay', 1.0):
can_msg.CANFrame = CANMessage(
self._params.get('id_report', 0xffff),
len(self._params.get('reports', {}).get(self._params['status'], '')) / 2,
bytes.fromhex(self._params.get('reports', {}).get(self._params['status'], 'ffffff')), False,
CANMessage.DataFrame)
can_msg.CANData = True
can_msg.bus = self._bus
self._last_sent = time.clock()
self._last_sent = time.process_time()

return can_msg
8 changes: 4 additions & 4 deletions cantoolz/replay.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@ class Replay:

def __init__(self):
self._stream = []
self._last = time.clock()
self._last = time.process_time()
self._curr = 0
self._pre_last = 0
self._shift = 0
self._size = 0
self.add_timestamp()

def reset(self):
self._last = time.clock()
self._last = time.process_time()
self._curr = 0
self._pre_last = 0

Expand All @@ -25,10 +25,10 @@ def stream(self):
return copy.deepcopy(self._stream)

def passed_time(self):
return time.clock() + self._shift - self._last
return time.process_time() + self._shift - self._last

def restart_time(self, shift=.0):
self._last = time.clock() - shift
self._last = time.process_time() - shift

def append_time(self, times, can_msg):
if can_msg.CANData:
Expand Down
8 changes: 4 additions & 4 deletions tests/modules/test_replay.py
Original file line number Diff line number Diff line change
Expand Up @@ -202,15 +202,15 @@ def test_replay_timestamp(self):
# Get number of loaded CAN packets from replay.
num = self.CANEngine.call_module(0, 'p')
self.assertTrue(0 <= num.find("Loaded packets: 11"), "Should be 11 packets")
time1 = time.clock()
time1 = time.process_time()
# Replay loaded CAN packets with replay.
self.CANEngine.call_module(0, 'r')
while self.CANEngine.actions[0][1]._status < 100.0:
continue
# Print table of CAN messages sniffed by analyze.
ret = self.CANEngine.call_module(1, 'p')
print(ret)
time2 = time.clock()
time2 = time.process_time()
print("TIME: " + str(time2 - time1))
# Get current status of analyze
st = self.CANEngine.call_module(1, 'S')
Expand All @@ -227,15 +227,15 @@ def test_replay_timestamp2(self):
# Get number of loaded CAN packets from replay.
num = self.CANEngine.call_module(0, 'p')
self.assertTrue(0 <= num.find("Loaded packets: 11"), "Should be 11 packets")
time1 = time.clock()
time1 = time.process_time()
# Replay loaded CAN packets with replay.
self.CANEngine.call_module(0, 'r')
while self.CANEngine.actions[0][1]._status < 100.0:
continue
# Print table of CAN messages sniffed by analyze.
ret = self.CANEngine.call_module(1, 'p')
print(ret)
time2 = time.clock()
time2 = time.process_time()
print("TIME: " + str(time2 - time1))
self.assertTrue(13.99 < time2 - time1 < 15.99, "Should be around 14 seconds")
# Get current status of analyze
Expand Down

0 comments on commit d755745

Please sign in to comment.