ArduinoStreamUtils icon indicating copy to clipboard operation
ArduinoStreamUtils copied to clipboard

Feature request: Calculate statistics on a (wrapped) stream

Open vshymanskyy opened this issue 2 years ago • 6 comments

Could be interesting to be able to grab some stats of the stream usage:

  • Total TX/RX bytes count
  • Total TX/RX packets count (writes)
  • Min/Max/Average read/write size

The only difficulty I see here: Some functions (i.e. readBytes) could be implemented via read() on some streams, but it can also be implemented directly by other implementations. Not sure if there's a clean way to handle it.

vshymanskyy avatar Jun 22 '22 11:06 vshymanskyy

Hi @vshymanskyy,

Thank you for this suggestion; I think this is a good idea.

We could name the new classes:

  • CountingPrint
  • CountingStream
  • WriteCountingStream
  • ReadCountingStream
  • etc.

The members could be:

  • readBytes()
  • readCount()
  • minReadSize()
  • maxReadSize()
  • avgReadSize()
  • writtenBytes()
  • writeCount()
  • minWriteSize()
  • maxWriteSize()
  • avgWriteSize()

How does that sound?

Best regards, Benoit

bblanchon avatar Jun 23 '22 15:06 bblanchon

Sounds cool. But some names may be ambiguous esp. readBytes(). Maybe prefix all of them with total, min, max, avg ?

vshymanskyy avatar Jun 24 '22 23:06 vshymanskyy

You're right! Maybe something like this, then:

read write
readCount() writeCount()
avgReadSize() avgWriteSize()
maxReadSize() maxWriteSize()
minReadSize() minWriteSize()
totalReadSize() totalWriteSize()

bblanchon avatar Jun 26 '22 08:06 bblanchon

I'm not sure how to handle error cases: should we count them, or ignore them?

For example, if upstream's write() function returns 0, should I increment writeCount? should I set minWriteSize to 0?

bblanchon avatar Jun 27 '22 15:06 bblanchon

Maybe readErrors() and writeErrors()?

dpharris avatar Oct 23 '22 23:10 dpharris

for such jobs I like the header-only stats classes by https://www.johndcook.com/blog/skewness_kurtosis/

he has several such classes on his website, real collectors items - I collect them here plus my adaptations: https://github.com/mhaberler/Stats

mhaberler avatar May 14 '23 20:05 mhaberler