简体   繁体   English

“干净代码”对性能的影响

[英]Performance implications with 'clean code'

At my workplace we're planning a major refactor on our core product, a web application with several 'modules'. 在我的工作场所,我们正在计划对我们的核心产品(具有多个“模块”的Web应用程序)进行重大重构。 I quoted that because that's one of our main concerns: modules are not really modules, the whole thing is monolithic. 我引用这是因为这是我们主要关注的问题之一:模块并不是真正的模块,整个事情都是整体的。 The application is written in PHP with smarty templating and using Pear for accessing a MySQL database. 该应用程序是用PHP编写的,模板巧妙,并使用Pear访问MySQL数据库。 We're not really concerned with database independence, although it would be nice if that wouldn't take months to implement. 我们并不真正关心数据库的独立性,尽管如果不需要花费数月的时间来实现,那会很好。

Our main concerns are that development time/cost is increasing exponentially because of bugs popping up in unrelated places and not having a sound common architecture to rely on to get the most common functionality (each module is basically copy/paste from the previous one, then adapt). 我们主要担心的是,开发时间/成本成倍增加,这是因为在不相关的地方会弹出错误,并且没有健全的通用体系结构可以依靠以获取最通用的功能(每个模块基本上都是从上一个模块进行复制/粘贴,然后适应)。

I've got some experience with the web MVC principle, mainly in ASP.NET MVC. 我对Web MVC原理有一些经验,主要是在ASP.NET MVC中。 I like the clean separation it offers and the testability. 我喜欢它提供的干净分离和可测试性。 However, when trying this on a local machine the app is simply a lot slower than it should be. 但是,在本地计算机上尝试该应用程序时,它的速度实际上比预期的要慢得多。

Alright, enough introduction, off to the questions: - Should I rely on caching modules? 好了,足够的介绍了,开始讨论以下问题:-我应该依靠缓存模块吗? Does this remove most of the overhead using a good architecture provide? 使用良好的体系结构是否可以消除大部分开销? Something like APC. 像APC之类的东西。

  • The application is mainly read. 主要阅读该应用程序。 Writing is mainly single values (change a single field on a record). 写入主要是单个值(更改记录上的单个字段)。 Any OR/M for PHP that are good at this? 任何擅长PHP的OR / M?
  • Also looking for a flexible MVC framework. 还正在寻找灵活的MVC框架。 I know Zend, CakePHP, maybe Symfony? 我知道Zend,CakePHP,也许是Symfony?

The tricky part is that we don't have the luxury of being able to do a full rewrite. 棘手的部分是,我们没有能力进行完全重写。 We'll have to incrementally improve a currently very messy codebase. 我们将不得不逐步改进当前非常混乱的代码库。 This has to be done while writing new code, or fixing bugs. 这必须在编写新代码或修复错误时完成。 One thing I'd really, REALLY like to be able to do is write a regression test for a new bug before fixing it, to prevent it from popping up again later (this happens, occasionally). 我真的很想做的一件事是在修复新错误之前编写回归测试,以防止以后再次弹出(偶尔发生这种情况)。

The stack I'm currently considering contains: 我当前正在考虑的堆栈包含:

  • MVC framework of choice 选择的MVC框架
  • Logging (log4php?) 记录(log4php?)
  • an OR/M of choice (doesn't have to be dynamic, code generation is fine too) 选择的OR / M(不必动态,代码生成也可以)
  • IoC container of choice 首选的IoC容器
  • Smarty Templating, perhaps abstracted so we can switch it out if we need to. Smarty模板,可能是抽象的,因此我们可以根据需要将其切换出来。
  • Opcode cache of choice (we're using one now, forgot which one, have to ask sysadmin) 选择的操作码缓存(我们现在正在使用一个,忘记了哪个,必须询问sysadmin)

The main point that worries me is the performance implications of creating clean code in PHP. 让我担心的要点是在PHP中创建干净代码的性能影响。 Seeing it's a parsed language opposed to something like the .NET/Java web stack, creating abstractions for otherwise in-line code (with obligatory separation in different files) might create new problems on another level. 看到它是一种与.NET / Java Web堆栈之类的东西相对的经过解析的语言,为否则为内联代码创建抽象(必须在不同文件中进行分隔)可能会在另一个层次上带来新问题。


Note: Retag if you come up with more appropriate tags, I'm not sure on the current ones. 注意:如果您想出更合适的标签,请重新标记,但不确定当前使用的标签。

Having a clean setup isn't a performance issue, usually. 通常,干净的设置不是性能问题。 Most performance is spent with databases or other external systems you're talking to. 大多数性能都花在您正在与之交谈的数据库或其他外部系统上。

Except for these there are usually one or two hotspots which might be worth optimizing but for that you should start with a clean design, then use a profiler (like XDebug or ZendDebugger) to identify the bottlenecks. 除此之外,通常有一个或两个热点可能值得优化,但为此您应该从一个干净的设计开始,然后使用探查器(例如XDebug或ZendDebugger)来识别瓶颈。

A clean software design is way more important than the 0.01% performance gain by a "optimized" design. 干净的软件设计比“优化”设计的0.01%性能提升更为重要。 Usuallyit's even cheaper to buy and run more hardware than worry about an "optimized" codebase which is unmaintainable. 通常,购买和运行更多的硬件要比担心“优化”的代码库难以维护要便宜得多。

I'd stress budgeting time to build tests, with the following arguments to management: 我会强调预算时间来构建测试,其中包含以下管理参数:

  1. When developers fix a bug, allow them to write a test for the bug. 开发人员修复错误后,请允许他们编写该错误的测试。 Bugs reoccur much more often than they probably should, and this is a cheap and effective way to stop that completely. 错误的发生频率比可能的要高得多,这是一种完全停止该错误的廉价有效方法。
  2. When developers are building new functionality, allow them to write tests under it. 当开发人员构建新功能时,允许他们在其下编写测试。 Since they're completely familiar with the functionality at that point, this is the least expensive time to build the "safety net" of automated testing. 由于他们当时对功能完全熟悉,因此这是构建自动化测试“安全网”的最省钱的时间。

Don't candy coat how long testing will take you; 不要涂糖果测试需要多长时间; whether it's 1% of your time or 50%, give that to the manager straight, but stress that building automated testing as a safety net will stop users from hitting as many bugs, and will save developer time for new development instead of bugfixing. 无论是您的时间的1%还是50%的时间,都应直接交给经理,但要强调的是,将自动化测试作为安全网进行构建将阻止用户遇到许多错误,并为开发人员节省了进行新开发的时间,而无需进行错误修复。

As far as managing an MVC component with a spaghetti code component, we had a similar issue with a large project. 至于使用意大利面条式代码组件管理MVC组件,我们在大型项目中遇到了类似的问题。 What worked well was just taking a directory and making that the new docroot for MVC app (Zend Framework in our case) such that: 效果很好的是仅获取一个目录,并为MVC应用程序创建新的docroot(在本例中为Zend Framework),使得:

old part: 旧部分:
http://site.com/data.php http://site.com/data.php
http://site.com/other.php http://site.com/other.php

new part: http://site.com/app/controller/action/ ... 新部分: http//site.com/app/controller/action/ ...

Re authentication, you have a couple of choices. 重新认证,您有两种选择。 Probably the most logical is to redirect your login.php script to the MVC login and then pass it back to the original page that you want to go with necessary info passed as a GET parameter. 可能最合乎逻辑的方法是将您的login.php脚本重定向到MVC登录名,然后将其传递回要使用的原始页面,并将必要的信息作为GET参数传递。 This will allow legacy and new systems to exist simultaneously and transparently. 这将允许旧系统和新系统同时透明地存在。

Re slowness, before I would pull out XDebug, I would try to isolate a problematic part and just output times it takes. 再慢一点,在我拿出XDebug之前,我会尝试隔离出有问题的部分并仅输出所需的时间。 Faster IMHO. 恕我直言。

There isn't any good reason that well structured object oriented code should perform significantly worse than sapghetti php code in a database driven web application. 没有充分的理由说明,在数据库驱动的Web应用程序中,结构良好的面向对象的代码的性能应比sapghetti php代码差得多。 You need to do some profiling to find where your bottlenecks are and optimize accordingly. 您需要进行一些分析,以找出瓶颈所在,并相应地进行优化。

You do have a tough (but not uncommon) situation. 您的处境确实很艰难(但并不罕见)。

As far as organizing the code to minimize bugs, all I can give is a tip of the cap to DRY. 至于组织代码以最大程度地减少错误,我所能提供的只是DRY的小费。

For performance issues, those are easy to find, because their very slowness shows them to you, by this technique . 对于性能问题,这些问题很容易找到,因为通过这种技术 ,它们的缓慢性会向您显示。

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

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