patterns, paradigms, and governance 2013-10-03

42
Scaling PHP Josh Levine [email protected] @heavypennies 1

Upload: josh-levine

Post on 20-May-2015

236 views

Category:

Technology


0 download

DESCRIPTION

Scaling PHP How patterns, paradigms, and governance can help you scale PHP performance and enable engineers contributing to your project

TRANSCRIPT

Page 1: Patterns, Paradigms, and Governance 2013-10-03

1

Scaling PHP

Josh Levine

[email protected]

@heavypennies

Page 2: Patterns, Paradigms, and Governance 2013-10-03

WHAT IS SHAPEWAYS?

2

Page 3: Patterns, Paradigms, and Governance 2013-10-03

Shapeways

3

http://www.shapeways.comhttp://developers.shapeways.comhttp://shapejs.shapeways.com

The worlds leading 3D printing marketplace and community

Page 4: Patterns, Paradigms, and Governance 2013-10-03

ARCHITECTURE

4

Page 5: Patterns, Paradigms, and Governance 2013-10-03

Architecture - System

5

Page 6: Patterns, Paradigms, and Governance 2013-10-03

Architecture - Framework

6

Road Runner

Page 7: Patterns, Paradigms, and Governance 2013-10-03

Architecture – Performance History

7

Page 8: Patterns, Paradigms, and Governance 2013-10-03

PATTERNSPARADIGMS

GOVERNANCE

8

Page 9: Patterns, Paradigms, and Governance 2013-10-03

Patterns, Paradigms, and Governance

9

• How do these concepts help us scale PHP ?• Will I see concrete actionable examples ?• Will you show me some hardcore PHP h4x ?

Page 10: Patterns, Paradigms, and Governance 2013-10-03

• Patterns – code• Paradigms – knowledge• Governance – process

10

Page 11: Patterns, Paradigms, and Governance 2013-10-03

PATTERNS

11

Programatically enforced goodness

Page 12: Patterns, Paradigms, and Governance 2013-10-03

Patterns – Traits of Goodness

12

• Accessibility• Repeatability• Clear Rationale

Page 13: Patterns, Paradigms, and Governance 2013-10-03

Patterns - Accessibility

13

• An accessible pattern is one that is intuitive, documented, and when invoked, reads like natural language

• Reading code that follows an accessible pattern is easier than reading code that does not follow

Page 14: Patterns, Paradigms, and Governance 2013-10-03

Patterns - Repeatability

14

• The repeatability of a pattern will ultimately decide its adoption

• If I can copy and paste, or use a scaffold for a pattern, the pattern is repeatable and will proliferate itself throughout the code

Page 15: Patterns, Paradigms, and Governance 2013-10-03

Patterns – Clear Rationale

15

• You need to be able to explain why a pattern was chosen

• All patterns have shortcomings – the *why* behind the pattern’s design and proliferation is critical in maintaining adherence

Page 16: Patterns, Paradigms, and Governance 2013-10-03

Pattern Examples: MVC

16

 abstract class RoadRunnerController {  $model = $this->getModel();  if($this->preHandle($model)) {    $this->handleRequest($model);    $this->postHandle($model); }  $this->finalize($model);}

Page 17: Patterns, Paradigms, and Governance 2013-10-03

Pattern Examples: Auto-wiring

17

 // Autowire the model managerpublic function preHandle($shapewaysModel, ModelManager $modelManager) { // do something with the ModelManager return parent::preHandle($shapewaysModel);}

I can use any Manager in our domain model by adding it to my function signature.

Page 18: Patterns, Paradigms, and Governance 2013-10-03

Pattern Examples: Auto-wiring

18

 public function invokeWithManagers($object, $function, $parameters){ $controllerClass = get_class($object); $function = new ReflectionMethod($controllerClass, $function); foreach ($function->getParameters() as $reflectionParameter) { $parameterClass = $reflectionParameter->getClass(); $parameterClassName = $parameterClass->getName(); // substr is much faster than pregmatch! if (substr($parameterClassName, -7) === "Manager") { $parameters[] = $this->instantiateManager($parameterClassName); } } return $function->invokeArgs($object, $parameters);}

Page 19: Patterns, Paradigms, and Governance 2013-10-03

PARADIGMS

19

Good things you need to know

Page 20: Patterns, Paradigms, and Governance 2013-10-03

20

Paradigms – Traits of Goodness

• Explicit• Consistent• Provide Clear Value

Page 21: Patterns, Paradigms, and Governance 2013-10-03

21

Paradigms – Explicit

A paradigm is explicit when the functionality exposed can be understood from the invoking side.

Explicit paradigms show visibly throughout a codebase.

Page 22: Patterns, Paradigms, and Governance 2013-10-03

22

Paradigms – Consistent

A paradigm is consistent when it is applied consistently and everywhere applicable in the code.  

If your paradigm is only consistent in 60% of your code base, chances are high that folks will copy and proliferate code that violates the paradigm.

Page 23: Patterns, Paradigms, and Governance 2013-10-03

23

Paradigms – Provide Clear Value

It is important for the entire team to understand and agree on the value of a paradigm.

Paradigms can easily feel like overhead - it is important to know *why* we choose to adhere to a paradigm.

Page 24: Patterns, Paradigms, and Governance 2013-10-03

Paradigm Examples - Constants

$myObj->callFunction( ',', true, false, false); 

Vs.

$myObj->callFunction( MyObj::DELIMITER_COMMA, MyObj::MATCH_MULTIPLE, MyObj::LOG_RESULTS,  MyObj::SKIP_DUPLICATES);

24

Page 25: Patterns, Paradigms, and Governance 2013-10-03

Paradigm Examples – More Constants

$_REQUEST["userId"]

Vs.

const REQUEST_USER_ID = "userId";…$_REQUEST[self::REQUEST_USER_ID];

25

Page 26: Patterns, Paradigms, and Governance 2013-10-03

Paradigm Examples - Consistent

• Code Style• Enum/Lookup Tables• Parameter marshaling

26

Page 27: Patterns, Paradigms, and Governance 2013-10-03

Paradigm Examples – More Constants

27

Page 28: Patterns, Paradigms, and Governance 2013-10-03

Paradigm Examples – One to Many

Making one query vs. two is often faster

SELECT umf.id… , mfma.model_file_material_asset_id… FROM udesign_model_file umf LEFT JOIN model_file_material_asset mfma ON mfma.model_file_id = umf.id

28

Page 29: Patterns, Paradigms, and Governance 2013-10-03

Paradigm Examples – One to Many

Generic function for mapping objects

/** * Select an array of objects using the given EntityMapper * * @param $db the database link to use (read-only/read-write/etc) * @param $sql the query * @param $mapper EntityMapper * @return array */public function selectObjects($db, $sql, $mapper){ $db = $this->getDbLink($db); $this->checkDBConnection($db); // Add backtrace information to the SQL as a comment // This helps immensely in slow query identification $sql = $this->prependURL($sql);

if ($resultSet = $this->runQuery($sql, $db)) { while ($row = mysql_fetch_assoc($resultSet)) { $mapper->mapRow($row); } mysql_free_result($resultSet); }

return $mapper->getResults();}

29

Page 30: Patterns, Paradigms, and Governance 2013-10-03

Paradigm Examples – One to Many

Mapping results follows a paradigm

public function mapRow($row){ if (!isset($this->rows[$row['id']])) {

$this->rows[$row['id']] = new ModelFileEntity( $row['id'] ); }

$modelFile = $this->rows[$row['id']];

if (isset($row['model_file_material_asset_id'])) { $modelFileMaterialAsset = new ModelFileMaterialAsset( $row['model_file_material_asset_id'] ); $modelFile->addModelFileMaterialAsset($modelFileMaterialAsset); }}

30

Page 31: Patterns, Paradigms, and Governance 2013-10-03

Paradigm Examples – Caching

Cache derived data

public function getPrice(){ if ($this->getPrice_cache !== NULL) return $this->getPrice_cache; // if override is set, use that if (($this->getPrice_cache = $this->getPriceOverride()) !== NULL) { return $this->getPrice_cache; } else { return ($this->getPrice_cache = $this->getPriceOriginal()); }}

31

Page 32: Patterns, Paradigms, and Governance 2013-10-03

Paradigm Examples – Caching

Give control to the engineers

public function getCurrencyById() { $cacheKey = 'currencyById-' . $currencyId . '-' . date(RoadRunnerDB::DATE_FORMAT_DAY); $currency = null; if(RoadRunnerLocalCache::get($cacheKey, $currency) === FALSE) { if (($currency = $this->memcacheGet($cacheKey, 0)) === FALSE) { $currency = $this->currencyDB->getcurrencyById($currencyId); $this->memcacheSet($cacheKey, $currency, 0); } RoadRunnerLocalCache::set($cacheKey, $currency); } return $currency;}

32

Page 33: Patterns, Paradigms, and Governance 2013-10-03

GOVERNANCE

33

For the good of all

Page 34: Patterns, Paradigms, and Governance 2013-10-03

Governance

Governance is the most difficult of all the goodness to achieve, and likely it is the most important for performance.

If you can find a problem before it is live, you are 10x more likely to fix it before it impacts your users and your bottom line.

34

Page 35: Patterns, Paradigms, and Governance 2013-10-03

Governance Example – DB Query Review

35

Page 36: Patterns, Paradigms, and Governance 2013-10-03

Governance Example – DB Query Review

36

Page 37: Patterns, Paradigms, and Governance 2013-10-03

Governance Example – DB Query Review

37

Page 38: Patterns, Paradigms, and Governance 2013-10-03

Governance Example – Tracking

38

Page 39: Patterns, Paradigms, and Governance 2013-10-03

Governance Example – Tracking

watch -n1 "cat rps_uri.sql | mysql tracking"

39

Page 40: Patterns, Paradigms, and Governance 2013-10-03

Governance Example – Tracking

40

Page 41: Patterns, Paradigms, and Governance 2013-10-03

Governance Example – Quality

41

Page 42: Patterns, Paradigms, and Governance 2013-10-03

THANK YOU

42