refactor: time steps with ElapsedTimer::lap()

loop(), socket client init and /discovery each read a step's time and
restarted the timer by hand. lap() does both. Also put the request timing
comment back on timeRequest() and say why /discovery logs its steps.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
cjkas 2026-10-13 15:39:14 +02:00
parent 293e013aa2
commit 4544eb6469
6 changed files with 53 additions and 28 deletions

View file

@ -16,7 +16,9 @@ typedef uint8_t byte;
// Fixed so anything formatted from it is deterministic.
inline unsigned long millis() { return 0; }
inline unsigned long micros() { return 0; }
// Tests that measure time set this.
inline unsigned long stubMicros = 0;
inline unsigned long micros() { return stubMicros; }
inline bool getLocalTime(struct tm *info, uint32_t ms = 5000) {
(void)ms;
time_t now = time(nullptr);

View file

@ -40,6 +40,26 @@ static void test_worthLogging_from_the_threshold() {
TEST_ASSERT_TRUE(worthLogging(UINT32_MAX));
}
/*********************************************************************
* ElapsedTimer
********************************************************************/
static void test_elapsedTimer_lap_times_each_step() {
stubMicros = 1000;
ElapsedTimer timer;
stubMicros = 1250;
TEST_ASSERT_EQUAL_UINT32(250, timer.lap());
stubMicros = 1300;
TEST_ASSERT_EQUAL_UINT32(50, timer.lap());
TEST_ASSERT_EQUAL_UINT32(0, timer.us());
}
static void test_elapsedTimer_lap_across_micros_wrap() {
stubMicros = 0xFFFFFF00UL;
ElapsedTimer timer;
stubMicros = 0x100;
TEST_ASSERT_EQUAL_UINT32(0x200, timer.lap());
stubMicros = 0;
}
int main(int argc, char **argv) {
(void)argc;
(void)argv;
@ -51,5 +71,7 @@ int main(int argc, char **argv) {
RUN_TEST(test_logLock_no_wait_allowed);
RUN_TEST(test_worthLogging_fast_timings_are_skipped);
RUN_TEST(test_worthLogging_from_the_threshold);
RUN_TEST(test_elapsedTimer_lap_times_each_step);
RUN_TEST(test_elapsedTimer_lap_across_micros_wrap);
return UNITY_END();
}