markhuot / craft-pest

https://craft-pest.com
Other
38 stars 11 forks source link

WIP Benchmarks #56

Closed markhuot closed 1 year ago

markhuot commented 1 year ago

Benchmarks will allow you to assert against a profile of the page under test. You could use this to check if a page is using eager loading correctly or if it has any particularly slow queries. For example,

it(‘benchmarks the homepage’)
  ->beginBenchmark()
  ->get(‘/‘)
  ->endBenchmark()
  ->assertLoadTimeLessThan(1)
  ->assertNoDuplicateQueries()
  ->assertAllQueriesFasterThan(0.05)
  ->assertMemoryLoadLessThan(2048)
  ->assertMorePerformantThanLastRun()
  ->savetoFile();

Because a test does not implicitly begin a benchmark you are free to do your setup first and then begin the benchmark, thus avoiding test preamble affecting your scores.

 it('loads an article', function () {
   $entry = Entry::factory()->section('articles')->create();

   $this->beginBenchmark()
     ->get($entry->uri)
     ->endBenchmark()
     ->assertLoadTimeLessThan(2);
 });