【问题标题】:Finding aggregate time across multiple text log files跨多个文本日志文件查找聚合时间
【发布时间】:2012-03-10 11:31:20
【问题描述】:

我的自动化脚本生成了很多日志文件,这些文件还记录了脚本的启动时间和完成时间。在相同的地方有一些重复,个别测试失败了。

我希望汇总时间来计算运行所有脚本(约 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是否可以做到这一点以及如何做到这一点,那就太好了?

文件详情:

日志文件的名称:

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 -----------------------------

【问题讨论】:

  • 这可以通过一个 .BATch 文件来完成,尽管方式不是很简单。请张贴文件名的具体细节(脚本和日志文件的名称?我们如何知道多个文件属于同一个脚本?)行的格式是什么? FAIL、ABORT 和 PASS 值出现在哪里?开始和结束时间在哪里,格式是什么?等等……
  • @Aacini 添加了文件名和内容。

标签: shell scripting vbscript batch-file


【解决方案1】:

我假设脚本文件有.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 格式。

【讨论】:

  • 我收到以下错误:汇总和平均总时间(以秒为单位):除以零错误。 0 将尝试解决它。谢谢:-)
  • @gagneet:每个脚本之前的平均时间呢?如果这些时间是正确的,那么总平均时间标记一个错误是很奇怪的,因为两个计算实际上是相同的!
  • 两个平均时间都出现了相同的错误。我认为脚本无法正确解析和验证时间,因此其中没有存储任何值(或者存在范围问题)。我正在努力了解并找出问题所在。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-02-13
  • 2015-12-12
  • 2011-05-23
  • 2011-06-10
  • 2018-07-04
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多