PHP code example of zachiler / laravel-cadence
1. Go to this page and download the library: Download zachiler/laravel-cadence library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
zachiler / laravel-cadence example snippets
// app/Cadence/Handlers/OrderHandler.php
class OrderHandler extends \Cadence\Support\BaseHandler
{
protected function handle(\Cadence\State\TickContext $context): void
{
$context->businessHours()->every('1 hour', 'create-orders', function () {
Order::factory()->count($this->config('order_rate', 3))->create();
});
}
}
'scenarios' => [
'growth' => App\Cadence\Scenarios\GrowthScenario::class,
],
// config/cadence.php
return [
// Environments where Cadence is allowed to run.
'allowed_environments' => ['local', 'staging'],
// Default simulation settings. Can be overridden per-run via CLI options.
'defaults' => [
// Simulated seconds per real second. 900 = 15 simulated minutes per real second.
'speed' => 900,
// Real-time interval between ticks in milliseconds.
'tick_interval' => 1000,
],
// Named presets for common simulation profiles.
// Usage: php artisan cadence:run --preset=fast
'presets' => [
'fast' => ['speed' => 7200, 'tick_interval' => 500],
'slow' => ['speed' => 300, 'tick_interval' => 2000],
'realtime' => ['speed' => 1, 'tick_interval' => 1000],
],
// Queue connection during simulation. Must be 'database'.
'queue_connection' => 'database',
// Where database snapshots are stored.
'snapshot_path' => storage_path('cadence/snapshots'),
// Custom binary paths for snapshot commands (null = auto-detect).
'binary_paths' => [
'mysqldump' => null,
'mysql' => null,
'pg_dump' => null,
'psql' => null,
],
// Prefix for all cache keys used for cross-process state.
'cache_prefix' => 'cadence',
// Cache store for simulation state and signals. null = app default.
'state_store' => null,
// Named scenarios (implement Cadence\Contracts\Scenario).
// Supports class strings or [class, default_params] arrays.
'scenarios' => [
// 'growth' => App\Cadence\Scenarios\GrowthScenario::class,
// 'growth-aggressive' => ['class' => App\Cadence\Scenarios\GrowthScenario::class, 'params' => ['signup_rate' => 0.9]],
],
// Tick handler classes (used when no --scenario is specified).
'handlers' => [],
// Default options passed to handler config.
'handler_options' => [],
// Maximum real-time seconds a single tick is allowed to take. 0 = no limit.
'max_tick_duration' => 30,
// Event log storage driver: 'database' or 'file'.
'event_log_driver' => 'database',
// Path for JSONL event log when using the 'file' driver.
'event_log_path' => storage_path('cadence/events.jsonl'),
];
$context->businessHours()->every('1 hour', 'process-orders', function () { /* ... */ });
$context->weekends()->every('4 hours', 'weekend-report', function () { /* ... */ });
$context->on('monday')->between('09:00', '12:00')->every('30 minutes', 'standup', function () { /* ... */ });
class BillingHandler extends BaseHandler implements HasHandlerDependencies
{
public function dependsOn(): array
{
return [TeamLifecycleHandler::class, ProjectActivityHandler::class];
}
}
$runner->invariant(fn () => User::count() > 0, 'has-users', InvariantBehavior::Pause);
$runner->breakWhen(fn () => Order::where('status', 'failed')->count() > 10, 'too-many-failures');
$this->metric('teams.active', Team::count()); // gauge
$this->increment('invoices.created', $count); // counter
$series = MetricQuery::series('teams.active'); // query after run
$runner->speedSchedule([
['until' => '7 days', 'speed' => 3600],
['until' => 'end', 'speed' => 60],
]);
class LogTickDuration implements TickMiddleware
{
public function handle(TickContext $context, \Closure $next): void
{
$start = microtime(true);
$next($context);
logger("Tick {$context->tick} took " . round(microtime(true) - $start, 3) . 's');
}
}
bash
composer cadence:install
php artisan migrate
bash
php artisan cadence:run --scenario=growth --duration="14 days"