0

I'm writing a bash script to benchmark write/read speeds through time dd command, time writes on stderr meanwhile dd writes on stdout, with the 2>&1 i'm redirecting the output I get to stdout and then i read in the variable time_writing.

dd_output=$(time dd bs="$bs" conv=fsync count=$count if=/dev/zero of="${file_temporaneo:-$block_device}" 2>&1)
write_speed=$(grep --only-matching --extended-regexp --ignore-case '[0-9.]+ ([GMk]?B|bytes)/s(ec)?' <<< "$dd_output")
time_writing=$(sed -n 's/real"\(."\)/\1/p' <<< "$dd_output")

I don't know how to get the time to output in the stdout just as dd command does, with: (time dd if=/dev/urandom of=/dev/null bs=1K count=100000) 2>&1|& sed -n 's/real\t*\(.*\)/\1/p' I'm able to get just the real time, but I also need the writing speed.

This is the output that i'm getting from the commands:

real    0m1,585s
user    0m0,060s
sys 0m1,352s
       512 -    170 MB/s -           

real    0m0,876s
user    0m0,024s
sys 0m0,719s
      1024 -    307 MB/s -           

real    0m0,584s
user    0m0,020s
sys 0m0,419s
      2048 -    460 MB/s -           

real    0m0,478s
user    0m0,012s
sys 0m0,305s
      4096 -    563 MB/s -           

real    0m0,435s
user    0m0,000s
sys 0m0,281s
      8192 -    618 MB/s -           

real    0m0,379s
user    0m0,000s
sys 0m0,261s
     16384 -    710 MB/s -           

real    0m0,402s
user    0m0,004s
sys 0m0,242s
     32768 -    670 MB/s -           

real    0m0,363s
user    0m0,000s
sys 0m0,232s
     65536 -    742 MB/s -           

real    0m0,387s
user    0m0,000s
sys 0m0,223s
    131072 -    695 MB/s -           

real    0m0,377s
user    0m0,004s
sys 0m0,218s
    262144 -    714 MB/s -           

real    0m0,364s
user    0m0,004s
sys 0m0,217s
    524288 -    741 MB/s -           

real    0m0,443s
user    0m0,000s
sys 0m0,286s
   1048576 -    607 MB/s -           

real    0m0,393s
user    0m0,000s
sys 0m0,260s
   2097152 -    687 MB/s -           

real    0m0,433s
user    0m0,004s
sys 0m0,282s
   4194304 -    623 MB/s -           

real    0m0,496s
user    0m0,000s
sys 0m0,245s
   8388608 -    543 MB/s -           

real    0m0,414s
user    0m0,000s
sys 0m0,246s
  16777216 -    651 MB/s -           

real    0m0,397s
user    0m0,000s
sys 0m0,238s
  33554432 -    681 MB/s -           

real    0m0,439s
user    0m0,000s
sys 0m0,293s
  67108864 -    617 MB/s -

What I'm getting without the time (just dd) is:

   512 -    192 MB/s -           
  1024 -    314 MB/s -           
  2048 -    429 MB/s -           
  4096 -    573 MB/s -           
  8192 -    637 MB/s -           
 16384 -    666 MB/s -           
 32768 -    702 MB/s -           
 65536 -    683 MB/s -           
131072 -    716 MB/s - 

What I'd like to get instead is:

     512 -    192 MB/s -     0m0,672s  
      1024 -    314 MB/s -   0m0,572s      
      2048 -    429 MB/s -   0m0,472s        
      4096 -    573 MB/s -   0m0,352s        

And so on... Thank you for anyone willing to help me out...

Boxxo
  • 3
  • 1
  • 2

1 Answers1

0

Modify this command:

dd_output=$(time dd bs="$bs" conv=fsync count=$count if=/dev/zero of="${file_temporaneo:-$block_device}" 2>&1)

like this to include the output produced by time:

dd_output=$( { time dd bs="$bs" conv=fsync count=$count if=/dev/zero of="${file_temporaneo:-$block_device}"; } 2>&1 )
Fonic
  • 2,625
  • 23
  • 20
  • In [How to Answer](https://stackoverflow.com/help/how-to-answer), note the section *Answer Well-Asked Questions*, and therein the bullet point regarding questions that "have been asked and answered many times before". – Charles Duffy Oct 24 '21 at 15:51
  • Thank you so much @Maxxim , I had been looking through all the stack overflow answers and couldn't figure out how to do it... – Boxxo Oct 24 '21 at 17:14