Skip to content

Commit

Permalink
Parameters and exception swallowing.
Browse files Browse the repository at this point in the history
  • Loading branch information
Dayle Rees committed Feb 4, 2016
1 parent 2ca0ee3 commit fbc6ebc
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 13 deletions.
25 changes: 25 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,31 @@ Upon running the code above, the control and trials are all executed. Using Jour

Once we've checked our data, and we're confident that our trial is equivalent (or better!) to our control code, we'll replace the experiment with that implementation.

## Parameters

Your count might not be useful without additional parameters. Don't worry, we've got you covered.

```php
->run($param, $secondParam, $more);
```

or

```php
->result($param, $secondParam, $more);
```

You can pass parameters to the `run()` or `result()` methods, and the parameters will be provided to both the control and trial callbacks on execution. Simple, right?

## Chance it!

Your experiments might be a little weighty. You might not want to run them all the time. Provide a % chance parameter to alter the chance that an experiment will run.

```php
$lab->experiment('foo')
->chance(50)...;
```

## Journals

Journals are assigned to a Laboratory, and are used to report on the result of experiments. You'll (hopefully soon) find a bunch of journals on packagist, or you can create your own.
Expand Down
33 changes: 26 additions & 7 deletions src/Execution.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,18 +33,27 @@ class Execution
*/
protected $match = false;

/**
* Thrown exception.
*
* @var boolean
*/
protected $exception;

/**
* Construct a new execution.
*
* @param mixed $value
* @param float $time
* @param boolean $match
* @param mixed $value
* @param float $time
* @param boolean $match
* @param \Exception $exception
*/
public function __construct($value, $time, $match = false)
public function __construct($value, $time, $match = false, $exception = null)
{
$this->value = $value;
$this->time = (float) $time;
$this->match = (boolean) $match;
$this->value = $value;
$this->time = (float) $time;
$this->match = (boolean) $match;
$this->exception = $exception;
}

/**
Expand Down Expand Up @@ -77,6 +86,16 @@ public function isMatch()
return $this->match;
}

/**
* Get a thrown exception.
*
* @return boolean
*/
public function getException()
{
return $this->exception;
}

/**
* Retrieve the execution in JSON string format.
*
Expand Down
10 changes: 10 additions & 0 deletions src/Experiment.php
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,16 @@ public function shouldRun()
return rand(0, 100) <= $this->chance;
}

/**
* Get the experiment parameters.
*
* @return array
*/
public function getParams()
{
return $this->params;
}

/**
* Execute the experiment within the laboratory.
*
Expand Down
31 changes: 27 additions & 4 deletions src/Intern.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Scientist;

use Exception;
use Scientist\Matchers\Matcher;

/**
Expand Down Expand Up @@ -39,7 +40,11 @@ public function run(Experiment $experiment)
*/
protected function runControl(Experiment $experiment)
{
return $this->executeCallback($experiment->getControl(), $experiment->getMatcher());
return $this->executeCallback(
$experiment->getControl(),
$experiment->getParams(),
$experiment->getMatcher()
);
}

/**
Expand All @@ -55,7 +60,12 @@ protected function runTrials(Experiment $experiment, Execution $control)
$executions = [];

foreach ($experiment->getTrials() as $name => $trial) {
$executions[$name] = $this->executeCallback($trial, $experiment->getMatcher(), $control);
$executions[$name] = $this->executeCallback(
$trial,
$experiment->getParams(),
$experiment->getMatcher(),
$control
);
}

return $executions;
Expand All @@ -65,13 +75,15 @@ protected function runTrials(Experiment $experiment, Execution $control)
* Execute a callback and record an execution.
*
* @param callable $callable
* @param array $params
* @param \Scientist\Matchers\Matcher $matcher
* @param \Scientist\Execution|null $control
*
* @return \Scientist\Execution
*/
protected function executeCallback(
callable $callable,
array $params,
Matcher $matcher,
Execution $control = null
) {
Expand All @@ -80,8 +92,18 @@ protected function executeCallback(
* recorded timestamps, so that we can calculate
* the execution time of the code.
*/
$exception = null;
$before = microtime(true);
$value = call_user_func($callable);
if ($control) {
try {
$value = call_user_func_array($callable, $params);
} catch (Exception $ex) {
$value = null;
$exception = $ex;
}
} else {
$value = call_user_func_array($callable, $params);
}
$after = microtime(true);

/**
Expand All @@ -92,7 +114,8 @@ protected function executeCallback(
return new Execution(
$value,
$after - $before,
$matcher->match($value, $compare)
$matcher->match($value, $compare),
$exception
);
}
}
8 changes: 6 additions & 2 deletions src/Laboratory.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,12 @@ public function runExperiment(Experiment $experiment)
if ($experiment->shouldRun()) {
$result = $this->getResult($experiment);
return $result->control()->getValue();
} else {
return call_user_func($experiment->getControl());
}

return call_user_func_array(
$experiment->getControl(),
$experiment->getParams()
);
}

/**
Expand All @@ -99,6 +102,7 @@ public function getResult(Experiment $experiment)
{
$result = (new Intern)->run($experiment);
$this->reportToJournals($experiment, $result);

return $result;
}

Expand Down

0 comments on commit fbc6ebc

Please sign in to comment.