查找多个文本日志文件的聚合时间

时间:2012-02-20 03:25:26

标签: shell scripting vbscript batch-file

我的自动化脚本生成了很多日志文件,这些日志文件还记录了脚本启动的时间和完成时间。在同一个测试失败的情况下,存在一些重新存在。

我希望得到一些时间来计算运行所有脚本所花费的时间(~200个脚本,大约8000个测试用例)。问题是,有时脚本在没有运行单个案例的情况下中止,框架仍会将时间记录到日志文件中。我想做点什么:

Pick specific files with the same script name (as a single script can be run multiple times and has a log file for each of these runs in its name)
    Read the Log file for a single script
        If FAIL value = (total test cases) then skip the log file
        If ABORT value = (total test cases) then skip the log file
        If PASS value = (total test cases -1) then record the Start and End times to the output file
    Goto the next script in the same name set
Goto the next set of script files 

此外,一旦上述操作完成,我想聚合并平均时间并找到每个脚本和完整运行的平均时间。

使用shell脚本或DOS批处理有没有简单的方法呢?我不想为这个实现编写一个完整的程序?如果有人能告知是否可以使用VBScript进行相同操作以及如何进行,那会很棒吗?

档案详细信息:

LOG文件的名称:

TestScriptA_20120201.LOG
TestScriptA_20120202.LOG
TestScriptA_20120203.LOG
TestScriptB_20120201.LOG
TestScriptB_20120202.LOG
TestScriptC_20120202.LOG
TestScriptD_20120203.LOG

文件内容:

Report File:        TestScriptA_20120203.LOG
Date Created:       14-02-2012
Time Started:       09:21:03
Test Database:      staging
Processor:          proc_stage
Test Login:         ABC
Test Date :         14-02-2012


Test #1                          Status : Pass
Purpose  = First Test
DataFile = master1.tda
StepName = TS1 - Test Step 1


Test #2                          Status : Pass
Purpose  = First Test
DataFile = master2.tda
StepName = TS1 - Test Step 2




Test Step Results:     First Test

Total Tests:                         2
Tests run:                           2
Aborted Tests:                       0
Passed Tests:                        2
Failed Tests:                        0


Script finished at 09:26:45 on 14-02-2012
Execution Time : 00:05:42
-------------------------------  End Of Report -----------------------------

1 个答案:

答案 0 :(得分:3)

我假设脚本文件的扩展名为.BAT,因此该批处理文件不能与脚本和日志文件位于同一目录中,但必须在其上运行。也许您需要在开始时添加CD命令...

@echo off
setlocal EnableDelayedExpansion
rem Initialize total time
set totalTime=0
set total=0
rem For each script file...
for %%s in (*.bat) do (
   rem Initialize aggregate time for this script
   set "aggregateTime[%%~Ns]=0"
   set "%%~Ns=0"
   rem For each log file created by this script file...
   for %%l in ("%%~Ns*.log") do (
      rem Get Passed Tests
      for /F "tokens=3" %%p in ('findstr /C:"Passed Tests:" %%l') do set passed=%%p
      rem If Passed Tests is greater than zero...
      if !passed! gtr 0 (
         rem Get Start and End times
         for /F "tokens=3" %%s in ('findstr /C:"Time Started:" %%l') do set start=%%s
         for /F "tokens=4" %%e in ('findstr /C:"Script finished" %%l') do set end=%%e
         rem Record Start and End times to output file
         echo Log file: %%l, Start time: !start!, End time: !end!
         rem Get Elapsed time
         rem BEWARE! Result in log file have a space in "Execution Time :" text
         rem If it is deleted, change 4 by 3 in the line below
         for /F "tokens=4" %%e in ('findstr /C:"Execution Time" %%l') do set elapsed=%%e
         rem Convert elapsed time to seconds
         for /F "tokens=1-3 delims=:" %%a in ("!elapsed!") do (
            set "elapsed=((1%%a %% 100)*60+(1%%b %% 100))*60+(1%%c %% 100)"
         )
         rem Add elapsed time to aggregates
         set /A "aggregateTime[%%~Ns]+=elapsed"
         set /A "%%~Ns+=1"
         set /A totalTime+=elapsed
         set /A total+=1
      )      
   )
   echo --------------------------------------------------
   echo/
)
echo/
echo --------------------------------------------------
echo Aggregate and average times for each script (in seconds):
for /F "tokens=2,3 delims=[]=" %%a in ('set aggregateTime[') do (
   set /A "average=%%b/%%a"
   echo %%a  !aggregateTime[%%a]!  !average!
)
echo/
echo Aggregate and average total times (in seconds):
set /A average=totalTime/total
echo !totalTime!  !average!

这是第一个版本。可以修复或调整若干细节,例如将聚合和平均时间转换为HH:MM:SS格式。