Testing the endurance of an USB flash drive

Since I started using USB sticks for databases, I wondered how reliable they are and how can I test the endurance of a specific model. I thought that I should use a script that writes data on it untill it breakes, but since I don’t have coding knowledge, I asked the omnipresent AI. My first expirience with Copilot (MS Edge) gave me this results. Can you review it and maybe tune it a little bit? Maybe add a pause after each run of 5 min, just to let it cool down? Thanks!
It’s pretty damn scarry to talk to a machine. :cold_face:
I don’t want to advertise MS’s AI, it’s just what I have at hand. I only want to share my expirience and inspire newbies like me.

Here’s the script:

#!/bin/bash

# Specify the path to your USB flash drive (change accordingly)
DEVICE="/dev/sdb"

# Function to check for errors
check_errors() {
    if [ $? -ne 0 ]; then
        echo "Error occurred. Please check the USB flash drive."
        exit 1
    fi
}

# Fill the disk with random data and calculate SHA-1 sum
while true; do
    dd if=/dev/urandom of="$DEVICE" bs=1M
    check_errors
    sha1sum "$DEVICE"
    check_errors
done

A flaw in your script is that sha1sum’ing the device would only stress that there are no hard disk errors, but it could still return a bad hash that you have not checked it for errors.

Luckily there is some software that does what you are wanting to do: stress/torture the disk until error: GitHub - ncw/stressdisk: Stress test your disks / memory cards / USB sticks before trusting your valuable data to them

However I’d caution being able to draw any meaningful results from this kind of test. For flash drives especially vendors can source from a variety of flash vendors of differing quality. You’ll need a very large sample of drives to even arrive at a guess to a particular drive’s reliability. I have a bunch of Kingston 100 G3 USB drives with wildly different IO speeds.

Disks in general follow the bathtub curve for failure, so if you run this continuous stress test for about an extended duration and there are no errors, chances are it will last quite a long time.

1 Like

I’m interested more in the longevity of the drive as TBW, but in a stress test I believe the controller will break first, by high temps.
I just realised, I need to add a counter for all the writes.

I recall seeing few years ago some youtuber tests showing that an average consumer USB flash drive survives 15-30 full drive writes. Can’t find it now, sorry, but at this point I assume that unless it is stated explicitly, a cheap flash drive survives about that much.

I suspect high-speed drives (like, >100MB/s reads and writes) are more durable though, higher speeds generally require a better design and more quality components.

As for the code itself: don’t use /dev/urandom or sha1sum, it’s going to be a bottleneck. It’s simpler to just use the badblocks command, it’s designed for this purpose.

I will search for some program that can do what I have in mind, but if I can’t find any, I can do it manualy too. I’ll just copy big movie files.