Reputation: 657
I am trying to get the total time to access a page and using this command:
curl -o NUL -s -w "%%{time_total}" http://stackoverflow.com
I am able to get the total time displayed in the command prompt.
However, now I would like to put this in a for loop like this:
echo off
SET /a i=0
:loop
IF %i%==10 GOTO END
echo Iteration %i%.
SET /a i=%i%+1
GOTO LOOP
:end
and sum up all the times I get, replacing echo Iteration %i%.
with something like
set /a var = curl -o NUL -s -w "%%{time_total}" http://stackoverflow.com
set /a vartotal = var + vartotal
But of course this doesn't work, and I know its partially because the times returned are decimal and batch can't handle decimals. Also, the URL may have non-alphabetic charcters in it such as ?, =, and &. Please help
Upvotes: 2
Views: 7007
Reputation: 447
The easiest way to do this is by redirecting the output of the result of the curl command to a file and the process that file.
It is a little bit more tricky because the number contains a comma (maybe a dot in your environment) and the command interpreter only does integer calculation and on my box it interpreted the number wrong. In addition if it the number started with a zero it assumed it was an OCTAL number so you have to strip that as well. Adding all this together would give you this command batch script:
set site=http://stackoverflow.com
set file=so.log
rem set this to , or . depending on your locale
set sep=,
set tot=0
del /Q %file%
rem add 10 values to the so.log file
for /L %%r in (1,1,10) do curl -o NUL -s -w "%%{time_total}\r\n" %site% >> %file%
rem read each line from the file
for /F %%a in (%file%) do call :sum "%%a"
goto :end
rem sum procedure takes a number as first parameter
:sum
set milli=%1
IF "%sep%"=="," set val=%milli:,=%
IF "%sep%"=="." set val=%milli:.=%
set val2=%val:"=%
rem strip 0 if that is the first char
rem otherwise the number is
rem considered to be octal
set lz=%val2:~0,1%
if @%lz%==@0 set val2=%val2:~1%
set /a "tot=tot+%val2%"
set milli=
set val=
set val2=
goto :EOF
:end
echo total time: %tot%
rem clean up
set sep=
set tot=
set site=
set file=
Upvotes: 2