Skip to content

Instantly share code, notes, and snippets.

@jmc734
Created October 30, 2014 10:18
Show Gist options
  • Save jmc734/6d8dc65116756d685e5f to your computer and use it in GitHub Desktop.
Save jmc734/6d8dc65116756d685e5f to your computer and use it in GitHub Desktop.
Batchfile to measure the elapsed time when doing an arbitrary number of pings to a specified IPv4 address
@echo off
setlocal
set ADDRESS=127.0.0.1
set COUNT=4
:START
set /P ADDRESS=Address to ping [%ADDRESS%]:
set /P COUNT=Number of requests [%COUNT%]:
rem The format of %TIME% is HH:MM:SS,CS for example 23:59:59,99
set STARTTIME=%TIME%
rem here begins the command you want to measure
ping -n %COUNT% %ADDRESS% 1>nul
rem here ends the command you want to measure
set ENDTIME=%TIME%
set /A STARTTIMEMS = (1%STARTTIME:~9,2%-100)*10
set /A ENDTIMEMS = (1%ENDTIME:~9,2%-100)*10
echo.
rem output as time
echo STARTTIME: %STARTTIME:~0,8%:%STARTTIMEMS%
echo ENDTIME: %ENDTIME:~0,8%:%ENDTIMEMS%
rem convert STARTTIME and ENDTIME to centiseconds
set /A STARTTIME=((((1%STARTTIME:~0,2%-100)*60 + (1%STARTTIME:~3,2%-100))*60 + (1%STARTTIME:~6,2%-100))*100 + (1%STARTTIME:~9,2%-100))*10
set /A ENDTIME=((((1%ENDTIME:~0,2%-100)*60 + (1%ENDTIME:~3,2%-100))*60 + (1%ENDTIME:~6,2%-100))*100 + (1%ENDTIME:~9,2%-100))*10
rem calculating the duration is easy
set /A DURATION=%ENDTIME%-%STARTTIME%
rem we might have measured the time inbetween days
if %ENDTIME% LSS %STARTTIME% set set /A DURATION=%STARTTIME%-%ENDTIME%
rem now break the centiseconds down to hours, minutes, seconds and the remaining centiseconds
set /A DURATIONH=%DURATION% / 3600000
set /A DURATIONM=(%DURATION% - %DURATIONH%*3600000) / 60000
set /A DURATIONS=(%DURATION% - %DURATIONH%*3600000 - %DURATIONM%*60000) / 1000
set /A DURATIONMS=(%DURATION% - %DURATIONH%*3600000 - %DURATIONM%*60000 - %DURATIONS%*1000)
rem some formatting
if %DURATIONH% LSS 10 set DURATIONH=0%DURATIONH%
if %DURATIONM% LSS 10 set DURATIONM=0%DURATIONM%
if %DURATIONS% LSS 10 set DURATIONS=0%DURATIONS%
if %DURATIONMS% LSS 10 set DURATIONMS=0%DURATIONMS%
rem outputing
rem echo STARTTIME: %STARTTIME% ms
rem echo ENDTIME: %ENDTIME% ms
echo DURATION: %DURATIONH%:%DURATIONM%:%DURATIONS%:%DURATIONMS%
echo.
set /P C=Run another test? (Y/N):
if /I "%C%" EQU "Y" goto :RESTART
if /I "%C%" EQU "y" goto :RESTART
goto :END
:RESTART
echo.
goto :START
:END
endlocal
PAUSE
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment