diff --git a/README.md b/README.md index 53114e78e..97bc2a4ef 100644 --- a/README.md +++ b/README.md @@ -1820,6 +1820,9 @@ typedef struct ncstats { // Acquire a snapshot of the notcurses object's stats. void notcurses_stats(const struct notcurses* nc, ncstats* stats); + +// Reset all stats. +void notcurses_reset_stats(struct notcurses* nc); ``` Timings for renderings are across the breadth of `notcurses_render()`: they diff --git a/include/notcurses.h b/include/notcurses.h index 6be3392ac..ee85e5496 100644 --- a/include/notcurses.h +++ b/include/notcurses.h @@ -304,6 +304,9 @@ typedef struct ncstats { // Acquire a snapshot of the notcurses object's stats. API void notcurses_stats(const struct notcurses* nc, ncstats* stats); +// Reset all stats. +API void notcurses_reset_stats(struct notcurses* nc); + // Resize the specified ncplane. The four parameters 'keepy', 'keepx', // 'keepleny', and 'keeplenx' define a subset of the ncplane to keep, // unchanged. This may be a section of size 0, though none of these four diff --git a/src/lib/notcurses.c b/src/lib/notcurses.c index 7678a9c64..b5fe48f23 100644 --- a/src/lib/notcurses.c +++ b/src/lib/notcurses.c @@ -653,6 +653,12 @@ make_nonblocking(FILE* fp){ return fcntl(fd, F_SETFL, flags | O_NONBLOCK); } +void notcurses_reset_stats(notcurses* nc){ + memset(&nc->stats, 0, sizeof(nc->stats)); + nc->stats.render_min_ns = 1ul << 62u; + nc->stats.render_min_bytes = 1ul << 62u; +} + notcurses* notcurses_init(const notcurses_options* opts, FILE* outfp){ const char* encoding = nl_langinfo(CODESET); if(encoding == NULL || strcmp(encoding, "UTF-8")){ @@ -669,9 +675,7 @@ notcurses* notcurses_init(const notcurses_options* opts, FILE* outfp){ free(ret); return NULL; } - memset(&ret->stats, 0, sizeof(ret->stats)); - ret->stats.render_min_ns = 1ul << 62u; - ret->stats.render_min_bytes = 1ul << 62u; + notcurses_reset_stats(ret); ret->ttyfp = outfp; ret->renderfp = opts->renderfp; ret->inputescapes = NULL; diff --git a/tests/notcurses.cpp b/tests/notcurses.cpp index d98c059c4..33947de12 100644 --- a/tests/notcurses.cpp +++ b/tests/notcurses.cpp @@ -138,3 +138,15 @@ TEST_F(NotcursesTest, ChannelSetBGAlpha){ EXPECT_TRUE(channels_fg_default_p(channel)); EXPECT_TRUE(channels_bg_default_p(channel)); } + +TEST_F(NotcursesTest, Stats){ + struct ncstats stats; + notcurses_stats(nc_, &stats); + EXPECT_EQ(0, stats.renders); + EXPECT_EQ(0, notcurses_render(nc_)); + notcurses_stats(nc_, &stats); + EXPECT_EQ(1, stats.renders); + notcurses_reset_stats(nc_); + notcurses_stats(nc_, &stats); + EXPECT_EQ(0, stats.renders); +}