Skip to content

Commit fe8e51f

Browse files
Qinghao ShiQinghao Shi
Qinghao Shi
authored and
Qinghao Shi
committed
TESTS: fix timing drift test in rtos folder
1 parent 0bacd5b commit fe8e51f

File tree

1 file changed

+136
-0
lines changed

1 file changed

+136
-0
lines changed
Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
"""
2+
mbed SDK
3+
Copyright (c) 2011-2013 ARM Limited
4+
SPDX-License-Identifier: Apache-2.0
5+
6+
Licensed under the Apache License, Version 2.0 (the "License");
7+
you may not use this file except in compliance with the License.
8+
You may obtain a copy of the License at
9+
10+
http://www.apache.org/licenses/LICENSE-2.0
11+
12+
Unless required by applicable law or agreed to in writing, software
13+
distributed under the License is distributed on an "AS IS" BASIS,
14+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
See the License for the specific language governing permissions and
16+
limitations under the License.
17+
"""
18+
19+
from mbed_host_tests import BaseHostTest
20+
import time
21+
22+
23+
class TimingDriftSync(BaseHostTest):
24+
"""
25+
This works as master-slave fashion
26+
1) Device says its booted up and ready to run the test, wait for host to respond
27+
2) Host sends the message to get the device current time i.e base time
28+
29+
#
30+
# *
31+
# * |
32+
#<---* DUT<- base_time | - round_trip_base_time ------
33+
# * | |
34+
# * - |
35+
# - |
36+
# | |
37+
# | |
38+
# | - measurement_stretch | - nominal_time
39+
# | |
40+
# | |
41+
# - |
42+
# * - |
43+
# * | |
44+
#<---* DUT <-final_time | - round_trip_final_time------
45+
# * |
46+
# * -
47+
#
48+
#
49+
# As we increase the measurement_stretch, the error because of transport delay diminishes.
50+
# The values of measurement_stretch is propotional to round_trip_base_time(transport delays)
51+
# by factor time_measurement_multiplier.This multiplier is used is 80 to tolerate 2 sec of
52+
# transport delay and test time ~ 180 secs
53+
#
54+
# Failure in timing can occur if we are ticking too fast or we are ticking too slow, hence we have
55+
# min_range and max_range. if we cross on either side tests would be marked fail. The range is a function of
56+
# tolerance/acceptable drift currently its 5%.
57+
#
58+
59+
"""
60+
__result = None
61+
mega = 1000000.0
62+
max_measurement_time = 180
63+
64+
# this value is obtained for measurements when there is 0 transport delay and we want accurancy of 5%
65+
time_measurement_multiplier = 80
66+
67+
def _callback_timing_drift_check_start(self, key, value, timestamp):
68+
self.round_trip_base_start = timestamp
69+
self.send_kv("base_time", 0)
70+
71+
def _callback_base_time(self, key, value, timestamp):
72+
self.round_trip_base_end = timestamp
73+
self.device_time_base = float(value)
74+
self.round_trip_base_time = self.round_trip_base_end - self.round_trip_base_start
75+
76+
self.log("Device base time {}".format(value))
77+
measurement_stretch = (self.round_trip_base_time * self.time_measurement_multiplier) + 5
78+
79+
if measurement_stretch > self.max_measurement_time:
80+
self.log("Time required {} to determine device timer is too high due to transport delay, skipping".format(measurement_stretch))
81+
else:
82+
self.log("sleeping for {} to measure drift accurately".format(measurement_stretch))
83+
time.sleep(measurement_stretch)
84+
self.round_trip_final_start = time.time()
85+
self.send_kv("final_time", 0)
86+
87+
def _callback_final_time(self, key, value, timestamp):
88+
self.round_trip_final_end = timestamp
89+
self.device_time_final = float(value)
90+
self.round_trip_final_time = self.round_trip_final_end - self.round_trip_final_start
91+
self.log("Device final time {} ".format(value))
92+
93+
# compute the test results and send to device
94+
results = "pass" if self.compute_parameter() else "fail"
95+
self.send_kv(results, "0")
96+
97+
def setup(self):
98+
self.register_callback('timing_drift_check_start', self._callback_timing_drift_check_start)
99+
self.register_callback('base_time', self._callback_base_time)
100+
self.register_callback('final_time', self._callback_final_time)
101+
102+
def compute_parameter(self, failure_criteria=0.05):
103+
t_max = self.round_trip_final_end - self.round_trip_base_start
104+
t_min = self.round_trip_final_start - self.round_trip_base_end
105+
t_max_hi = t_max * (1 + failure_criteria)
106+
t_max_lo = t_max * (1 - failure_criteria)
107+
t_min_hi = t_min * (1 + failure_criteria)
108+
t_min_lo = t_min * (1 - failure_criteria)
109+
device_time = (self.device_time_final - self.device_time_base) / self.mega
110+
111+
self.log("Compute host events")
112+
self.log("Transport delay 0: {}".format(self.round_trip_base_time))
113+
self.log("Transport delay 1: {}".format(self.round_trip_final_time))
114+
self.log("DUT base time : {}".format(self.device_time_base))
115+
self.log("DUT end time : {}".format(self.device_time_final))
116+
117+
self.log("min_pass : {} , max_pass : {} for {}%%".format(t_max_lo, t_min_hi, failure_criteria * 100))
118+
self.log("min_inconclusive : {} , max_inconclusive : {}".format(t_min_lo, t_max_hi))
119+
self.log("Time reported by device: {}".format(device_time))
120+
121+
if t_max_lo <= device_time <= t_min_hi:
122+
self.log("Test passed !!!")
123+
self.__result = True
124+
elif t_min_lo <= device_time <= t_max_hi:
125+
self.log("Test inconclusive due to transport delay, retrying")
126+
self.__result = False
127+
else:
128+
self.log("Time outside of passing range. Timing drift seems to be present !!!")
129+
self.__result = False
130+
return self.__result
131+
132+
def result(self):
133+
return self.__result
134+
135+
def teardown(self):
136+
pass

0 commit comments

Comments
 (0)