2015-09-25 23 views
0

我在搜索文件内容时遇到性能问题。我正在使用FileStream类来读取文件(每个搜索将涉及大约10个文件,每个文件大小约为70 MB)。但是,在我的搜索过程中,所有这些文件都将被另一个进程同时访问和更新。因此,我不能使用Buffersize来读取文件。尽管我使用的是正则表达式,但在StreamReader中使用缓冲区大小需要3分钟。如何使用FileSteam加速读取文件

有没有人遇到类似的情况,并可以提供任何提高文件搜索性能的指针?

代码段

private static int BufferSize = 32768; 
    using (FileStream fs = File.Open(filePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) 
     { 

      using (TextReader txtReader = new StreamReader(fs, Encoding.UTF8, true, BufferSize)) 

      { 
       System.Text.RegularExpressions.Regex patternMatching = new System.Text.RegularExpressions.Regex(@"(?=\d{4}-\d{2}-\d{2}\s\d{2}:\d{2}:\d{2})(.*?)(?=\n\d{4}-\d{2}-\d{2}\s\d{2}:\d{2}:\d{2})", System.Text.RegularExpressions.RegexOptions.IgnoreCase); 
       System.Text.RegularExpressions.Regex dateStringMatch = new Regex(@"^\d{4}-\d{2}-\d{2}\s\d{2}:\d{2}:\d{2}"); 
       char[] temp = new char[1048576]; 
       while (txtReader.ReadBlock(temp, 0, 1048576) > 0) 
       { 
        StringBuilder parseString = new StringBuilder(); 
        parseString.Append(temp); 
        if (temp[1023].ToString() != Environment.NewLine) 
        { 
         parseString.Append(txtReader.ReadLine()); 
         while (txtReader.Peek() > 0 && !(txtReader.Peek() >= 48 && txtReader.Peek() <= 57)) 
         { 
          parseString.Append(txtReader.ReadLine()); 
         } 
        } 
        if (parseString.Length > 0) 
        { 
         string[] allRecords = patternMatching.Split(parseString.ToString()); 
         foreach (var item in allRecords) 
         { 

          var contentString = item.Trim(); 
          if (!string.IsNullOrWhiteSpace(contentString)) 
          { 
           var matches = dateStringMatch.Matches(contentString); 
           if (matches.Count > 0) 
           { 

            var rowDatetime = DateTime.MinValue; 
            if (DateTime.TryParse(matches[0].Value, out rowDatetime)) 
            { 
             if (rowDatetime >= startDate && rowDatetime < endDate) 
             { 
              if (contentString.ToLowerInvariant().Contains(searchText)) 
              { 
               var result = new SearchResult 
               { 
                LogFileType = logFileType, 
                Message = string.Format(messageTemplateNew, item), 
                Timestamp = rowDatetime, 
                ComponentName = componentName, 
                FileName = filePath, 
                ServerName = serverName 
               }; 
               searchResults.Add(result); 
              } 
             } 
            } 

           } 

          } 
         } 
        } 
       } 
      } 
     } 

     return searchResults; 
+0

你看过http://stackoverflow.com/questions/14827350/faster-way-of-searching-a-string-in-text-files –

+0

你说多个文件正在被多个进程访问和更新,你怎么解决你的并发问题? –

+0

嗨Yacoub,我目前的搜索不打扰任何未来的更新文件。我所需要的是我需要打开该文件并用字符串进行搜索,目前可用。 – Uma

回答

-1

前一段时间,我不得不分析很多的FileZilla服务器日志文件与每个> 120MB。 我使用了一个简单的列表来获取每个日志文件的所有行,然后在搜索特定行时有很好的性能。

List<string> fileContent = File.ReadAllLines(pathToFile).ToList() 

但在你的情况下,我认为性能不佳的主要原因是不读取文件。尝试停止观察循环的某些部分以检查最耗时的地方。 Regex和TryParse如果在像你这样的循环中使用多次,可能会非常耗时。

+0

实际上,我的日志文件在整个文件中的格式不是相同的。 – Uma

+0

我需要将文件从一个匹配模式读取到另一个匹配模式。匹配专利不总是在同一行。匹配模式字符串中有换行符。所以我需要使用RegEx来识别匹配标准。可能我需要弄清楚如何优化RegEx来提高性能。 – Uma