0

我想在“GSA 搜索”中计算总“经过时间”,但我面临着差异。在某些情况下,格式为“Elapsed Time: 97ms”,在某些情况下为“Elapsed Time:97ms”。我如何涵盖这两种情况?

以下是我的日志文件格式:

WX 搜索 = 服务器:nomos-scanner.corp.com 用户:vibsharm appGUID:wx 已用时间:975 毫秒 SaveSearchID:361
WX 搜索 = 服务器:nomos-scanner.corp.com 用户:vibsharm appGUID:wx 已用时间:875 毫秒 SaveSearchID:361
GSA 搜索 = 服务器:nomos-scanner.corp.com 用户:gulanand appGUID:wx 已用时间:890 毫秒 SaveSearchID:361
GSA 搜索 = 服务器:nomos-scanner.corp.com 用户:vibsharm appGUID:wx 已用时间:887 毫秒 SaveSearchID:361
GSA 搜索 = 服务器:nomos-scanner.corp.com 用户:gulanand appGUID:wx 经过时间:875.5 毫秒 SaveSearchID:361
GSA 搜索 = 服务器:nomos-scanner.corp.com 用户:vibsharm appGUID:wx 已用时间:877.6 毫秒 SaveSearchID:361

我的代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.IO;
using System.Linq.Expressions;

namespace ConsoleApplication5
{
    class Program
    {
        public static void Main(string[] args)
        {
            string searchKeyword = "WX GSA Search";
            string fileName = @"C:\Users\karan\Desktop\Sample log file.txt";
            string[] textLines = File.ReadAllLines(fileName);

            List<string> results = new List<string>();

            foreach (string line in textLines)
            {
                if (line.Contains(searchKeyword))
                {
                    results.Add(line);
                }
            }
            var elapsedTime = results.SelectMany(line => line.ToLower().Split(' '))
            .Where(line => line.StartsWith("time"))
            .Select(timeLine => decimal.Parse(timeLine.Split(':')[1].Replace("ms", String.Empty)))
            .Average(time => time);
            Console.WriteLine(elapsedTime);
            // keep screen from going away
            // when run from VS.NET
            Console.ReadLine();
            }  
    }  
}
4

2 回答 2

0
        string x = @"Elapsed Time: 97ms";
        int startIndex = x.LastIndexOf("Elapsed Time");
        int endIndex = x.LastIndexOf("ms");
        //Here there might be a problem, you might need to change to endIndex - startIndex +1
        string valueSubString = x.Substring(startIndex, endIndex - startIndex);
        decimal value = decimal.Parse(valueSubString.Replace(':').Trim());
于 2013-03-04T07:45:08.857 回答
0

如果减少字符串分配,您可以加快速度,如下所示:

(理想情况下,我会完全使用有限状态机解析器,但逐行读取也是可以接受的):

Decimal totalMs = 0;
Int32 totalRecords = 0;

using(StringReader rdr = new StreamReader(fileName, Encoding.UTF8)) {

    String line;
    while( (line = rdr.ReadLine()) != null ) {

        if( line.IndexOf("GSA Search", StringComparison.OrdinalIgnoreCase ) != 0 ) continue;

        Int32 idxStart = line.IndexOf("Elapsed time:", StringComparison.OrdinalIgnoreCase ) + "Elapsed time:".Length;
        Int32 idxEnd   = line.IndexOf("ms", idxStart, StringComparison.OrdinalIgnoreCase );

        Decimal lineMs = Decimal.Parse( line.Substring( idxStart, idxEnd - idxStart ) );
        totalMs += lineMs;
        totalRecords++;
    }
}
Decimal average = totalMs / totalRecords;

调用.Trim是不必要的,因为允许空格,但如果您希望它更健壮Decimal.Parse,您可能需要考虑使用它。Decimal.TryParse

于 2013-03-04T07:49:42.243 回答