Finding code paths that frequently call a method in .NET
Does anyone have concrete advice for how to find code paths that are frequently calling a method at runtime. Are there any tools for assisting in this? I'm using c# and my main goal is to cut down on the number of times a method is being called. Thanks.
Also, lets say I add a section of code in the method that does something like
public void FunctionICareAbout() { var trace = new StackTrace(); var sb = new StringBuilder(); foreach (var frame in trace.GetFrames()) { var method = frame.GetMethod(); var ns = method.ReflectedType.Namespace; var className = method.ReflectedType.Name; var methodName = method.Name; var args = string.Join(", ", method.GetParameters().Select(x => x.ParameterType.ToString())); var methodString = $"{ns}.{className}.{methodName}({args})"; sb.Append($"{i}: {methodString}{Environment.NewLine}"); } LogToCentralRepositoryLikeSplunk(sb.ToString()) // do real work }
and I create a chart using that data that has the x axis be the stack trace and the y axis be the number of times its called. Would you think that was ridiculous or is that an okay solution to this problem? (I tried a version using Environment.StackTrace and string parsing instead of reflection as well, but it was waaaay slower because new StackTrace() seems to take around 4x less time than Environment.StackTrace)
0 comments:
Post a Comment