为什么我不能在Zend Framework 2中使用ClassMapAutoloader获得性能?

时间:2015-08-18 14:55:45

标签: php zend-framework2 autoloader

在我的公司,我们正在开发一个ZF2应用程序,没有程序;-)。我们正处于这样一个阶段:我们希望通过将其引入公共www来测试应用程序的某些部分。

我们准备了一个STAGE环境,现在我做了一些性能调整。我读到使用ClassMapAutoloaderStandardAutoLoader快得多,例如http://samminds.com/2012/11/zf2-performance-quicktipp-2-classmap-autoloading/。我明白为什么它应该更快但在我的情况下我用ClassMapAutoloader使用xdebug分析,WinCacheGring / QCacheGrind分析了有 <?php // Generated by ZF2's ./bin/classmap_generator.php return array( 'Search\Elasticsearch\Document\AbstractDocument' => __DIR__ . '/src/Search/Elasticsearch/Document/AbstractDocument.php', 'Search\Elasticsearch\Document\ArticleDocument' => __DIR__ . '/src/Search/Elasticsearch/Document/ArticleDocument.php', 'Search\Elasticsearch\Document\BookingDocument' => __DIR__ . '/src/Search/Elasticsearch/Document/BookingDocument.php', 'Search\Elasticsearch\Document\DocumentType' => __DIR__ . '/src/Search/Elasticsearch/Document/DocumentType.php', 'Search\Elasticsearch\Document\InvoiceDocument' => __DIR__ . '/src/Search/Elasticsearch/Document/InvoiceDocument.php', 'Search\Elasticsearch\Document\OfficeDocument' => __DIR__ . '/src/Search/Elasticsearch/Document/OfficeDocument.php', 'Search\Elasticsearch\Document\OfficeMemberDocument' => __DIR__ . '/src/Search/Elasticsearch/Document/OfficeMemberDocument.php', 'Search\Elasticsearch\Document\ProductDocument' => __DIR__ . '/src/Search/Elasticsearch/Document/ProductDocument.php', 'Search\Elasticsearch\Document\ProfileDocument' => __DIR__ . '/src/Search/Elasticsearch/Document/ProfileDocument.php', 'Search\Elasticsearch\Document\RatingDocument' => __DIR__ . '/src/Search/Elasticsearch/Document/RatingDocument.php', 'Search\Elasticsearch\Document\AbstractWebSearchDocument' => __DIR__ . '/src/Search/Elasticsearch/Document/AbstractWebSearchDocument.php', 'Search\Elasticsearch\AutoSuggestionQueryHandler' => __DIR__ . '/src/Search/Elasticsearch/AutoSuggestionQueryHandler.php', 'Search\Elasticsearch\SearchStatisticIndexHandler' => __DIR__ . '/src/Search/Elasticsearch/SearchStatisticIndexHandler.php', 'Search\Elasticsearch\TermRecognizerQueryHandler' => __DIR__ . '/src/Search/Elasticsearch/TermRecognizerQueryHandler.php', 'Search\Elasticsearch\SearchIndexHandler' => __DIR__ . '/src/Search/Elasticsearch/SearchIndexHandler.php', 'Search\Elasticsearch\SearchQueryHandler' => __DIR__ . '/src/Search/Elasticsearch/SearchQueryHandler.php', 'Search\Elasticsearch\TermRecognizerIndexHandler' => __DIR__ . '/src/Search/Elasticsearch/TermRecognizerIndexHandler.php', 'Search\Exception\Exception' => __DIR__ . '/src/Search/Exception/Exception.php', 'Search\Factory\AutoSuggestQueryHandlerFactory' => __DIR__ . '/src/Search/Factory/AutoSuggestQueryHandlerFactory.php', 'Search\Factory\AutoSuggestServiceFactory' => __DIR__ . '/src/Search/Factory/AutoSuggestServiceFactory.php', 'Search\Factory\DocumentStorerServiceFactory' => __DIR__ . '/src/Search/Factory/DocumentStorerServiceFactory.php', 'Search\Factory\QueueWorkerServiceFactory' => __DIR__ . '/src/Search/Factory/QueueWorkerServiceFactory.php', 'Search\Factory\SearchIndexHandlerFactory' => __DIR__ . '/src/Search/Factory/SearchIndexHandlerFactory.php', 'Search\Factory\SearchQueryHandlerFactory' => __DIR__ . '/src/Search/Factory/SearchQueryHandlerFactory.php', 'Search\Factory\SearchServiceFactory' => __DIR__ . '/src/Search/Factory/SearchServiceFactory.php', 'Search\Factory\SearchSimpleServiceFactory' => __DIR__ . '/src/Search/Factory/SearchSimpleServiceFactory.php', 'Search\Factory\SearchStatistikIndexHandlerFactory' => __DIR__ . '/src/Search/Factory/SearchStatistikIndexHandlerFactory.php', 'Search\Factory\TermRecognizerServiceFactory' => __DIR__ . '/src/Search/Factory/TermRecognizerServiceFactory.php', 'Search\Factory\TermrecognizerIndexHandlerFactory' => __DIR__ . '/src/Search/Factory/TermrecognizerIndexHandlerFactory.php', 'Search\Factory\TermrecognizerQueryHandlerFactory' => __DIR__ . '/src/Search/Factory/TermrecognizerQueryHandlerFactory.php', 'Search\Factory\RequestHandlerFactory' => __DIR__ . '/src/Search/Factory/RequestHandlerFactory.php', 'Search\Logger\LoggerInterface' => __DIR__ . '/src/Search/Logger/LoggerInterface.php', 'Search\Logger\StatisticLogger' => __DIR__ . '/src/Search/Logger/StatisticLogger.php', 'DatabaseQueue' => __DIR__ . '/src/Search/Queue/DatabaseQueue.php', 'Search\Search\QueryWordReducer' => __DIR__ . '/src/Search/Search/QueryWordReducer.php', 'Search\Search\RecognizedTermConsumer' => __DIR__ . '/src/Search/Search/RecognizedTermConsumer.php', 'Search\Search\SearchService' => __DIR__ . '/src/Search/Search/SearchService.php', 'Search\Search\SuggestionListBuilder' => __DIR__ . '/src/Search/Search/SuggestionListBuilder.php', 'Search\Search\Util' => __DIR__ . '/src/Search/Search/Util.php', 'Search\Search\ViewState' => __DIR__ . '/src/Search/Search/ViewState.php', 'Search\Search\ViewStateToSearchRequestTransformer' => __DIR__ . '/src/Search/Search/ViewStateToSearchRequestTransformer.php', 'Search\Search\SearchSimpleService' => __DIR__ . '/src/Search/Search/SearchSimpleService.php', 'Search\AutoSuggester' => __DIR__ . '/src/Search/AutoSuggester.php', 'Search\QueryCleaner' => __DIR__ . '/src/Search/QueryCleaner.php', 'Search\Request' => __DIR__ . '/src/Search/Request.php', 'Search\RequestHandler' => __DIR__ . '/src/Search/RequestHandler.php', 'Search\SearchSource' => __DIR__ . '/src/Search/SearchSource.php', 'Search\Util' => __DIR__ . '/src/Search/Util.php', 'Search\QueueWorker' => __DIR__ . '/src/Search/QueueWorker.php', 'Search\AbstractDocumentStorer' => __DIR__ . '/src/Search/AbstractDocumentStorer.php', 'Search\DocumentStorer' => __DIR__ . '/src/Search/DocumentStorer.php', 'Search\TermRecognizer' => __DIR__ . '/src/Search/TermRecognizer.php', 'Search\Module' => __DIR__ . '/Module.php', ); 的网站,并且约为0,2 %。

有谁知道为什么这会慢一点?

我正在使用CentOS和PHP Version =&gt; 5.6.12

编辑添加信息:

一个autoload_classmap.php示例:

public function getAutoloaderConfig() {
    return [
        'Zend\Loader\ClassMapAutoloader' => [
            __DIR__ . '/autoload_classmap.php'
        ],
        'Zend\Loader\StandardAutoloader' => [
            'namespaces' => [
                __NAMESPACE__ => __DIR__ . '/src/' . str_replace('\\', '/', __NAMESPACE__)
            ]
        ]
    ];
}

从相关的Module.php中提取:

{{1}}

我已经检查过Autoloader使用了类图。

编辑添加信息: Zend developer Toolbar 1

Zend developer Toolbar 2

Zend developer Toolbar 3

1 个答案:

答案 0 :(得分:1)

对不起,迟到了。现在你的应用加载了半秒 - 准确地说是0.6秒。所以我自动加载器正在做他的工作。您的查询也会快速执行。我还有两种方法可以加速您的应用程序。

  • 第一种方式 - 使用模板地图

找到templatemap_generator.php文件。它应该在您的vendor/zendframework/zendframework/bin文件夹中。导航到您的模块文件夹,例如Application文件夹所在的src, view, config目录。打开一个终端并键入php ../../vendor/zendframework/zendframework/bin/templatemap_generator.php ZF将在您的模块目录中创建一个模板映射。现在使用此模板,只需修改module.config.php文件即可。文件结构类似于clasmap_autoloader.php

中的文件结构
return array(
// Telling where the views are
'view_manager' => array(
    'display_not_found_reason' => true,
    'display_exceptions'       => true,
    'doctype'                  => 'HTML5',
    'not_found_template'       => 'error/404',
    'exception_template'       => 'error/index',
    'template_map' => include __DIR__ . '/../template_map.php', // <-- add this line. You can remove `template_path_stack`
),

在每个操作的控制器中添加一个视图模板。     public indexAction()     {         $ view = new ViewModel();         $视图 - &GT; setTemplate(&#34; aplication /索引/索引&#34);         return $ view;     }

  • 第二种方式 - 在生产环境中使用模块缓存。

我们假设您在.htaccess - SetEnv APPLICATION_ENV "development"

中有这一行

如果您还没有做类似的事情,请在public/index.php文件中添加以下内容:

/**
 * Set global ENV. Used for debugging
 */
if (isset($_SERVER['APPLICATION_ENV']) && $_SERVER["APPLICATION_ENV"] === 'development') {
    define("APP_ENV", 'development');
} else {
    define("APP_ENV", "production");
}

这将确保您的应用程序具有全局环境,该环境表明调试是打开还是关闭,它可以帮助您避免干扰代码。 现在从您的根文件夹打开config/application.config.php

<?php

$modules = [];

if (APP_ENV === 'development') {
    $modules[] = 'ZendDeveloperTools';
    $modules[] = 'BjyProfiler';
    $modules[] = 'SanSessionToolbar';
}

$modules[] = 'Application';
$modules[] = 'Admin';

return [
    // This should be an array of module namespaces used in the application.
    'modules' => $modules,

    // These are various options for the listeners attached to the ModuleManager
    'module_listener_options' => [
        // This should be an array of paths in which modules reside.
        // If a string key is provided, the listener will consider that a module
        // namespace, the value of that key the specific path to that module's
        // Module class.
        'module_paths' => [
            './module',
            './vendor',
        ],

        // An array of paths from which to glob configuration files after
        // modules are loaded. These effectively override configuration
        // provided by modules themselves. Paths may use GLOB_BRACE notation.
        'config_glob_paths' => [
            'config/autoload/{{,*.}global,{,*.}local}.php',
        ],

        // Whether or not to enable a configuration cache.
        // If enabled, the merged configuration will be cached and used in
        // subsequent requests.
        'config_cache_enabled' => (APP_ENV === 'production'),

        // The key used to create the configuration cache file name.
        'config_cache_key' => md5('app_config'),

        // Whether or not to enable a module class map cache.
        // If enabled, creates a module class map cache which will be used
        // by in future requests, to reduce the autoloading process.
        'module_map_cache_enabled' => (APP_ENV === 'production'),

        // The key used to create the class map cache file name.
        'module_map_cache_key' => md5('module_map'),
        // The path in which to cache merged configuration.
        'cache_dir' => dirname(__DIR__)."/data/cache",

        // Whether or not to enable modules dependency checking.
        // Enabled by default, prevents usage of modules that depend on other modules
        // that weren't loaded.
        'check_dependencies' => (APP_ENV !== 'production'),
    ],

    // Used to create an own service manager. May contain one or more child arrays.
    //'service_listener_options' => [
    //     [
    //         'service_manager' => $stringServiceManagerName,
    //         'config_key'      => $stringConfigKey,
    //         'interface'       => $stringOptionalInterface,
    //         'method'          => $stringRequiredMethodName,
    //     ],
    // )

   // Initial configuration with which to seed the ServiceManager.
   // Should be compatible with Zend\ServiceManager\Config.
   // 'service_manager' => [],
];
当您的应用投入生产时,

config_cache_*将激活并缓存您的路线配置。所有模块路由都将被缓存。此外,如果网站正在生产中,则不会加载调试模块。 您可以将md5('app_config')替换为您想要的任何内容。