DDAY2014 - Performance in Drupal 8

Post on 08-Jul-2015

318 views 0 download

description

Speaker: Luca Lusso Area: Development Probabilmente Drupal8 sarà la versione più performante di Drupal mai realizzata (o almeno questo è l'obiettivo). In questo talk vedremo alcune delle tecniche che sono state messe in campo per raggiungere questo risultato e vedremo come sfruttarle dentro ad un modulo custom.

Transcript of DDAY2014 - Performance in Drupal 8

PERFORMANCEIN DRUPAL 8

Web: / Twitter: / drupal.org: Luca Lusso @lussoluca lussoluca

AGENDAA che punto siamoUtenti o sviluppatori?Un nuovo approccio al cachingProfiling

A CHE PUNTO SIAMO

DRUPAL 8 BETA 3Le API sono stabili (a meno di qualche modifica urgenteper correggere issue critiche)Gli sviluppatori possono iniziare a portare i loro moduliQualcuno lo sta già usando in produzione

drupal.comwww.amazeelabs.comwww.sensiolabs.co.uklastcallmedia.com

DRUPAL 8 È PIÙ VELOCE DIDRUPAL 7?Beh... non ancora, ma:

UTENTI O SVILUPPATORI?

https://www.drupal.org/node/2226761Change all default settings and config to fast/safe

production values

IMPOSTAZIONI PREDEFINITECompressione css/js abilitataCache di Twig abilitataDebug di Twig disabilitatoCache Drupal abilitataNo jQuery caricato di default in ogni pagina

IMPOSTAZIONI PREDEFINITEConfigurazione ottimale per usare Drupal 8 in produzione,così gli utenti finali non devono sapere cosa configurare per

ottimizzare le performance del proprio sitoDurante lo sviluppo di un sito è bene che tutte queste

ottimizzazioni siano spente

Configurazione attivabile ambiente per ambiente e gestitada due file:settings.phpservices.yml

settings.phpif (file_exists(__DIR__ . '/settings.local.php')) { include __DIR__ . '/settings.local.php';}

settings.local.php$settings['container_yamls'][] = DRUPAL_ROOT . '/sites/development.services.yml';$config['system.logging']['error_level'] = 'verbose';$config['system.performance']['css']['preprocess'] = FALSE;$config['system.performance']['js']['preprocess'] = FALSE;$settings['cache']['bins']['render'] = 'cache.backend.null';$settings['extension_discovery_scan_tests'] = TRUE;$settings['rebuild_access'] = TRUE;

development.services.ymlservices: cache.backend.memory: class: Drupal\Core\Cache\MemoryBackendFactory cache.backend.null: class: Drupal\Core\Cache\NullBackendFactory

services.ymlparameters: twig.config: debug: true auto_reload: true cache: false factory.keyvalue: {} factory.keyvalue.expirable: {}

Debug Twig<!-- THEME DEBUG --><!-- THEME HOOK: 'page' --><!-- FILE NAME SUGGESTIONS: * page--front.html.twig * page--node.html.twig x page.html.twig--><!-- BEGIN OUTPUT from 'core/themes/bartik/templates/page.html.twig' --><div id="page-wrapper"><div id="page">

Devel Themer in core!

UN NUOVO APPROCCIO ALCACHING

RENDER CACHE IN DRUPAL 7Drupal 7 ha introdotto il caching dei render array

$build['content'] = array( '#theme' => 'weather', '#timestamp' => $timestamp, '#city' => $city, '#pre_render' => array('current_weather_cache_pre_render'), '#cache' => array( 'keys' => array('weather', $city), ),);[...]drupal_render($build);

RENDER CACHE IN DRUPAL 7Questa cache è difficile da invalidarePochi sanno che esiste e nessuno lo usa (neanche il Core,tranne che per i blocchi)

DRUPAL 8https://www.drupal.org/node/1534648

Cache tag support

https://www.drupal.org/node/2095167Entity render output is now cached by default

Drupal 7 invalida la cache delle pagine ad ogni salvataggio diun nodo, di un commento o di un utente!

node.pages.incfunction node_form_submit($form, &$form_state) {[...] // Clear the page and block caches. cache_clear_all();[...]

comment.modulefunction comment_form_submit($form, &$form_state) {[...] // Clear the block and page caches so that anonymous users see the comment // they have posted. cache_clear_all();[...]

user.pages.incfunction user_profile_form_submit($form, &$form_state) {[...] // Clear the page cache because pages can contain usernames and/or profile information: cache_clear_all();[...]

Questo vuol dire che ogni volta che viene inserito anche soloun commento tutta la cache dei blocchi e delle pagine viene

invalidata!In Drupal 7 è impossibile sapere a priori dove un

informazione verrà stampata, se metto in cache l'output diun nodo e successivamente l'autore di quel nodo cambianon ho modo di invalidare la cache di quel nodo specifico,

devo invalidare la cache di tutti i nodi!

CACHE TAGS IN DRUPAL 8Posso aggiungere dei tag agli item salvati in cache, i tag sono

stringhe$tags = array( 'node' => array(42), 'user' => array(314), 'taxonomy_term' => array(1337, 9001),);

cache($bin)->set($cid, $value, CACHE_PERMANENT, $tags);

Invalido tutti gli item nella cache che hanno un certo tagCache::invalidateTags(array('user:314'));

Elimino tutti gli item nella cache che hanno un certo tagCache::deleteTags(array('user:314'));

CACHE TAGS IN DRUPAL 8A questo punto se cambiamo il nome dell'utente 314 non èpiù necessario invalidare la cache di tutti i nodi, ma solo di

quelli che hanno user:314 come tag

ENTITÀTutte le entità (sia del Core che non) supportano questo

meccanismo, in automatico

ENTITÀcomment_listfilter_format:basic_htmlnode:1node_viewrenderedtaxonomy_term:1taxonomy_term:2taxonomy_term:3user:1user_view

TAGS NEI RENDER ARRAYreturn array( '#theme' => 'weather', // theme function '#data' => $weather, '#timestamp' => $timestamp, '#city' => $city, '#pre_render' => array('current_weather_cache_pre_render'), '#cache' => array( 'keys' => array('weather', $city), 'tags' => array("city:$city"), // cache tags ),);

city:Milanorendered

RENDER CACHELa funzione cache_clear_all() è stata rimossa in Drupal 8,

come invalido/elimino la cache delle pagine/blocchi?Cache::invalidateTags(array('rendered'));

RENDER CACHEResta il problema di personalizzare l'output per utenti

diversi o per ruoli diversi, ad esempio il marker "new" suicommenti non letti da un utente (se metto in cache per

utente perdo gran parte del vantaggio delle cache)

data-attribute generici nell'HTML + javascript perarricchire il DOM#post_render_cache

RENDER CACHE<div style="top: 32px;" role="form" class="contextual-render-processed contextual" data-contextual-id="node:node=1:changed=1415864183">

I link contestuali fanno parte del render di un nodo macambiano in base ai permessi che l'utente ha sul nodo

stesso. Invece di stampare l'elenco dei link direttamentenell'HTML (come fa Drupal 7) viene stampato un div con un

attributo "data-contextual-id" generico, una funzionejavascript e una chiamata ajax faranno il resto, aggiungendo

al div l'elenco di link specifico per l'utente.

PROFILING

PROFILINGPer poter risolvere i colli di bottiglia bisogna identificarli!

Code level profilingDrupal level profiling

CODE LEVEL PROFILINGOpen source

Commerciali

XHProfUprofiler

New RelicBlackfire.ioQafoo

CODE LEVEL PROFILING

CODE LEVEL PROFILING

DRUPAL LEVEL PROFILINGWebprofiler project

www.drupal.org/project/webprofiler

WEBPROFILER

WEBPROFILERAssetBlocksCacheConfigDatabaseExtension (Modules andThemes)EventsForm

MemoryPhpConfigRequestRoutingServicesStateTimelineUserViews

AUTOMATED PERFORMANCETRACKING

https://www.drupal.org/node/2308683Create a 'Profiling' install profile, for testbot-powered simple

profiling and easier local profiling

GRAZIE!Domande?