Skip to content

sdcardio: new example #1193

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 29, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 60 additions & 0 deletions CircuitPython_sdcardio_sdioio/benchmark.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import time
import os

import mount_sd

# First, just write the file 'hello.txt' to the card
with open("/sd/hello.txt", "w") as f: print("hello world", file=f)

print()
print("SD card I/O benchmarks")

# Test read and write speed in several scenarios:
# * 512 or 4096 bytes at a time
# * Writing 1 time or 16 times
# First write the content to the SD card, then read it back, reporting the
# time taken.
for sz in 512, 4096:
b = bytearray(sz)
for i in range(sz): b[i] = 0xaa
for n in (1, 16):
with open("/sd/hello.bin", "wb") as f:
t0 = time.monotonic_ns()
for i in range(n):
f.write(b)
t1 = time.monotonic_ns()

dt = (t1-t0) / 1e9
print(f"write {len(b)} x {n} in {dt}s {n * len(b) / dt / 1000:.1f}Kb/s")

with open("/sd/hello.bin", "rb") as f:
t0 = time.monotonic_ns()
for i in range(n):
f.readinto(b)
t1 = time.monotonic_ns()

dt = (t1-t0) / 1e9

print(f"read {len(b)} x {n} in {dt}s {n * len(b) / dt / 1000:.1f}Kb/s")
print()

# Test "logging" speed and report the time to write each line.
# Note that in this test the file is not closed or flushed after each
# line, so in the event of power loss some lines would be lost. However,
# it allows much more frequent logging overall.
#
# If keeping data integrity is your highest concern, follow the logging
# example, not this logging benchmark!
print("logging test")
with open("/sd/log.txt", "wt") as logfile:
t0 = time.monotonic_ns()
for i in range(10000):
t1 = time.monotonic_ns()
dt = (t1-t0) / 1e9
print(f"Line {i}, {dt:2f}s elapsed", file=logfile)
t1 = time.monotonic_ns()
dt = (t1-t0) / 1e9

print(f"Logged 10000 lines in {dt} seconds, {dt*100:.0f}us/line")
sz = os.stat('/sd/log.txt')[6]
print(f"{sz} bytes written, {sz/dt/1000:.1f}Kb/s")