1
0
mirror of https://github.com/akpaevj/OneSTools.TechLog.git synced 2024-12-06 08:16:09 +02:00

Оптимизирован алгоритм парсинга текста события

This commit is contained in:
Акпаев Евгений Александрович 2019-10-01 14:56:56 +03:00
parent 0367481d94
commit 847af3b469
7 changed files with 191 additions and 54 deletions

View File

@ -5,6 +5,11 @@ VisualStudioVersion = 16.0.29318.209
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "OneSTechLog", "OneSTechLog\OneSTechLog.csproj", "{BC6E4DCE-2722-4E6F-BCBC-8945DE1127DD}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "OneSTechLogTest", "OneSTechLogTest\OneSTechLogTest.csproj", "{0FA99F0C-E533-46CB-A2BE-D83BDC6D4698}"
ProjectSection(ProjectDependencies) = postProject
{BC6E4DCE-2722-4E6F-BCBC-8945DE1127DD} = {BC6E4DCE-2722-4E6F-BCBC-8945DE1127DD}
EndProjectSection
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
@ -15,6 +20,10 @@ Global
{BC6E4DCE-2722-4E6F-BCBC-8945DE1127DD}.Debug|Any CPU.Build.0 = Debug|Any CPU
{BC6E4DCE-2722-4E6F-BCBC-8945DE1127DD}.Release|Any CPU.ActiveCfg = Release|Any CPU
{BC6E4DCE-2722-4E6F-BCBC-8945DE1127DD}.Release|Any CPU.Build.0 = Release|Any CPU
{0FA99F0C-E533-46CB-A2BE-D83BDC6D4698}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{0FA99F0C-E533-46CB-A2BE-D83BDC6D4698}.Debug|Any CPU.Build.0 = Debug|Any CPU
{0FA99F0C-E533-46CB-A2BE-D83BDC6D4698}.Release|Any CPU.ActiveCfg = Release|Any CPU
{0FA99F0C-E533-46CB-A2BE-D83BDC6D4698}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE

View File

@ -29,53 +29,32 @@ namespace OneSTechLog
/// </summary>
public Action<Dictionary<string, string>> EventHandler { get => eventHandler; set => eventHandler = value; }
public TechLogParser(
string folder,
Action<Dictionary<string, string>> eventHandler,
ExecutionDataflowBlockOptions readBlockOptions = null,
ExecutionDataflowBlockOptions parseBlockOptions = null,
ExecutionDataflowBlockOptions eventHandlerBlockOptions = null)
/// <summary>
/// Creates a new instance of TechLogParser class
/// </summary>
/// <param name="folder"></param>
/// <param name="eventHandler"></param>
public TechLogParser(string folder, Action<Dictionary<string, string>> eventHandler)
{
Folder = folder;
EventHandler = eventHandler;
if (readBlockOptions == null)
readBlockOptions = new ExecutionDataflowBlockOptions
{
this.readBlockOptions = new ExecutionDataflowBlockOptions
{
MaxDegreeOfParallelism = Environment.ProcessorCount
};
}
else
{
this.readBlockOptions = readBlockOptions;
}
MaxDegreeOfParallelism = Environment.ProcessorCount
};
if (parseBlockOptions == null)
parseBlockOptions = new ExecutionDataflowBlockOptions
{
this.parseBlockOptions = new ExecutionDataflowBlockOptions
{
MaxDegreeOfParallelism = Environment.ProcessorCount,
BoundedCapacity = 10000
};
}
else
{
this.parseBlockOptions = parseBlockOptions;
}
MaxDegreeOfParallelism = Environment.ProcessorCount,
BoundedCapacity = 10000
};
if (eventHandlerBlockOptions == null)
eventHandlerBlockOptions = new ExecutionDataflowBlockOptions
{
this.eventHandlerBlockOptions = new ExecutionDataflowBlockOptions
{
MaxDegreeOfParallelism = Environment.ProcessorCount,
BoundedCapacity = 10000
};
}
else
{
this.eventHandlerBlockOptions = eventHandlerBlockOptions;
}
MaxDegreeOfParallelism = Environment.ProcessorCount,
BoundedCapacity = 10000
};
}
/// <summary>
@ -86,7 +65,7 @@ namespace OneSTechLog
{
var eventHandlerBlock = new ActionBlock<Dictionary<string, string>>(EventHandler, eventHandlerBlockOptions);
var parseEventBlock = new TransformBlock<string, Dictionary<string, string>>(ParseEventData, parseBlockOptions);
var readFileBlock = new ActionBlock<string>(async (filePath) => await ReadFile(filePath, parseEventBlock), readBlockOptions);
var readFileBlock = new ActionBlock<string>((filePath) => ReadFile(filePath, parseEventBlock), readBlockOptions);
parseEventBlock.LinkTo(eventHandlerBlock);
@ -94,7 +73,7 @@ namespace OneSTechLog
foreach (var filePath in files)
{
await SendDataToNextBlock(filePath, readFileBlock);
SendDataToNextBlock(filePath, readFileBlock);
}
var readBlockTask = readFileBlock.Completion.ContinueWith(c => parseEventBlock.Complete());
@ -105,7 +84,7 @@ namespace OneSTechLog
await Task.WhenAll(readBlockTask, parseEventBlockTask, eventHandlerBlock.Completion);
}
private async Task ReadFile(string filePath, ITargetBlock<string> nextBlock)
private void ReadFile(string filePath, ITargetBlock<string> nextBlock)
{
using (var stream = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite | FileShare.Delete))
using (var reader = new StreamReader(stream))
@ -119,7 +98,7 @@ namespace OneSTechLog
{
var currentLine = reader.ReadLine();
if (Regex.IsMatch(currentLine, @"^\d\d:\d\d\.\d+", RegexOptions.Compiled))
if (Regex.IsMatch(currentLine, @"^\d\d:\d\d\.", RegexOptions.Compiled))
{
if (firstEvent)
{
@ -127,38 +106,37 @@ namespace OneSTechLog
}
else
{
await SendDataToNextBlock(fileDateTime + ":" + currentEvent.ToString(), nextBlock);
SendDataToNextBlock(fileDateTime + ":" + currentEvent.ToString(), nextBlock);
currentEvent.Clear();
}
currentEvent.Append(currentLine);
currentEvent.AppendLine(currentLine);
}
else
{
currentEvent.Append(currentLine);
currentEvent.AppendLine(currentLine);
}
}
while (!reader.EndOfStream);
await SendDataToNextBlock(fileDateTime + ":" + currentEvent.ToString(), nextBlock);
SendDataToNextBlock(fileDateTime + ":" + currentEvent.ToString(), nextBlock);
}
}
private Dictionary<string, string> ParseEventData(string eventData)
{
var properties = new Dictionary<string, string>
{
["EventName"] = Regex.Match(eventData, @",.*?,", RegexOptions.IgnoreCase | RegexOptions.Compiled).ToString().Trim(','),
["DateTime"] = Regex.Match(eventData, @"^.*?\.\d+", RegexOptions.IgnoreCase | RegexOptions.Compiled).ToString(),
["Duration"] = Regex.Match(eventData, @"-\d+?,", RegexOptions.IgnoreCase | RegexOptions.Compiled).ToString().Trim('-', ',')
["EventName"] = Regex.Match(eventData, @",.*?,", RegexOptions.Compiled).ToString().Trim(','),
["DateTime"] = Regex.Match(eventData, @"^.*?\.\d+", RegexOptions.Compiled).ToString(),
["Duration"] = Regex.Match(eventData, @"-\d+?,", RegexOptions.Compiled).ToString().Trim('-', ',')
};
var props = Regex.Matches(eventData, @",[\w:]+=.*?(?=(,[\w:]+=|$))", RegexOptions.IgnoreCase | RegexOptions.Singleline | RegexOptions.Compiled);
var props = Regex.Matches(eventData, @",[\w:]+=.*?(?=(,[\w:]+=|$))", RegexOptions.ExplicitCapture | RegexOptions.Singleline | RegexOptions.Compiled);
for (int x = 0; x < props.Count; x++)
{
var prop = props[x];
var propText = prop.ToString();
var propText = props[x].ToString();
var splInd = propText.IndexOf('=');
var propName = propText.Substring(0, splInd).Trim(',');
var propVal = propText.Substring(splInd + 1).Trim('\'', '"');
@ -168,9 +146,9 @@ namespace OneSTechLog
return properties;
}
private async Task SendDataToNextBlock<T>(T data, ITargetBlock<T> nextBlock)
private void SendDataToNextBlock<T>(T data, ITargetBlock<T> nextBlock)
{
while (!await nextBlock.SendAsync(data)) ;
while (!nextBlock.Post(data)) ;
}
private string[] GetTechLogFiles()
{

View File

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1" />
</startup>
</configuration>

View File

@ -0,0 +1,64 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{0FA99F0C-E533-46CB-A2BE-D83BDC6D4698}</ProjectGuid>
<OutputType>Exe</OutputType>
<RootNamespace>OneSTechLogTest</RootNamespace>
<AssemblyName>OneSTechLogTest</AssemblyName>
<TargetFrameworkVersion>v4.6.1</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
<Deterministic>true</Deterministic>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
<DebugType>full</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<DebugSymbols>true</DebugSymbols>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Threading.Tasks.Dataflow, Version=4.6.4.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>..\packages\System.Threading.Tasks.Dataflow.4.10.0\lib\netstandard2.0\System.Threading.Tasks.Dataflow.dll</HintPath>
</Reference>
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" />
<Reference Include="System.Net.Http" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
<None Include="App.config" />
<None Include="packages.config" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\OneSTechLog\OneSTechLog.csproj">
<Project>{bc6e4dce-2722-4e6f-bcbc-8945de1127dd}</Project>
<Name>OneSTechLog</Name>
</ProjectReference>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>

View File

@ -0,0 +1,40 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using OneSTechLog;
using System.Diagnostics;
namespace OneSTechLogTest
{
class Program
{
static object locker = new object();
static int i = 0;
static async Task Main(string[] args)
{
var parser = new TechLogParser(@"C:\Users\akpaev.e.ENTERPRISE\Desktop\ExpertTools\tl", EventHandler);
var watch = new Stopwatch();
watch.Start();
await parser.Parse();
watch.Stop();
Console.WriteLine($"Считано событий: {i}");
Console.WriteLine($"Время выполнения: {watch.Elapsed}");
Console.ReadKey();
}
private static void EventHandler(Dictionary<string, string> eventData)
{
lock(locker)
{
i++;
}
}
}
}

View File

@ -0,0 +1,36 @@
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
// Общие сведения об этой сборке предоставляются следующим набором
// набора атрибутов. Измените значения этих атрибутов для изменения сведений,
// связанные с этой сборкой.
[assembly: AssemblyTitle("OneSTechLogTest")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("HP Inc.")]
[assembly: AssemblyProduct("OneSTechLogTest")]
[assembly: AssemblyCopyright("Copyright © HP Inc. 2019")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]
// Установка значения False для параметра ComVisible делает типы в этой сборке невидимыми
// для компонентов COM. Если необходимо обратиться к типу в этой сборке через
// из модели COM задайте для атрибута ComVisible этого типа значение true.
[assembly: ComVisible(false)]
// Следующий GUID представляет идентификатор typelib, если этот проект доступен из модели COM
[assembly: Guid("0fa99f0c-e533-46cb-a2be-d83bdc6d4698")]
// Сведения о версии сборки состоят из указанных ниже четырех значений:
//
// Основной номер версии
// Дополнительный номер версии
// Номер сборки
// Номер редакции
//
// Можно задать все значения или принять номера сборки и редакции по умолчанию
// используя "*", как показано ниже:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]

View File

@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="System.Threading.Tasks.Dataflow" version="4.10.0" targetFramework="net461" />
</packages>