Last active
December 25, 2015 13:08
-
-
Save tractorcow/6981101 to your computer and use it in GitHub Desktop.
SQLBenchmark.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| class SQLBenchmark extends SS_Benchmark { | |
| protected static $fixture_file = 'SQLBenchmark.yml'; | |
| protected $extraDataObjects = array( | |
| 'DataObjectTest_Team', | |
| 'DataObjectTest_Fixture', | |
| 'DataObjectTest_SubTeam', | |
| 'OtherSubclassWithSameField', | |
| 'DataObjectTest_FieldlessTable', | |
| 'DataObjectTest_FieldlessSubTable', | |
| 'DataObjectTest_ValidatedObject', | |
| 'DataObjectTest_Player', | |
| 'DataObjectTest_TeamComment' | |
| ); | |
| public function benchmarkSelect() { | |
| $this->benchmark('Select all pages', function() { | |
| SiteTree::get()->toArray(); | |
| }); | |
| $this->benchmark('Select top level pages', function() { | |
| SiteTree::get()->filter(array( | |
| "ShowInMenus" => 1, | |
| "ParentID" => 0 | |
| ))->toArray(); | |
| }); | |
| $this->benchmark('Select home page', function() { | |
| SiteTree::get()->filter(array( | |
| "URLSegment" => 'home', | |
| "ParentID" => 0 | |
| ))->first(); | |
| }); | |
| $this->benchmark('Performance of DataList->relation', function() { | |
| DataObjectTest_Team::get()->relation('Players')->toArray(); | |
| }); | |
| $this->benchmark('Aggregate query (SUM)', function() { | |
| DataObjectTest_Player::get()->sum('ShirtNumber'); | |
| }); | |
| $this->benchmark('Aggregate query (SUM) with condition', function() { | |
| DataObjectTest_Player::get()->exclude('FirstName', 'Captain')->sum('ShirtNumber'); | |
| }); | |
| } | |
| public function benchmarkMethods() { | |
| $this->benchmark('Select top level pages using DB::query', function() { | |
| DB::query(' | |
| SELECT * FROM "SiteTree" | |
| WHERE "SiteTree"."ParentID" = 0 | |
| AND "SiteTree"."ShowInMenus" = 1 | |
| ORDER BY "Sort" ASC' | |
| )->table(); | |
| }); | |
| if(method_exists('DB', 'prepared_query')) { | |
| $this->benchmark('Select top level pages using DB::prepared_query', function() { | |
| DB::prepared_query(' | |
| SELECT * FROM "SiteTree" | |
| WHERE "SiteTree"."ParentID" = ? | |
| AND "SiteTree"."ShowInMenus" = ? | |
| ORDER BY "Sort" ASC', | |
| array( | |
| 0, | |
| 1 | |
| ) | |
| )->table(); | |
| }); | |
| } | |
| } | |
| public function benchmarkCreate() { | |
| $this->benchmark('Create DataObject', function(){ | |
| $obj = new DataObjectTest_Fixture(); | |
| $obj->Data = 'NewObject'; | |
| $obj->DatetimeField = date('Y-m-d H:i:s'); | |
| $obj->write(); | |
| }); | |
| $this->benchmark('Create and publish page', function() { | |
| $page = new SiteTree(); | |
| $page->Title = 'My Page'; | |
| $page->write(); | |
| $page->doPublish(); | |
| }); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment