mbox series

[v2,0/3] use shuf to choose a random file

Message ID cover.1692599767.git.naohiro.aota@wdc.com (mailing list archive)
Headers show
Series use shuf to choose a random file | expand

Message

Naohiro Aota Aug. 21, 2023, 6:37 a.m. UTC
Currently, we use "ls ... | sort -R | head -n1" (or tail) to choose a
random file in a directory.It sorts the files with "ls", sort it randomly
and pick the first line, which wastes the "ls" sort.

Also, using "sort -R | head -n1" is inefficient. Furthermore, even without
"head" or "tail", "shuf" is faster than "sort -R".

This series introduces a new helper _random_file() to choose a file in a
directory randomly. Also, replace "sort -R" with _random_file() or "shuf".

Naohiro Aota (3):
  common/rc: introduce _random_file() helper
  fstests/btrfs: use _random_file() helper
  btrfs/004: use shuf to shuffle the file lines

 common/rc       |  7 +++++++
 tests/btrfs/004 |  2 +-
 tests/btrfs/179 |  4 ++--
 tests/btrfs/192 | 14 ++++----------
 4 files changed, 14 insertions(+), 13 deletions(-)

Comments

Naohiro Aota Aug. 21, 2023, 6:52 a.m. UTC | #1
On Mon, Aug 21, 2023 at 03:37:01PM +0900, Naohiro Aota wrote:
> Currently, we use "ls ... | sort -R | head -n1" (or tail) to choose a
> random file in a directory.It sorts the files with "ls", sort it randomly
> and pick the first line, which wastes the "ls" sort.
> 
> Also, using "sort -R | head -n1" is inefficient. Furthermore, even without
> "head" or "tail", "shuf" is faster than "sort -R".
> 
> This series introduces a new helper _random_file() to choose a file in a
> directory randomly. Also, replace "sort -R" with _random_file() or "shuf".

Please discard this version. The _random_file() helper doesn't have the
base directory prefixed. So, the btrfs/192 didn't work well. I'll revise it
soon.