Tideways and Xhgui in Valet+ (Valet Plus)
This post is for folks who are already familiar with PHP, command line, Valet, Tideways and XHGui.
You can see some explanation and screenshots of Tideways and Xhgui in my Tideways and Xhgui using Dev Desktop post if you need a bit of an introduction.
I’ve successfully gotten Tideways running with Acquia Dev Desktop, Lando, and now Valet+. This post illustrates the process I used to get Tideways, Xhgui, and their dependencies running on my Mac for use in Valet+ sites.
Step 1: Setup in Valet the site we want to profile
1 2 3 |
mkdir -p ~/www/mysite/docroot cd ~/www/mysite/docroot valet link mysite |
Step 2: Setup Tideways php extension
1 2 |
brew tap tideways/homebrew-profiler brew install php71-tideways --env=std |
Step 3: Setup mongodb and its php extension
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
brew install mongodb sudo pecl install mongodb sudo chown adam:admin /usr/local/lib/php/pecl/20160303/mongodb.so sudo mkdir -p /data/db sudo chown adam:admin /data/db # Open a new terminal and leave mongod running mongod # Switch back to original terminal mongo > use xhprof > db.results.ensureIndex( { 'meta.SERVER.REQUEST_TIME' : -1 } ) > db.results.ensureIndex( { 'profile.main().wt' : -1 } ) > db.results.ensureIndex( { 'profile.main().mu' : -1 } ) > db.results.ensureIndex( { 'profile.main().cpu' : -1 } ) > db.results.ensureIndex( { 'meta.url' : 1 } ) |
Step 4: Setup in Valet the xhgui web application
1 2 3 4 5 |
mkdir ~/www/xhgui cd ~/www/xhgui git clone https://github.com/perftools/xhgui.git . php install.php composer install |
Step 5: Verify PHP extensions
First, restart valet with valet restart .
Then, create an info.php ( <?php phpinfo(); ?> ) file and verify that we have Tideways and mongodb libraries loading.
Step 6: Attempt to profile something
Drop in one-liners above and below what you want to profile. Example:
1 2 3 4 5 |
/**tideways-enable*/tideways_enable(TIDEWAYS_FLAGS_CPU+TIDEWAYS_FLAGS_MEMORY); doAllTheThings(); /**tideways-disable*/$description="My First Profile";$data=[];$data['profile']=tideways_disable();$time=array_key_exists('REQUEST_TIME',$_SERVER)?$_SERVER['REQUEST_TIME']:time();$request_time_float=explode('.',$_SERVER['REQUEST_TIME_FLOAT']);if(!isset($request_time_float[1])){$request_time_float[1]=0;}$request_ts=['sec'=>$time,'usec'=>0];$request_ts_micro=['sec'=>$request_time_float[0],'usec'=>$request_time_float[1]];$data['meta']=['url'=>$description,'SERVER'=>$_SERVER,'get'=>$_GET,'env'=>$_ENV,'request_ts'=>$request_ts,'request_ts_micro'=>$request_ts_micro,'request_date'=>date('Y-m-d',$time),];file_put_contents(date('Y-m-d_H-i-s').'--'.preg_replace('/[^a-z0-9.]+/i','',$description).'.xhprof',json_encode($data)); |
Step 7: Import the data into Xhgui and look at the results
Import the data into xhgui’s database
1 |
php ~/www/xhgui/external/import.php -f ~/www/mysite/docroot/2018-12-04_15-43-18--MyFirstProfile.xhprof |
Visit http://xhgui.test
3 Comments
Jonathan Shaw
Would love to see how to set up Tideways with Lando
adam
Done! https://agileadam.com/2019/03/tideways-xhgui-using-lando/
James Williams
For anyone on a recent mac system that runs into not being able to create /data/db due to having a Read-only file system and/or a ‘No such file or directory’ error – and so then gets the ‘exception in initAndListen: NonExistentPath: Data directory /data/db not found’ error when running
mongod
, instead run:mongod --config /usr/local/etc/mongod.conf
(This is adapted from https://stackoverflow.com/questions/58034955/read-only-file-system-when-attempting-mkdir-data-db-on-mac/58895373#58895373)