Skip to content

Commit

Permalink
now you can see also how much time takes each method
Browse files Browse the repository at this point in the history
  • Loading branch information
ignatandrei committed Aug 30, 2018
1 parent cdecea8 commit dc6ace7
Show file tree
Hide file tree
Showing 9 changed files with 97 additions and 47 deletions.
4 changes: 4 additions & 0 deletions AOPRoslyn/AOPRoslyn/AOPRoslyn.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,8 @@
<PackageReference Include="Newtonsoft.Json" Version="11.0.2" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\AOPStatistics\AOPStatistics.csproj" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -3,34 +3,26 @@

namespace AOPStatistics
{
public struct Method
{
public string className;
public string methodName;
public int line;
}
public class AOPStatisticsMethod : IDisposable
public class AOPMethod : IDisposable
{
private readonly string methodName;
private readonly string className;
private readonly int line;
private readonly Stopwatch sw;
public AOPStatisticsMethod(string className, string methodName, int line)
public AOPMethod(string className, string methodName, int line)
{
sw = Stopwatch.StartNew();
this.className = className;
this.methodName = methodName;
this.line = line;
this.line = line;
}


public void Dispose()
{
sw.Stop();
AOPStatistics.PushMethod(new Method() { className = this.className, line= this.line, methodName= this.methodName}
GatherStatistics.PushMethod(new Method() { className = this.className, line= this.line, methodName= this.methodName}
, sw.ElapsedMilliseconds);


}
}
}
29 changes: 0 additions & 29 deletions AOPRoslyn/AOPStatistics/AOPStatistics.cs

This file was deleted.

4 changes: 2 additions & 2 deletions AOPRoslyn/AOPStatistics/AOPStatistics.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netcoreapp2.1</TargetFramework>
<TargetFramework>netstandard2.0</TargetFramework>
</PropertyGroup>

</Project>
49 changes: 49 additions & 0 deletions AOPRoslyn/AOPStatistics/GatherStatistics.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace AOPStatistics
{
public struct Data
{
public Method m;
public int NumberHits;
public long TotalDuration;
}

public class GatherStatistics
{
public static ConcurrentDictionary<Method, List<long>> timingMethod;
static GatherStatistics()
{
timingMethod = new ConcurrentDictionary<Method, List<long>>();
}

public static void PushMethod(Method m, long timeExecuting)
{
var data = new List<long>();
data.Add(timeExecuting);
timingMethod.AddOrUpdate(m,data ,(newValue, oldValue) =>
{
oldValue.Add(timeExecuting);
return oldValue;
}
);
}
public static Data[] DataGathered()
{
return GatherStatistics.timingMethod.Select(it =>
new Data()
{
m = it.Key,
NumberHits = it.Value.Count,
TotalDuration = it.Value.Sum(),

}).ToArray();

}

}
}
9 changes: 9 additions & 0 deletions AOPRoslyn/AOPStatistics/Method.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace AOPStatistics
{
public struct Method
{
public string className;
public string methodName;
public int line;
}
}
21 changes: 20 additions & 1 deletion AOPRoslyn/aopCmd/Program.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
using System;
using System.ComponentModel.DataAnnotations;
using System.IO;
using System.Linq;
using System.Reflection;
using AOPRoslyn;
using AOPStatistics;
using ConsoleTables;
using InterpreterDll;
using McMaster.Extensions.CommandLineUtils;

Expand All @@ -28,7 +31,23 @@ public static int Main(string[] args)
Console.WriteLine($"no arguments name file, taking default processme.txt file from on {pathFile}");
args = new string[] { pathFile };
}
return CommandLineApplication.Execute<Program>(args);
var ret= CommandLineApplication.Execute<Program>(args);
var table = new ConsoleTable("Class", "Method(Line)", "NumberHit", "TotalTime", "AverageTime");
if (GatherStatistics.timingMethod.Count == 0)
{
Console.WriteLine("no statistics loaded");
return ret;
}
var data = GatherStatistics.DataGathered()
.OrderByDescending(it => it.TotalDuration / it.NumberHits)
.ToArray();

foreach (var item in data)
{
table.AddRow(item.m.className, $"{item.m.methodName}({item.m.line})", item.NumberHits, item.TotalDuration, (item.TotalDuration / item.NumberHits));
}
table.Write(Format.Alternative);
return ret;
}

[Argument(0, Description = "The settings file to aop.\nYou can find a processme.txt near the executable.")]
Expand Down
2 changes: 2 additions & 0 deletions AOPRoslyn/aopCmd/aop.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,12 @@ dotnet aop processme.txt</Description>
<ItemGroup>
<PackageReference Include="InterpreterDll" Version="2018.7.13.3" />
<PackageReference Include="McMaster.Extensions.CommandLineUtils" Version="2.2.3" />
<PackageReference Include="PortableConsoleLibs" Version="1.0.0" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\AOPRoslyn\AOPRoslyn.csproj" />
<ProjectReference Include="..\AOPStatistics\AOPStatistics.csproj" />
</ItemGroup>

</Project>
10 changes: 7 additions & 3 deletions AOPRoslyn/processme.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"$type": "AOPRoslyn.RewriteCodeFolder, AOPRoslyn",
"ExcludeFileNames": ["TestClassPerson.cs","TestClassPersonWithArguments.cs"],
"ExcludeFileNames": ["TestClassPerson.cs","TestClassPersonWithArguments.cs","AOPMethod.cs","GatherStatistics.cs"],
"FolderName": "@static:Environment.CurrentDirectory@",
"SearchPattern": "*.cs",
"AddDefaultArguments": "true",
Expand All @@ -11,15 +11,19 @@
},
"MethodsToLog":1,
"AddDefaultArguments": true,
"FormatterFirstLine": "try
"FormatterFirstLine": "
using(new AOPStatistics.AOPMethod(\"{nameClass}\",\"{nameMethod}\",{lineStartNumber})){{
try
{{
System.Console.WriteLine(\"start method {nameClass}_{nameMethod}_{lineStartNumber}\");System.Console.WriteLine(\"!!!arguments \" + {arguments});
",
"FormatterLastLine": "}}
"FormatterLastLine": "
}}//try
catch(System.Exception ex)
{{
System.Console.WriteLine(\"error in{nameClass}_{nameMethod}_{lineStartNumber}:\" + ex.Message);throw;}}finally{{System.Console.WriteLine(\"end method {nameClass}_{nameMethod}_{lineStartNumber}\");
}}
}}//using
"
},
"Options": {
Expand Down

0 comments on commit dc6ace7

Please sign in to comment.