简体   繁体   English

如何确定项目/解决方案中最常使用的代码?

[英]How to determine which code in a project/solution is the most often used?

If I have an existing solution containing multiple c# projects, are there any static analysis tools that can help me determine which areas of code are the most often used? 如果我有一个包含多个c#项目的现有解决方案,是否有任何静态分析工具可以帮助我确定哪些代码区域最常用?

I'd like to use this information in order to determine which areas should have their test coverage ramped up first. 我想使用这些信息来确定哪些区域应该首先增加测试覆盖率。

I've looked at some static analysis tools already, but they mostly seem to focus on things like complexity, coding conventions, code duplication etc. 我已经看过一些静态分析工具,但它们似乎主要集中在复杂性,编码约定,代码复制等方面。

Alternatively, if there aren't any analysis tools available that can do this, do you have any advice about how to determine which code I ought to focus on testing first? 或者,如果没有任何可用的分析工具可以做到这一点,您是否有任何关于如何确定我应该首先关注测试的代码的建议?

Thanks! 谢谢!

EDIT: Just to clarify, what I'm looking for isn't code coverage. 编辑:只是为了澄清,我正在寻找的不是代码覆盖。 I'd like a rundown of which parts of my application are most often used, so that I can in turn focus on improving the coverage in those areas. 我想了解我最常使用的应用程序的哪些部分,以便我可以反过来专注于改善这些领域的覆盖范围。 I'm trying to avoid just writing tests for areas that don't yet have any, as they may be edge cases that aren't often executed. 我试图避免只为那些还没有的区域编写测试,因为它们可能是不经常执行的边缘情况。

Even static analysis tools which do try to figure out what happens at run-time do not usually try to estimate how often a piece of code is executed. 即使是试图弄清楚在运行时发生的事情的静态分析工具通常也不会尝试估计一段代码的执行频率。 The subject is hard enough as it is! 这个主题很难实现!

But dynamic analysis tools (eg profiling tools that either rely on transparent instrumentation of the code or use sampling) can tell you, after one or several "typical" executions (you provide the entries that you judge typical), how often this or that function was executed. 但是动态分析工具(例如,依赖于代码的透明检测或使用采样的分析工具)可以告诉您,在一次或几次“典型”执行(您提供您判断的典型条目)之后,这个或那个函数的频率被执行了。

See Profiling (computer programming) on Wikipedia . 请参阅Wikipedia上的性能分析(计算机编程)

If I understood the question right, you are looking for a profiler. 如果我理解正确的问题,那么您正在寻找一个分析器。 Give EQATEC Profiler a try. 试试EQATEC Profiler吧。 It's free. 免费。

It's originally intended to profile an application before shipping (to detect bottlenecks by measuring execution time of methods etc.) so I'm not sure if it's suitable for an application in a productive environment. 它原本打算在发货之前分析应用程序(通过测量方法的执行时间来检测瓶颈等),所以我不确定它是否适合在高效环境中的应用程序。 At least it changes your code for profiling purposes and that might be unwanted. 至少它会更改您的代码以进行性能分析,这可能是不需要的。 You should check this out. 你应该看看这个。

Code coverage appears to be what you want. 代码覆盖率似乎是您想要的。

NCover is a popular code coverage tool for .NET, if you can afford it. NCover是一种流行的.NET代码覆盖工具,如果你负担得起的话。

If you want to see just what is used: SD C# Test Coverage Tool 如果你想看到的是使用正是SD C#测试覆盖工具

If you want to see how often it is used: SD C# Profiler Tool 如果您想查看它的使用频率SD C#Profiler工具

What you're asking for is simply impossible to do accurately. 你所要求的是根本无法准确做到的。 The number of times something is executed can and usually will depend on the data that's entered at run-time. 执行某些操作的次数通常取决于在运行时输入的数据。 The best you can hope for from a static analysis tool is 您可以从静态分析工具中获得最佳效果

  1. a direct answer when statically determined 静态确定时的直接答案
  2. An O(N) style analysis otherwise 否则是O(N)风格分析
Even the latter would be quite difficult to get right overall. 即使是后者也很难整体上做得很好。 For example, it would need to know the complexity of essentially every function in the (huge and ever-expanding) .NET library. 例如,它需要知道(庞大且不断扩展的).NET库中基本上每个函数的复杂性。 Some of those are hard to even characterize. 其中一些甚至很难描述。

Just for example, how long does it take to allocate a block of memory? 例如,分配一块内存需要多长时间? Well, usually it's usually nearly constant time -- but it's always possible that an allocation can trigger a garbage collection cycle, in which case the time taken will be (roughly) proportional to the number of objects still in use that were allocated since the last GC cycle... 好吧,通常它通常几乎是恒定的时间 - 但是分配总是可能触发垃圾收集周期,在这种情况下,所花费的时间将(大致)与自上次分配以来分配的仍在使用的对象数量成比例GC循环......

"Profiler" is what you're looking for; “Profiler”是您正在寻找的; which you choose is up to you. 您选择的是由您自己决定的。

I've used HP's Diagnostic Server to do this, although it'd cost money. 我已经使用惠普的诊断服务器来做到这一点,虽然它需要花钱。 It'll tell me what methods are called how many times, and the average and worst-case time spent in them. 它会告诉我什么方法被称为多少次,以及在它们中花费的平均和最坏情况时间。

As an important safety tip, running a profiler will slow down the execution of your code; 作为一个重要的安全提示,运行探查器会降低代码的执行速度; it's not ideal for a long-term installation into a production environment. 它不适合长期安装到生产环境中。

If coverage is not what you look for then you could look use two things: 如果覆盖范围不是您所寻找的,那么您可以使用两件事:

  1. As suggested a profiler and run predictive test scenarios. 正如建议的剖析器和运行预测测试场景。
  2. Use performance counters which would end in a more permanent solution. 使用性能计数器,这将以更永久的解决方案结束。 These are quite difficult to implement but are helpful to diagnose performance deltas by analyzing the counters report. 这些很难实现,但通过分析计数器报告有助于诊断性能增量。 One way to implement them would be to wrap boundaries and manage counters from those wrap. 实现它们的一种方法是包装边界并从那些包装中管理计数器。 Be wary that it's way much easier to integrate in a new project than in an existing one. 要小心,在新项目中集成比在现有项目中更容易。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM