DDAY2014 - Performance in Drupal 8

39
PERFORMANCE IN DRUPAL 8 Web: / Twitter: / drupal.org: Luca Lusso @lussoluca lussoluca

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

Page 1: DDAY2014 - Performance in Drupal 8

PERFORMANCEIN DRUPAL 8

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

Page 2: DDAY2014 - Performance in Drupal 8

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

Page 3: DDAY2014 - Performance in Drupal 8

A CHE PUNTO SIAMO

Page 4: DDAY2014 - Performance in Drupal 8

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

Page 5: DDAY2014 - Performance in Drupal 8

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

Page 6: DDAY2014 - Performance in Drupal 8

UTENTI O SVILUPPATORI?

Page 7: DDAY2014 - Performance in Drupal 8

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

production values

Page 8: DDAY2014 - Performance in Drupal 8

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

Page 9: DDAY2014 - Performance in Drupal 8

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

Page 10: DDAY2014 - Performance in Drupal 8

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

Page 11: DDAY2014 - Performance in Drupal 8

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

Page 12: DDAY2014 - Performance in Drupal 8

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

Page 13: DDAY2014 - Performance in Drupal 8

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!

Page 14: DDAY2014 - Performance in Drupal 8

UN NUOVO APPROCCIO ALCACHING

Page 15: DDAY2014 - Performance in Drupal 8

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);

Page 16: DDAY2014 - Performance in Drupal 8

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

Page 17: DDAY2014 - Performance in Drupal 8

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

Cache tag support

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

Page 18: DDAY2014 - Performance in Drupal 8

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();[...]

Page 19: DDAY2014 - Performance in Drupal 8

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!

Page 20: DDAY2014 - Performance in Drupal 8

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'));

Page 21: DDAY2014 - Performance in Drupal 8

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

Page 22: DDAY2014 - Performance in Drupal 8

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

meccanismo, in automatico

Page 23: DDAY2014 - Performance in Drupal 8

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

Page 24: DDAY2014 - Performance in Drupal 8

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

Page 25: DDAY2014 - Performance in Drupal 8

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

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

Page 26: DDAY2014 - Performance in Drupal 8

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

Page 27: DDAY2014 - Performance in Drupal 8

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.

Page 28: DDAY2014 - Performance in Drupal 8

PROFILING

Page 29: DDAY2014 - Performance in Drupal 8

PROFILINGPer poter risolvere i colli di bottiglia bisogna identificarli!

Code level profilingDrupal level profiling

Page 30: DDAY2014 - Performance in Drupal 8

CODE LEVEL PROFILINGOpen source

Commerciali

XHProfUprofiler

New RelicBlackfire.ioQafoo

Page 31: DDAY2014 - Performance in Drupal 8

CODE LEVEL PROFILING

Page 32: DDAY2014 - Performance in Drupal 8

CODE LEVEL PROFILING

Page 33: DDAY2014 - Performance in Drupal 8

DRUPAL LEVEL PROFILINGWebprofiler project

www.drupal.org/project/webprofiler

Page 34: DDAY2014 - Performance in Drupal 8

WEBPROFILER

Page 35: DDAY2014 - Performance in Drupal 8

WEBPROFILERAssetBlocksCacheConfigDatabaseExtension (Modules andThemes)EventsForm

MemoryPhpConfigRequestRoutingServicesStateTimelineUserViews

Page 36: DDAY2014 - Performance in Drupal 8

AUTOMATED PERFORMANCETRACKING

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

profiling and easier local profiling

Page 38: DDAY2014 - Performance in Drupal 8

GRAZIE!Domande?

Page 39: DDAY2014 - Performance in Drupal 8