14. Our code generates business value when it runs,
not when we write code.
@coda
15. Store log and error data
Query on past events
Locate the bug
Know Servers/application
status: healthy/resources/
Responding as it should?
Face the reality of my
ecosystem
Understand why and
where the app. is slow
Analyze the behaviour
when it ‘s running
watch data
be alerted
fast website
Needs
data
…
BUSINESS
VALUE
18. It's not about the accuracy of estimating it's
about facing the reality of uncertainty.
@jimhighsmith
19. metrics
http://samate.nist.gov/index.php/Metrics_and_Measures.html
http://nvlpubs.nist.gov/nistpubs/jres/104/6/j46gra.pdf
The IEEE Standard Glossary of Software Engineering Terminology
also defines quality metric as :
(1) A quantitative measure of the degree to which
an item possesses a given quality attribute.
(2) A function whose inputs are software data and whose output is a
single numerical value that can be interpreted as the degree to which
the software possesses a given quality attribute.
* @param array $data
* @return float
function calculateUglyness(array $data);Ninja territory
25. StatsD simple
— Node.JS daemon. Listens for message over UDP
simple daemon for easy stats aggregation.
Graphite rude <key> <numeric value> <timestamp>
Graphite stores numeric time-series data.
— Real-time graphing system
— Components:
— Carbon stores the data in Graphite’s specialized database.
— The data can then be visualized through graphite
web interface developed with django.
— Whisper allows carbon to write multiple data points.
metrics
26. Symfony2 integration:
there’s a bundle for it ‘liuggio/StatsDClientBundle’
— Amount of visitor
— Logged vs Anonymous user
— Doctrine collector
— Amount of PHP peak
— Execution Time
— Monolog
— Easily add a new metric:
$this->get(‘stats_d_client.factory')->increment(’upload.file');
metrics
29. GAUGES are arbitrary values, which can be recorded.
metrics
$factory = $this->get('stats_d_client.factory');
$data[] = $factory->gauge('cart.item.number', 30);
$data[] = $factory->gauge('cart.item.number', 34);
$data[] = $factory->gauge('cart.item.number', 38);
$this->get('stats_d_client.service’)->send($data);
cart.item.number|38
Result is the last one (per flush time)
30. COUNTS increment and decrement a value.
metrics
$factory = $this->get('stats_d_client.factory');
$data[] = $factory->increment('user.visitor');
$this->get('stats_d_client.service’)->send($data);
The value stay in memory until the flush interval.
34. you have more
data than you think
and you need less
data than you think
MORELESS
35. new metrics
— Why do you want to know?
— How much do you know now?
— What is the value to additional
information
36. The time you spend for metrics has a cost,
and usually is more economical
than many managers assume.
maybe because
they didn’t
measure.
bazinga
metrics
42. Great caching is like great sex.
It hides all the real problems
@vivekhaldar
43. Wikipedia: “a Cache ( /’kæ∫/ KASH[1]) is a
component that transparently stores data
so that future requests for that data can be
served faster.”
the cache miss
happens once for the
same response.
given the same request the Cache miss happens once.
given the same request the response is not ‘processed’ twice.
A Cache Hit happens if the Cache
contains the answer.
A Cache Miss happens if the Cache
doesn’t contain the answer.
The perfect cache when
45. — Application caching: app/cache with opcode
— Response caching and HTTP-CACHE
— Doctrine2 Query and Result Caching
— CDN and static files
— Other
CACHE + SF2
46. Terravison vendor folder is about 340 MB
The app/cache/prod folder is about 3000 files
1. Annotation (Entity/Controller/...)
2. Template
a. name => file
b. twig in php
3. Assetic (definition)
4. Url matcher, Url creator
5. Translations array key=>value
6. Doctrine Entity Proxy
./app/cache
47. Add your class to the services with the tag kernel.cache_warmer
Note: the CacheWarmerInterface is not properly the same as the snippet.
cache warmer
namespace SymfonyComponentHttpKernelCacheWarmer;
interface CacheWarmerInterface
/**
* Warms up the cache.
* @param string $cacheDir The cache directory
*/
public function warmUp($cacheDir);
public function isOptional()
48. Symfony2 is a web framework built from scratch around
the http specification
— a resource could be Fresh || Stale
— http-caching only on safe method
— validation || expiration
HTTP
50. HTTP headers
$response->setPublic() //default is private
$response->setMaxAge(600);
// Same as above but only for shared caches
$response->setSharedMaxAge(600);
$response->setETag(md5($response->getContent()));
$response->setLastModified($datetime);
if ($response->isNotModified($this->getRequest()))
...
$response->setVary(‘Accept-Encoding’);
symfony.com/doc/master/book/http_cache.html
52. DoctrineCommonCacheCache
(There’s not tagging)
doctrine cache
// Fetches an entry from the cache.
string function fetch($id);
// Tests if an entry exists in the cache.
bool function contains($id);
// Put data into the cache.
bool function save($id, $data, $lifeTime);
// Deletes a cache entry.
bool function delete($id);
53. MetaData Cache Annotation (OPCODE)
Query Cache DQL parsed into SQL
(OPCODE/REDIS/MEMCACHED)
Result Cache The query result
(REDIS/MEMCACHED/OPCODE)
doctrine caches
55. There are only two hard things in Computer Science
cache invalidation and naming things
Phil Karlton
There are only two hard things in Computer Science
cache invalidation, naming things and off-by-1
errors.
anonymous
56. Invalidation
Especially for http, but in general, you should not waste time to
invalidate a value, but you should invest resources to find the right
time of validity.
Cache miss storm
When a resource is no longer available (cache miss), and is required
by many clients simultaneously (storm), a congestion happens.
cache miss storm, cache stampede,
dog-piling, APC Cache slam
cache
57. Pre-Caching/Cacheback
Sometimes you need to have some data already available and you
don’t want that the first request wait.
Uniqueness of the cache key
Attention to the conflicts in the key definition, always put a prefix or
namespace such as language or role.
Http-Microcaching:
Save all the responses for 1||2 seconds
lot of examples on internet for Nginx
cache
58. and _ _ _
will move your
to the next level.
???
61. — The user should never wait
slow operation data in a queue (Redis/*MQ)
processed by a consumer (cli + supersivord):
— Image Manipulation
— File Creation (pdf)
— Sending email as spool
— I/O operation
— Use Session with Redis/Memcache
— Caution on what do you do in the loop && nested loop
easy …
62. … tips
— Must! OPCODE: APC using the apc.stat = 0
— $php composer.phar dump-autoload –optimize
— Twig.c php extension
— Assets
— Minify and combine all of your CSS and JS files
— Run all (or just some) of your CSS or JS files
through some sort of compiler,
such as LESS, SASS or CoffeeScript
— Run image optimizations on your images
http://symfony.com/doc/current/book/performance.html
https://speakerdeck.com/alexandresalome/symfony2-and-performances
63. the DIC of sf2.3 is lazy
A lazy loading value holder proxy is a virtual proxy that wraps and
lazily initializes a "real" instance of the proxied class.
<service id="foo" class="AcmeFoo" lazy="true" />
When do I use a lazy value holder?
-Your object takes a lot of time and memory to be initialized (with all
dependencies)
-Your object is not always used, and the instantiation overhead can be
avoided
github.com/Ocramius/ProxyManager/blob/master/docs/lazy-loading-value-holder.md
github.com/Ocramius/symfony-docs/blob/master/components/dependency_injection/lazy_services.rst
70. $article = new Article();
$article->setHeadline('I love PHPday');
$em->persist($article); // the entity is managed
$em->flush(); // the entity is persisted
...
$article->setHeadline('I love JSday and PHPday');
$em->flush(); // the Headline is updated
on the persistence layer
$em->persist($article);
71. $article = $em->find('Article', 1234);
$article->setHeadline(’I love GRUSP');
$article2 = $em->find('Article', 1234);
echo $article2->getHeadline(); // I love GRUSP
if ($article === $article2) ---> true!!
73. Identity Map
The flush has a cost
— Number of managed Entities
— Tracking policy
— Type of entities
https://doctrine-orm.readthedocs.org/en/latest/reference/unitofwork.html
contains all the managed entities
On flush makes a property by property comparison for all the
entities managed.
74. tracking policy
Doctrine definition:
“is the process of determining what has changed in managed entities
since the last time they were synchronized with the database.”
— implicit tracking (default and simple)
— explicit tracking (suggested for lot of entities managed)
— notify tracking
/**
* @Entity
* @ChangeTrackingPolicy("DEFERRED_EXPLICIT”)
http://docs.doctrine-project.org/en/latest/reference/change-tracking-policies.html
Flush optimization #1
75. tracking policy
Deferred Implicit
UOW = DB
http://docs.doctrine-project.org/en/latest/reference/change-tracking-policies.html
Deferred Explicit
UOW != DB
— UOW = ’I love PUG Roma’
— DB is not updated.
Need the persist!
Flush optimization #1
// $article is already managed or loaded
// we are changing the Headline
$article->setHeadline('I love PUG Roma');
$em->flush();
echo $article->getHeadline();// is ‘I love PUG Roma’
76. Read only entity
/**
* @Entity
* @READONLY
*/
Class Status {
$em->getUnitOfWork->markReadOnly($entity);
Temporarily mark entities as read only
Flush optimization #2
77. Flush only on few
entities
$em->flush($entity)
Flush optimization #3
78. Reducing the
flush cost
The flush has a cost
— Number of managed Entities (flushing only some)
— Tracking policy (explicit || implicit)
— Type of entities (read-only || temp. read-only)
Recap.
80. lazy associations
Association are LAZY by default
// Order
/**
* @ManyToOne(targetEntity=”Cart”, cascade={“all”},
fetch=”EAGER”)
*/
private $cart
<?php
$query = $em->createQuery(“SELECT o FROM Order o”);
$query->setFetchMode(“Order”, “Cart”, “LAZY”);
$query->execute();
Change the fetch mode
86. Knowing when optimization is premature
defines the differences the master engineer
and the apprentice
Theo Schlossnagle
Optimization
We’ve heard
this a million
times
87. When you discover the real problem, you done the half.
Problem are not all equals, and most are illusions.
Pieter Hintjens
OK the real title is zzz and blblabla will move your something to the next level
This is a not invented here talk, The topics I ‘ d like to discuss are all well known, I have not invented anything please go there to read the references such as the talk of Jordi etc. I’m here to share my knowledge SEE THE REFERENCES
I love the Symfony2 framework, G ave us: a breath of FRESHNESS to the whole php world , GREAT DOCUMENTATION/ NOT FRAMEWORK but BESTPRACTICES , great community / METHODOLOGIES gave as a real dark passenger, dark passenger means that there ’ s a little genious/geek that is on your shoulder when you develop B ut this dark passengere LOOK AT YOU not at the code at YOU M y dark passenger speaks FRANCE You ’ ave not Follow^ng coding standards, dependency injection. When I met this small dark passenger he grows up, I feel better my libraries are more usable. I realized that bigger is your dark passenger and the more you are a cool developer. S o at terravision we do love sf2 and it ’ s fabpot passenger.
OK the real title is zzz and blblabla will move your something to the next level
OK the real title is zzz and blblabla will move your something to the next level
We are developer our ecosystem create a lot of data. [ok hey liuggio what problem do you have with data?] -------- data is difficult to measure, difficult to read, data is time consuming. ------- data is boring,-- data sucks * (sometimes) Logging ---------- If you think that logging is a file with a lot of unreadable information. Logging will have a Matrix effect with data. You don’t’need logging only when something bad happen How many of you use logging, How many of you thinks that logging is a file with too much iformation in it… Logging is a black hole in the developers career,
[ok hey liuggio what problem do you have with data?] data is difficult to measure, difficult to read, data is time consuming. ------- data is boring,-- data sucks * (sometimes)
what is data? different information from ----- Note riunione (5/15/13 08:26) ----- data gathers
what is data? different information from ----- Note riunione (5/15/13 08:26) ----- data gathers
I do we do, we used to ignore the most of data that we receive. But sometimes we track it, maybe passively but we keep track of some events for example logging
What do you do? 1. I ignore them 2. I ignore them but I have a clear coscience (because there ’ s a file for it) 3. I ignore them mmm but I ’ ll use regex when an halling customer will call (if you have a problem that you want to solve with the regex, you have 2 problem) I f you try to solve a problem with a regular expression, you have 2 problems. 4. I ignore the log, regex and the customer. S ometimes you need to investigate, A lways you need to be alerted when .. Y ou want to see graphes In the daylife is very important have PULL information like a big lcd monitor over there
Why should we use all that data? What is business value: all the things that produces money W e are coder, we love write code, but our code produce business, W e use unit testing in order to have maintainability of the code, this could be a business value we use function testing, because we want to add new features easily this could be a business value A nd we want that if angry customer will call you we have already the answer Y ou want a fast user experience Y ou want to know what is happening in your application --------- code is not a trophy functionality measure it in order to know face the reality measure on production
If you don’t understand this slide you are not stupid maybe because you never seen the ‘ hitchhiker's guide to the galaxy ’ movie. Hitchhikers
We use phpunit in order to easily add new feature and have code manintainability not because is cool ----- Note riunione (5/16/13 17:55) ----- each company has it's own business value.
All of this are some business value that you could achieve tracking data. Business Value is everything that produces € ( For example Unit testing produce business value (we do use unit testing in order to have maintainability of the code (easy to add a new feature) that is a business value.) Each company has its own definition of business Value, but we could define some needs that could increment your Business Value. (could because if you don ’ t care it ’ s ok) Store log and error data (investigate) Complex query on past events Easily Locate the bugs Know Servers status: healthy/resources Know Application status: Responding as it should? Know what is happening in my ecosystem Understand why and where the app. is slow Analyze the behaviour when it ‘ s running I want to watch data I want to be alerted Fast website I want to know how if after a certain event “ how much ” something else changed Interpolate data from different sources (after the deploy, how many t-shirt were bought?) We added to many information here Investigate means that we need to collect and create relations among different logs and make queries Be alerted means that we want on different context an action Visualize means meseaure and draw from the data Noi creiamo cose e dobbiamo interessarci del business value, il nostro codice produce business value add new features easily business value -> all the things that produces €€ code is not a trophy functionality T here ’ s a name for everything … measure it in order to know face the reality measure on production ----- Note riunione (5/14/13 00:11) ----- satisfy ----- Note riunione (5/16/13 17:55) ----- this is business value from the developer side and data
In order to do this we need to add different actions ----- Note riunione (5/15/13 08:50) ----- perform actions
In order to do this we need to add different actions ----- Note riunione (5/16/13 11:20) ----- in here there's the talk title: from metrics and optimizing you obtain a business value
I’d like to talk with you about Metrics Measure: WE WANT TO FACE THE REALITY The terms metric and measure have some overlap. We use measure for more concrete or objective attributes and metric for more abstract, higher-level, or somewhat subjective attributes. For instance, lines of code (LOC) is a measure: it is objective and concrete The robustness is a metrics M easurment are the basis for the metrics Measures help us approximate less tangible metrics.
Metrics is a link of two worlds technical and business vision of IT. [yes ok but what is?] ------ definition of metrics … sucks is so general and everybody has their own, ----- Metrics -- definition metrics gives you qualitatively information (and measure quantity information) ----- Note riunione (5/15/13 09:15) ----- line of code examples measure, metrics
A quantitatively expressed reduction of uncertainty based on one or more observations. Think a measure as a quantitative number (image you could measure anything) This “ uncertainty reduction ” point of view is what is critical to busi- ness reduction can be worth millions. HOW TO MEASURE ANYTHING Hubbard measurement process of experimentally obtaining one or more quantity values that can reasonably be attributed to a quantity
When you don’t know something you could ask to them. What metrics is
Metrics number visual ops it’s a ninja territory?
Why metrics is so important? Is a join of two worlds
Metrics in order to produce Graphs → Decision decision because you could Collects and aggregates from different sources sources of metrics: webservers, application, cache server, averything! Better decision using numbers -- @coda
How to do metrics? StatsD We want a great tool in order to add easily new metrics. From every envoirnment, every language every where.
Calculate the amout of php ram during it’s peak time Ready for the WTF effect? This is a “timer”
Each stat is in its own " bucket ” the KEY, They are not predefined anywhere. Buckets can be named anything that will translate to Graphite (periods make folders, etc) After the flush interval timeout (defined by config.flushInterval, default 10 seconds), stats are aggregated and sent to an upstream backend service.
A gauge simply indicates an arbitrary value at a point in time and is the most simple type in StatsD. It just takes any number and ships it to the backend.
Counters are simple. It adds a value to a bucket and stays in memory until the flush interval.
Timers collects numbers. They does not necessarily need to contain a value of time. You can collect bytes read, number of objects in some storage, or anything that is a number. Useful for: mean/upper/sum/lower/count Operation
Timers collects numbers. They does not necessarily need to contain a value of time. You can collect bytes read, number of objects in some storage, or anything that is a number. Useful for: mean/upper/sum/lower/count Operation
Here is a golden rule that you find in all talks about metrics I prefer metrics CAN … Because too much not usefull information could go to CONFUSION. == with Metrics you track everythings that moves [on internet this is like a cool definition] BUT
Se teniamo sempre alto il livello di razionalità questo ci farà dire:
È molto
Se teniamo sempre alto il livello di razionalità questo ci farà dire:
Better decision using numbers -- @coda Good and Fast decisions help the business
Better decision using numbers -- @coda Good and Fast decisions help the business
OK the real title is zzz and blblabla will move your something to the next level
Another topic that has a lot of usages is the cache What do you think if I say cache?
Another topic that has a lot of usages is the cache What do you think if I say cache?
Another topic that has a lot of usages is the cache What do you think if I say cache?
----- Note riunione (5/11/13 21:43) ----- proxy
This is a great idea, We are web developer So we need to follow the web protocol Why I didn’t think about it before?
----- Note riunione (5/14/13 00:11) ----- scrivere nei commenti fig!!
Another topic that has a lot of usages is the cache What do you think if I say cache?
OK the real title is zzz and blblabla will move your something to the next level
Y ou could do by yourself This code is problematic, and adds a lot of complexity that makes your unit tests' code even worse. https://github.com/Ocramius/ProxyManager/blob/master/docs/lazy-loading-value-holder.md https://github.com/Ocramius/symfony-docs/blob/931091d8e3aa0b9d4f1393d313388d5ee1a5d47d/components/dependency_injection/lazy_services.rst
flush multiples entities $em->flush(array($entityA, $entityB); paginator master-slave Flush to Master Read from Slave join to interface (Target Entity Resolver: /** @ORM\\ManyToOne(targetEntity="Tvision\\Bundle\\OrderBundle\\Entity\\StatusInterface") **/
flush multiples entities $em->flush(array($entityA, $entityB); paginator master-slave Flush to Master Read from Slave join to interface (Target Entity Resolver: /** @ORM\\ManyToOne(targetEntity="Tvision\\Bundle\\OrderBundle\\Entity\\StatusInterface") **/
The deferred implicit policy is the default change tracking policy and the most convenient one.property-by-property comparison at commit time and also detects changes to entities or new entities that are referenced by other managed entities ( “ persistence by reachability ” ) Explicit only considers entities that have been explicitly marked for change detection through a call to EntityManager#persist(entity) or through a save cascade.
Starting from the daylife in order to undestand UOW ----- Note riunione (5/16/13 11:41) ----- there's a line that could'nt be there
T he unit of work class in doctrine ha 3206 lines, is complex UOW start with with init of EM or after a flush. The size of a Unit of Work mainly refers to the number of managed entities Is an Entity managed by UOW when is persisted or when is fetched Identity Map: In this case the Article is accessed from the entity manager twice, but modified in between. How costly a flush operation is, mainly depends on two factors: The size of the EntityManager ’ s current UnitOfWork. The configured change tracking policies You can get the size of a UnitOfWork as follows: <?php $uowSize = $em->getUnitOfWork()->size();
T he unit of work class in doctrine ha 3206 lines, is complex UOW start with with init of EM or after a flush. The size of a Unit of Work mainly refers to the number of managed entities Is an Entity managed by UOW when is persisted or when is fetched Identity Map: In this case the Article is accessed from the entity manager twice, but modified in between. How costly a flush operation is, mainly depends on two factors: The size of the EntityManager ’ s current UnitOfWork. The configured change tracking policies You can get the size of a UnitOfWork as follows: <?php $uowSize = $em->getUnitOfWork()->size();
T he unit of work class in doctrine ha 3206 lines, is complex UOW start with with init of EM or after a flush. The size of a Unit of Work mainly refers to the number of managed entities Is an Entity managed by UOW when is persisted or when is fetched Identity Map: In this case the Article is accessed from the entity manager twice, but modified in between. How costly a flush operation is, mainly depends on two factors: The size of the EntityManager ’ s current UnitOfWork. The configured change tracking policies You can get the size of a UnitOfWork as follows: <?php $uowSize = $em->getUnitOfWork()->size();
The deferred implicit policy is the default change tracking policy and the most convenient one.property-by-property comparison at commit time and also detects changes to entities or new entities that are referenced by other managed entities ( “ persistence by reachability ” ) Explicit only considers entities that have been explicitly marked for change detection through a call to EntityManager#persist(entity) or through a save cascade.
Deferred Implicit P ro : persist($entity) is not necessary Cons : bad performance with large UOW Deferred Explicit Pro : flush operation cheaper Cons : DB != UOW spread persists into the code Unit of work now has $entity.title=liuggio loves symfony2 Database has flushed without persist $entity.title=liuggio loves symfony2 Pro : $entityManager->persist($entity) is not necessary Cons : Slowness, Doctrine has to check property-by-property for all the object in the unit of work Explicit Doctrine2 tracking policy 23 April 2013 doctrine2 1 php 9 Which tracking policy are you using? With doctrine you can change the tracking policy for each entity, the tracking policy is the way in which Doctrine understands if a entity property must be ‘ saved ’ in the database. Doctrine definition: “ Change tracking is the process of determining what has changed in managed entities since the last time they were synchronized with the database. ” There are 3 tracking policies: Implicit, Explicit, Nofity. Implicit tracking (the default one) The implicit is the default one and the slowest, you don ’ t need to explicit call ‘ persist ’ eg. // $entity is not a new object, and has $entity->Title = 'liuggio loves tvision.github.io' $entity->setTitle('liuggio loves symfony2'); $entityManager->flush(); echo $entity->getTitle(); // output 'liuggio loves symfony2' Unit of work now has $entity.title=liuggio loves symfony2 Database has flushed without persist $entity.title=liuggio loves symfony2 Pro : $entityManager->persist($entity) is not necessary Cons : Slowness, Doctrine has to check property-by-property for all the object in the unit of work The explicit tracking (the suggested one) You have to explicit flag which object you want to persist, than Doctrine will check property-by-property in order to find with property has changed. Pro : Faster than implicit, must if you have a lot of entities. Cons : the object in the unit of work could be different from that one in the db, not easy to debug. The example
Temporarily mark entities as read only. If you have a very large UnitOfWork but know that a large set of entities has not changed, just mark them as read only with $entityManager->getUnitOfWork()->markReadOnly($entity).
T he unit of work class in doctrine ha 3206 lines, is complex UOW start with with init of EM or after a flush. The size of a Unit of Work mainly refers to the number of managed entities Is an Entity managed by UOW when is persisted or when is fetched Identity Map: In this case the Article is accessed from the entity manager twice, but modified in between. How costly a flush operation is, mainly depends on two factors: The size of the EntityManager ’ s current UnitOfWork. The configured change tracking policies You can get the size of a UnitOfWork as follows: <?php $uowSize = $em->getUnitOfWork()->size();
OK the real title is zzz and blblabla will move your something to the next level
I’d like to talk to you about metrics I do love metrics Mr smisth sentence:
È molto
OK the real title is zzz and blblabla will move your something to the next level
OK the real title is zzz and blblabla will move your something to the next level
OK the real title is zzz and blblabla will move your something to the next level
OK the real title is zzz and blblabla will move your something to the next level
OK the real title is zzz and blblabla will move your something to the next level
OK the real title is zzz and blblabla will move your something to the next level
OK the real title is zzz and blblabla will move your something to the next level
OK the real title is zzz and blblabla will move your something to the next level
OK the real title is zzz and blblabla will move your something to the next level