1 of 46

Les outils avancés du développeur PHP

Patrick Allaert

PHP Tour Lille 2011

http://tinyurl.com/OutilsAvancesPHP

2 of 46

À propos de moi

  • Indépendant et fondateur de Libereco
  • Expert PHP/Linux
  • Développeur d'eZ Publish
  • Auteur de l'extension PHP APM
  • @patrick_allaert
  • patrickallaert@php.net
  • http://github.com/patrickallaert/
  • http://patrickallaert.blogspot.com/

3 of 46

Les outils présentés

  1. MySQL Proxy
  2. Wireshark
  3. Analyse statique avec HipHop
  4. Naviguer dans votre code
  5. Bisecting
  6. Intercepter les appels systèmes effectués
  7. Le refactoring en ligne de commande
  8. Les modules PECL incontournables

4 of 46

MySQL Proxy

5 of 46

MySQL Proxy

Communication client/serveur

SELECT * FROM tab1 WHERE col1 = 42;

6 of 46

MySQL Proxy

Communication client/proxy/serveur

SELECT * FROM tab1 WHERE col1 = 42;

7 of 46

MySQL Proxy

  • Répartition de charge
  • Basculement (failover)
  • Analyses de requêtes
  • Filtrage et modification de requêtes
  • Logging
  • Nouvelles fonctionnalités

8 of 46

Démo MySQL Proxy

9 of 46

MySQL Proxy

Chainage

SELECT * FROM tab1 WHERE col1 = 42;

10 of 46

Wireshark

11 of 46

Wireshark

Logiciel libre d'analyse de protocol/packet sniffer

Anciennement: Ethereal

12 of 46

Wireshark

eth0: 192.168.0.10

lo: 127.0.0.1

13 of 46

Wireshark

eth0: 192.168.0.10

lo: 127.0.0.1

14 of 46

Wireshark

  1. Configuration de la capture

Interface capturée

Filtre de capture

15 of 46

Wireshark

  • Capture

16 of 46

Wireshark

  • Filtrage / inspection / statistiques / sauvegarde / ...

17 of 46

Démo Wireshark

18 of 46

HipHop for PHP

19 of 46

HipHop for PHP

Compilateur de code PHP:

Transforme le code source PHP en C++ pour ensuite être compilé avec g++.

20 of 46

HipHop for PHP

Compilateur de code PHP:

Transforme le code source PHP en C++ pour ensuite être compilé avec g++.

Langage dynamiquement typé

21 of 46

HipHop for PHP

Compilateur de code PHP:

Transforme le code source PHP en C++ pour ensuite être compilé avec g++.

Langage dynamiquement typé

Langage fortement typé

22 of 46

HipHop for PHP

Compilateur de code PHP:

Transforme le code source PHP en C++ pour ensuite être compilé avec g++.

Langage dynamiquement typé

Langage fortement typé

Compilateur strict

23 of 46

24 of 46

HipHop for PHP

Lancer l'analyse:

$ hphp -t analyze --input-dir .

ou pour plus de contrôle:

$ find -name \*.php > filelist.txt

$ vi filelist.txt

$ hphp -t analyze --input-list filelist.txt

25 of 46

HipHop for PHP

running hphp...

creating temporary directory /tmp/hphp_0CQu7i ...

parsing inputs...

parsing inputs took 0'01" (1250 ms) wall time

pre-optimizing...

pre-optimizing took 0'00" (317 ms) wall time

inferring types...

inferring types took 0'00" (835 ms) wall time

post-optimizing...

post-optimizing took 0'00" (271 ms) wall time

saving code errors and stats...

saving stats...

saving stats took 0'00" (71 ms) wall time

all files saved in /tmp/hphp_0CQu7i ...

running hphp took 0'03" (3170 ms) wall time

26 of 46

HipHop for PHP

$ CodeErrorFormatter.php /path/to/CodeError.js

PHPIncludeFileNotFound

================================================================================

/home/patrick.allaert/Projets/ezpublish/autoload.php

26:5 -> 26:31 details: require 'Base/src/base.php'

36:21 -> 36:47 details: include 'ezc/Base/base.php

UseUndeclaredVariable

================================================================================

/home/patrick.allaert/Projets/ezpublish/lib/ezpdf/classes/class.ezpdftable.php

317:128 -> 317:135 details: $colName

/home/patrick.allaert/Projets/ezpublish/kernel/setup/steps/ezstep_site_types.php

736:39 -> 736:51 details: $php_errormsg

TooManyArgument

================================================================================

/home/patrick.allaert/Projets/ezpublish/lib/ezpdf/classes/class.pdf.php

3318:91 details: $this->addText($x, $y, $size, $text, $angle, $adjust, $angle)

27 of 46

Naviguer dans votre code

28 of 46

OpenGrok

29 of 46

OpenGrok

Outil permettant de parcourir du code source qu'il aura préalablement indexé.

Exemple pour le code source du langage PHP:

http://lxr.php.net/

30 of 46

Démo OpenGrok

31 of 46

GNU Global

32 of 46

GNU Global

Indexer le projet courant:

$ gtags

Rechercher la définition d'un symbole:

$ global -x symbole

Rechercher les utilisations d'un symbole:

$ global -rx symbole

Global façon bash:

$ globash

33 of 46

Bisecting

34 of 46

Bisecting (git)

Permet de découvrir le moment où une régression a été introduite.

$ git bisect start

$ git bisect bad

$ git checkout <une-bonne-revision>

$ git bisect good

# jusqu'à la découverte du mauvais commit:

$ <tester>

$ git bisect (bad|good)

35 of 46

Bisecting (git)

Ou de manière automatique... :)

$ git bisect start

$ git bisect bad

$ git checkout <une-bonne-revision>

$ git bisect good

$ git bisect run <test-script>

36 of 46

Intercepter les appels systèmes effectués

37 of 46

Intercepter les appels systèmes effectués

strace

$ strace -p <pid>

$ strace <program>

-e <expr>: filtre le type d'appels systèmes

-T: affichage du temps pris pour chaque appel système

-c: résume en fin d'exécution des statistiques

38 of 46

Démo strace

39 of 46

Le refactoring en ligne de commande

40 of 46

Le refactoring en ligne de commande

sed (Stream EDitor) a bientôt 30 ans!

Modifier "copyright 1999-2011" par "copyright 1999-2012":

$ sed -i "s/copyright 1999-2011/copyright 1999-2012/g" fichier1 [fichier2] ... [fichiern]

Ou encore, changer le nom de la classe TrucBrol en MachinBidule:

$ sed -i "s/TrucBrol/MachinBidule/g" $(global -r TrucBrol)

41 of 46

Le refactoring en ligne de commande

codemod: Projet Facebook de modification de code en masse

https://github.com/facebook/codemod

Modifier "copyright 1999-2011" par "copyright 1999-2012":

$ codemod.py --extensions php -d . "copyright 1999-2011" "copyright 1999-2012"

42 of 46

Démo codemod

43 of 46

Les modules PECL incontournables

44 of 46

Les modules PECL incontournables

débogage: Xdebug, scream, inclued

profilage: Xhprof, Pinba, Xdebug

monitoring: APM, Pinba, memtrack

callback: funcall, intercept

Environnements: Production, Développement

45 of 46

Questions ?

46 of 46

Merci :)