Skip to content
This repository has been archived by the owner on Dec 31, 2022. It is now read-only.

Commit

Permalink
Caching is now an option for the PieCrust constructor so that config …
Browse files Browse the repository at this point in the history
…and page caching can both be controlled by it.

Refactored the error handling code to be more robust and simpler.
  • Loading branch information
ludovicchabant committed Mar 3, 2011
1 parent f5b15ac commit be0a666
Show file tree
Hide file tree
Showing 12 changed files with 170 additions and 127 deletions.
11 changes: 11 additions & 0 deletions tests/_content/config.yml
@@ -0,0 +1,11 @@
site:
title: Test Site
enable_cache: true

something:
foo: bar
blah: blah

author:
name: John Doe
email: john@doe.com
73 changes: 49 additions & 24 deletions tests/benchmark.php
Expand Up @@ -12,6 +12,12 @@
define('PIECRUST_BENCHMARKS_CACHE_DIR', PIECRUST_ROOT_DIR . '_cache');
require_once 'PieCrust.class.php';

function init_app($cache)
{
$pc = new PieCrust(array('cache' => $cache));
$pc->getConfig();
}

function run_query($pieCrust, $uri)
{
$page = new Page($pieCrust, $uri);
Expand All @@ -20,12 +26,15 @@ function run_query($pieCrust, $uri)
}

function run_detailed_query($bench, $pieCrust, $uri)
{
{
$pieCrust->getConfig();
$bench->setMarker('App config loaded');

$page = new Page($pieCrust, $uri);
$bench->setMarker('Created page');

$page->getConfig();
$bench->setMarker('Loaded page config and contents.');
$bench->setMarker('Loaded page config and contents');

$renderer = new PageRenderer($pieCrust);
$bench->setMarker('Created renderer');
Expand All @@ -46,35 +55,58 @@ function run_detailed_query($bench, $pieCrust, $uri)
<body>
<?php

echo '<h1>Rendering Markdown syntax page</h1>';

echo '<h1>PieCrust Benchmarks</h1>';

$runCount = 100;
function filter_end_marker($value) { return preg_match('/^end_/', $value['name']); }
function map_diff_time($value) { return $value['diff']; }
function display_profiling_times($prof)
{
$diffValues = array_map('map_diff_time', array_filter($prof, 'filter_end_marker'));
echo '<p>Ran '.$runCount.' times.</p>';
echo '<p>Median time: <strong>'.(median($diffValues)*1000).'ms</strong></p>';
echo '<p>Average time: <strong>'.(average($diffValues)*1000).'ms</strong></p>';
echo '<p>Max time: <strong>'.(max($diffValues)*1000).'ms</strong></p>';
}

//
// Iteration benchmark.
// App init benchmark.
//
echo '<h2>Iteration Benchmark</h2>';
echo '<h2>App Init Benchmark (non-caching config)</h2>';
ensure_cache(PIECRUST_BENCHMARKS_CACHE_DIR, true);
$bench = new Benchmark_Iterate();
$bench->start();
$bench->run($runCount, 'init_app', false);
$bench->stop();
display_profiling_times($bench->getProfiling());

echo '<h2>App Init Benchmark (caching config)</h2>';
ensure_cache(PIECRUST_BENCHMARKS_CACHE_DIR, true);
$bench = new Benchmark_Iterate();
$bench->start();
$bench->run($runCount, 'init_app', true);
$bench->stop();
display_profiling_times($bench->getProfiling());

//
// Page rendering benchmark.
//
echo '<h2>Page Rendering Benchmark</h2>';
ensure_cache(PIECRUST_BENCHMARKS_CACHE_DIR, true);
$bench = new Benchmark_Iterate();
$bench->start();
$pieCrust = new PieCrust();
$pieCrust->setConfig(array('site' => array('enable_cache' => true)));
$runCount = 100;
$bench->run($runCount, 'run_query', $pieCrust, '/empty');
$bench->stop();

function filter_end_marker($value) { return preg_match('/^end_/', $value['name']); }
function map_diff_time($value) { return $value['diff']; }
$prof = $bench->getProfiling();
$diffValues = array_map('map_diff_time', array_filter($prof, 'filter_end_marker'));
echo '<p>Ran page query '.$runCount.' times.</p>';
echo '<p>Median page query: <strong>'.(median($diffValues)*1000).'ms</strong></p>';
echo '<p>Average page query: <strong>'.(average($diffValues)*1000).'ms</strong></p>';
echo '<p>Max page query: <strong>'.(max($diffValues)*1000).'ms</strong></p>';
display_profiling_times($bench->getProfiling());

//
// Marked run (uncached, then cached).
//
echo '<h2>Timed Benchmark</h2>';

$pieCrust = new PieCrust();

echo '<h3>Uncached</h3>';
ensure_cache(PIECRUST_BENCHMARKS_CACHE_DIR, true);
$bench = new Benchmark_Timer();
Expand All @@ -90,13 +122,6 @@ function map_diff_time($value) { return $value['diff']; }
$bench->stop();
$bench->display();

/*echo '<h1>Rendering HTML page</h1>';
$bench = new Benchmark_Iterate();
$bench->start();
$bench->run(10, 'file_get_contents', '_reference/markdown-syntax.html');
$bench->stop();
$bench->display();*/

?>
</body>
</html>
4 changes: 3 additions & 1 deletion website/_content/pages/_404.html
@@ -1,3 +1,5 @@
---
layout: 404
title: Not found
---
<h2>Sorry, we couldn't find what you asked for.</h2>
<p>The article or resource you requested is not here.</p>
14 changes: 12 additions & 2 deletions website/_content/pages/_error.html
@@ -1,4 +1,14 @@
---
layout: error
title: Oops, something went wrong
---
<p>Try going back to the homepage and navigate from there.</p>
<h2>Oops, something went wrong</h2>
<p>{{ error.message }}</p>
{% if error.debug %}
<p>Error: <code>{{ error.code }}</code><br/>
File: <code>{{ error.file }}</code><br/>
Line <code>{{ error.line }}</code></p>
<blockquote>
<pre>{{ error.trace }}</ppre>
</blockquote>
{% endif %}
{{ content|raw }}
6 changes: 0 additions & 6 deletions website/_content/templates/404.html

This file was deleted.

19 changes: 0 additions & 19 deletions website/_content/templates/error.html

This file was deleted.

33 changes: 0 additions & 33 deletions website/_piecrust/FatalError.inc.php

This file was deleted.

4 changes: 2 additions & 2 deletions website/_piecrust/Page.class.php
Expand Up @@ -220,7 +220,7 @@ public function __construct(PieCrust $pieCrust, $uri)
}

$this->cache = null;
if ($pieCrust->getConfigValueUnchecked('site', 'enable_cache') === true)
if ($pieCrust->isCachingEnabled())
{
$this->cache = new Cache($pieCrust->getCacheDir() . 'pages_r');
}
Expand Down Expand Up @@ -346,7 +346,7 @@ protected function loadConfigAndContents()

foreach (array_keys($segments) as $key)
{
$this->cache->write($this->uri, $key . '.html', $this->contents);
$this->cache->write($this->uri . '.' . $key, 'html', $this->contents);
}
}
}
Expand Down

0 comments on commit be0a666

Please sign in to comment.