Wikia/app — pull request #87 by federico-lox (2012-11-12 17:03:32+00:00)
Title: ArticleService: refactor for performance

I've refactored ArticleService to speed it up sensibly with the goal of building a new API controller to use in our mobile apps.

The original implementation had a sub-optimal implementation which was also suffering from a caching bug, here's a summary of the major changes:

* uses anon parser cache instead of parsing a stripped down version of the article's wikitext
* processes HTML (tags names are a constant) instead of wikitext (which might change), this makes it more reliable
* caches the maximum snippet length, shorter versions are cut from that cache; the original version wanted to achieve the same but was buggy and once the cache for 150 chars was set, then asking for 80 was always returning 150 (numbers are just for the sake of an example)
* memoizes per-article maximum snippet to avoid memcache deserialization overhead when requesting the same snippet (even of different lengths) in the same process
* doesn't instantiate ArticleService in the purge-related hooks
* added a function to set the article instance directly
* added cache versioning

## Testode used for measurement ##

```php
class TestApiController extends WikiaApiController {
private $service;
private $articles = array(
2087,//w:c:glee:Rachel_Berry
2187,//w:c:glee:Santana_Lopez
27600,//w:c:glee:Blaine
17632,//w:c:glee:Season_Three
65341,//w:c:glee:Category:Characters
1461//w:c:glee:Glee_TV_Show_Wiki
);

public function init(){
$this->service = new ArticleService();
}

public function getList() {
$limit = $this->request->getInt( 'limit', 100 );
$res = array();
$start = microtime( true );

foreach ( $this->articles as $id ) {
$this->service->setArticleById($id);
$res[$id] = $this->service->getTextSnippet( $limit );
}

$total = microtime( true ) - $start;

$this->response->setVal( 'elapsed', $total );
$this->response->setVal( 'items', $res );
}
}
```

## Test results##

* before refactor
** no cache: **17~33s**
** cache: 0.050~0.150s
* after refactor
** no cache: **0.530~1.7s**
** cache: 0.017~0.033s

@Wyvek @owend @macbre @hakubo : I'd appreciate if you guys could take a look at this, I'd like this to be part of dev before Wednesday (no connection with the release, it's just because of dev deadlines). Thanks.