Skip to content

Commit 2a2cf25

Browse files
authored
Merge pull request #2588 from bridadan/timing-tests-drift-refactor
Timing tests drift refactor
2 parents 5dcd546 + 1473240 commit 2a2cf25

File tree

7 files changed

+205
-103
lines changed

7 files changed

+205
-103
lines changed

TESTS/host_tests/timing_drift_auto.py

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
"""
2+
mbed SDK
3+
Copyright (c) 2011-2013 ARM Limited
4+
5+
Licensed under the Apache License, Version 2.0 (the "License");
6+
you may not use this file except in compliance with the License.
7+
You may obtain a copy of the License at
8+
9+
http://www.apache.org/licenses/LICENSE-2.0
10+
11+
Unless required by applicable law or agreed to in writing, software
12+
distributed under the License is distributed on an "AS IS" BASIS,
13+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
See the License for the specific language governing permissions and
15+
limitations under the License.
16+
"""
17+
18+
from mbed_host_tests import BaseHostTest
19+
20+
21+
class TimingDriftTest(BaseHostTest):
22+
""" This test is reading single characters from stdio
23+
and measures time between their occurrences.
24+
"""
25+
__result = None
26+
27+
# This is calculated later: average_drift_max * number of tick events
28+
total_drift_max = None
29+
30+
average_drift_max = 0.05
31+
ticks = []
32+
start_time = None
33+
finish_time = None
34+
dut_seconds_passed = None
35+
total_time = None
36+
total_drift = None
37+
average_drift = None
38+
39+
def _callback_result(self, key, value, timestamp):
40+
# We should not see result data in this test
41+
self.__result = False
42+
43+
def _callback_end(self, key, value, timestamp):
44+
""" {{end;%s}}} """
45+
self.log("Received end event, timestamp: %f" % timestamp)
46+
self.notify_complete(result=self.result(print_stats=True))
47+
48+
49+
def _callback_tick(self, key, value, timestamp):
50+
""" {{tick;%d}}} """
51+
self.log("tick! %f" % timestamp)
52+
self.ticks.append((key, value, timestamp))
53+
54+
55+
def setup(self):
56+
self.register_callback("end", self._callback_end)
57+
self.register_callback('tick', self._callback_tick)
58+
59+
60+
def result(self, print_stats=True):
61+
self.dut_seconds_passed = len(self.ticks) - 1
62+
63+
if self.dut_seconds_passed < 1:
64+
if print_stats:
65+
self.log("FAIL: failed to receive at least two tick events")
66+
self.__result = False
67+
return self.__result
68+
69+
self.total_drift_max = self.dut_seconds_passed * self.average_drift_max
70+
71+
self.start_time = self.ticks[0][2]
72+
self.finish_time = self.ticks[-1][2]
73+
self.total_time = self.finish_time - self.start_time
74+
self.total_drift = self.total_time - self.dut_seconds_passed
75+
self.average_drift = self.total_drift / self.dut_seconds_passed
76+
77+
if print_stats:
78+
self.log("Start: %f" % self.start_time)
79+
self.log("Finish: %f" % self.finish_time)
80+
self.log("Total time taken: %f" % self.total_time)
81+
82+
total_drift_ratio_string = "Total drift/Max total drift: %f/%f"
83+
self.log(total_drift_ratio_string % (self.total_drift,
84+
self.total_drift_max))
85+
86+
average_drift_ratio_string = "Average drift/Max average drift: %f/%f"
87+
self.log(average_drift_ratio_string % (self.average_drift,
88+
self.average_drift_max))
89+
90+
91+
if abs(self.total_drift) > self.total_drift_max:
92+
if print_stats:
93+
self.log("FAIL: Total drift exceeded max total drift")
94+
self.__result = False
95+
elif self.average_drift > self.average_drift_max:
96+
if print_stats:
97+
self.log("FAIL: Average drift exceeded max average drift")
98+
self.__result = False
99+
else:
100+
self.__result = True
101+
102+
return self.__result
103+
104+
105+
def teardown(self):
106+
pass

TESTS/integration/threaded_blinky/main.cpp

Lines changed: 0 additions & 29 deletions
This file was deleted.

TESTS/mbed_drivers/ticker/main.cpp

Lines changed: 29 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -21,32 +21,31 @@
2121
using namespace utest::v1;
2222

2323
static const int ONE_SECOND_MS = 1000;
24+
static const int total_ticks = 10;
2425

2526
DigitalOut led1(LED1);
2627
DigitalOut led2(LED2);
2728

2829
Ticker *ticker1;
2930
Ticker *ticker2;
3031

32+
volatile int ticker_count = 0;
33+
volatile bool print_tick = false;
34+
3135
void send_kv_tick() {
32-
static int count = 0;
33-
if (count < 10) {
34-
greentea_send_kv("tick", count);
35-
} else if (count == 10) {
36-
count = 0;
37-
Harness::validate_callback();
36+
if (ticker_count <= total_ticks) {
37+
print_tick = true;
3838
}
39-
count++;
4039
}
4140

4241
void ticker_callback_0(void) {
43-
static int ticker_count = 0;
44-
if (ticker_count >= ONE_SECOND_MS) {
42+
static int fast_ticker_count = 0;
43+
if (fast_ticker_count >= ONE_SECOND_MS) {
4544
send_kv_tick();
46-
ticker_count = 0;
45+
fast_ticker_count = 0;
4746
led1 = !led1;
4847
}
49-
ticker_count++;
48+
fast_ticker_count++;
5049
}
5150

5251
void ticker_callback_1(void) {
@@ -78,26 +77,38 @@ void ticker_callback_2_switch_to_1(void) {
7877
ticker_callback_2();
7978
}
8079

81-
utest::v1::control_t test_case_1x_ticker() {
80+
void wait_and_print() {
81+
while(ticker_count <= total_ticks) {
82+
if (print_tick) {
83+
print_tick = false;
84+
greentea_send_kv("tick", ticker_count++);
85+
}
86+
}
87+
}
88+
89+
void test_case_1x_ticker() {
8290
led1 = 0;
8391
led2 = 0;
92+
ticker_count = 0;
8493
ticker1->attach_us(ticker_callback_0, ONE_SECOND_MS);
85-
return CaseTimeout(15 * ONE_SECOND_MS);
94+
wait_and_print();
8695
}
8796

88-
control_t test_case_2x_ticker() {
97+
void test_case_2x_ticker() {
8998
led1 = 0;
9099
led2 = 0;
100+
ticker_count = 0;
91101
ticker1->attach(&ticker_callback_1, 1.0);
92102
ticker2->attach(&ticker_callback_2_led, 2.0);
93-
return CaseTimeout(15 * ONE_SECOND_MS);
103+
wait_and_print();
94104
}
95105

96-
utest::v1::control_t test_case_2x_callbacks() {
106+
void test_case_2x_callbacks() {
97107
led1 = 0;
98108
led2 = 0;
109+
ticker_count = 0;
99110
ticker1->attach(ticker_callback_1_switch_to_2, 1.0);
100-
return CaseTimeout(15 * ONE_SECOND_MS);
111+
wait_and_print();
101112
}
102113

103114
utest::v1::status_t one_ticker_case_setup_handler_t(const Case *const source, const size_t index_of_case) {
@@ -130,7 +141,7 @@ Case cases[] = {
130141
};
131142

132143
utest::v1::status_t greentea_test_setup(const size_t number_of_cases) {
133-
GREENTEA_SETUP(60, "wait_us_auto");
144+
GREENTEA_SETUP((total_ticks + 5) * 3, "timing_drift_auto");
134145
return greentea_test_setup_handler(number_of_cases);
135146
}
136147

TESTS/mbed_drivers/timeout/main.cpp

Lines changed: 19 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -20,43 +20,33 @@
2020

2121
using namespace utest::v1;
2222

23-
Timeout timer;
23+
Timeout timeout;
2424
DigitalOut led(LED1);
25-
26-
namespace {
27-
const int MS_INTERVALS = 1000;
28-
}
25+
volatile int ticker_count = 0;
26+
volatile bool print_tick = false;
27+
static const int total_ticks = 10;
28+
const int ONE_SECOND_US = 1000000;
2929

3030
void send_kv_tick() {
31-
static int count = 0;
32-
if (count < 10) {
33-
greentea_send_kv("tick", count);
34-
} else if (count == 10) {
35-
Harness::validate_callback();
31+
if (ticker_count <= total_ticks) {
32+
timeout.attach_us(send_kv_tick, ONE_SECOND_US);
33+
print_tick = true;
3634
}
37-
count++;
3835
}
3936

40-
void toggleOff(void);
41-
42-
void toggleOn(void) {
43-
static int toggle_counter = 0;
44-
if (toggle_counter == MS_INTERVALS) {
45-
led = !led;
46-
send_kv_tick();
47-
toggle_counter = 0;
37+
void wait_and_print() {
38+
while(ticker_count <= total_ticks) {
39+
if (print_tick) {
40+
print_tick = false;
41+
greentea_send_kv("tick", ticker_count++);
42+
led = !led;
43+
}
4844
}
49-
toggle_counter++;
50-
timer.attach_us(toggleOff, 500);
51-
}
52-
53-
void toggleOff(void) {
54-
timer.attach_us(toggleOn, 500);
5545
}
5646

57-
control_t test_case_ticker() {
58-
toggleOn();
59-
return CaseTimeout(15 * 1000);
47+
void test_case_ticker() {
48+
timeout.attach_us(send_kv_tick, ONE_SECOND_US);
49+
wait_and_print();
6050
}
6151

6252
// Test cases
@@ -65,7 +55,7 @@ Case cases[] = {
6555
};
6656

6757
utest::v1::status_t greentea_test_setup(const size_t number_of_cases) {
68-
GREENTEA_SETUP(20, "wait_us_auto");
58+
GREENTEA_SETUP(total_ticks + 5, "timing_drift_auto");
6959
return greentea_test_setup_handler(number_of_cases);
7060
}
7161

TESTS/mbed_drivers/wait_us/main.cpp

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,23 @@
1818
#include "greentea-client/test_env.h"
1919
#include "utest/utest.h"
2020

21+
/**
22+
NOTE: This test will have a bit of inherent drift due to it being
23+
single-threaded, so having a drift that is non-zero should be ok. However,
24+
it should still be well under the limit.
25+
**/
26+
27+
2128
using namespace utest::v1;
2229

2330
DigitalOut led(LED1);
31+
volatile bool print_tick = false;
32+
const int ONE_SECOND_US = 1000000;
33+
const int total_ticks = 10;
2434

25-
void test_case_ticker() {
26-
for (int i=0; i < 10; ++i) {
27-
// 10 secs...
28-
for (int j = 0; j < 1000; ++j) {
29-
// 1000 * 1000us = 1 sec
30-
wait_us(1000);
31-
}
32-
led = !led; // Blink
35+
void test_case_ticker() {
36+
for (int i = 0; i <= total_ticks; i++) {
37+
wait_us(ONE_SECOND_US);
3338
greentea_send_kv("tick", i);
3439
}
3540
}
@@ -40,7 +45,7 @@ Case cases[] = {
4045
};
4146

4247
utest::v1::status_t greentea_test_setup(const size_t number_of_cases) {
43-
GREENTEA_SETUP(20, "wait_us_auto");
48+
GREENTEA_SETUP(total_ticks + 5, "timing_drift_auto");
4449
return greentea_test_setup_handler(number_of_cases);
4550
}
4651

TESTS/mbedmicro-rtos-mbed/basic/main.cpp

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -29,25 +29,31 @@
2929
#define STACK_SIZE DEFAULT_STACK_SIZE
3030
#endif
3131

32+
#define SIGNAL_PRINT_TICK 0x01
33+
3234
DigitalOut led1(LED1);
33-
DigitalOut led2(LED2);
3435

35-
void led2_thread(void const *argument) {
36-
static int count = 0;
37-
while (true) {
38-
led2 = !led2;
39-
Thread::wait(1000);
40-
greentea_send_kv("tick", count++);
36+
const int total_ticks = 10;
37+
38+
void print_tick_thread() {
39+
for (int i = 0; i <= total_ticks; i++) {
40+
Thread::signal_wait(SIGNAL_PRINT_TICK);
41+
greentea_send_kv("tick", i);
42+
led1 = !led1;
4143
}
4244
}
4345

4446
int main() {
45-
GREENTEA_SETUP(15, "wait_us_auto");
46-
47-
Thread thread(led2_thread, NULL, osPriorityNormal, STACK_SIZE);
48-
49-
while (true) {
50-
led1 = !led1;
51-
Thread::wait(500);
47+
GREENTEA_SETUP(total_ticks + 5, "timing_drift_auto");
48+
49+
Thread tick_thread(osPriorityNormal, STACK_SIZE);
50+
tick_thread.start(print_tick_thread);
51+
52+
for (int i = 0; i <= total_ticks; i++) {
53+
Thread::wait(1000);
54+
tick_thread.signal_set(SIGNAL_PRINT_TICK);
5255
}
56+
57+
tick_thread.join();
58+
GREENTEA_TESTSUITE_RESULT(1);
5359
}

0 commit comments

Comments
 (0)