首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >为什么我不能在Zend 2中使用ClassMapAutoloader获得性能呢?

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

Stack Overflow用户
提问于 2015-08-18 14:55:45
回答 1查看 362关注 0票数 1

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

我们准备了一个舞台环境,我现在做一些性能调优。我读到,使用ClassMapAutoloaderStandardAutoLoader快得多,比如http://samminds.com/2012/11/zf2-performance-quicktipp-2-classmap-autoloading/。我确实理解为什么它应该更快,但在我的例子中,我使用ClassMapAutoloader分析( WinCacheGring/QCacheGrind )和慢一些的(约0.2%)描述了这个站点。

有人知道为什么会慢一点吗?

我使用的是CentOS和=> 5.6.12

编辑添加的信息:

一个autoload_classmap.php的例子:

代码语言:javascript
复制
    <?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',
);

从相关的Module.php中提取:

代码语言:javascript
复制
public function getAutoloaderConfig() {
    return [
        'Zend\Loader\ClassMapAutoloader' => [
            __DIR__ . '/autoload_classmap.php'
        ],
        'Zend\Loader\StandardAutoloader' => [
            'namespaces' => [
                __NAMESPACE__ => __DIR__ . '/src/' . str_replace('\\', '/', __NAMESPACE__)
            ]
        ]
    ];
}

我已经检查过Autoloader是否使用了类映射。

编辑添加的信息:

EN

回答 1

Stack Overflow用户

发布于 2015-08-23 05:43:15

很抱歉你的回答太迟了。现在,您的应用程序加载半秒- 0.6s准确地说。所以我这个自愿者正在做他的工作。您的查询也执行得很快。我还可以想到两种方法来加速您的应用程序。

  • 第一种方法-使用模板映射

找到您的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的文件结构。

代码语言:javascript
复制
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 =新的ViewModel();$view->setTemplate("aplication/index/index");返回$view;}

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

假设您的.htaccess - SetEnv APPLICATION_ENV "development"中有这一行

在您的public/index.php文件中,如果您没有执行类似的操作,请添加以下内容:

代码语言:javascript
复制
/**
 * Set global ENV. Used for debugging
 */
if (isset($_SERVER['APPLICATION_ENV']) && $_SERVER["APPLICATION_ENV"] === 'development') {
    define("APP_ENV", 'development');
} else {
    define("APP_ENV", "production");
}

这将确保您在整个应用程序中都有一个全局env,即调试是打开还是关闭,它帮助您避免编写枯燥的代码。现在从根目录中打开config/application.config.php

代码语言:javascript
复制
<?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')

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/32075876

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档