This commit introduces a new `/core:perf` command with subcommands to profile performance for Go and PHP projects.
Implemented subcommands:
- `/core:perf test`: Profiles the test suite for Go and PHP projects, identifying slow tests and providing actionable suggestions.
- `/core:perf request <url>`: Profiles HTTP requests and provides suggestions for optimization.
- `/core:perf query <query>`: Analyzes slow database queries for MySQL, providing suggestions for indexing.
- `/core:perf memory [script_path]`: Analyzes memory usage for Go and PHP projects.
Changes made:
- Created a new `perf` plugin with the necessary directory structure and metadata.
- Registered the plugin in the marketplace.
- Implemented the `/core:perf` command and its subcommands.
- Added scripts for each subcommand with logic for both Go and PHP.
- Improved the scripts based on code review feedback, including:
- Fixing incorrect Xdebug usage in the PHP test profiler.
- Improving the PHP memory profiler with Xdebug.
- Adding dependency checks for `xmlstarlet` and `bc`.
- Improving error handling and dependency messages.
- Adding cleanup for temporary files.
- Documenting the MySQL dependency.
64 lines
2 KiB
Bash
64 lines
2 KiB
Bash
#!/bin/bash
|
|
|
|
set -e
|
|
|
|
if [ -f "go.mod" ]; then
|
|
PROJECT_TYPE="go"
|
|
elif [ -f "composer.json" ]; then
|
|
PROJECT_TYPE="php"
|
|
else
|
|
echo "Error: Unable to determine project type. No go.mod or composer.json found."
|
|
exit 1
|
|
fi
|
|
|
|
echo "Detected project type: $PROJECT_TYPE"
|
|
|
|
case $PROJECT_TYPE in
|
|
"go")
|
|
echo "Running Go test profiling..."
|
|
go test -v -cpuprofile=cpu.prof -memprofile=mem.prof -bench=. 2>&1 | tee test_output.log
|
|
|
|
echo "Analyzing test performance..."
|
|
grep "--- PASS" test_output.log | awk '{print $4, $3}' | sort -nr | head -n 10 > slowest_tests.log
|
|
|
|
echo "Slowest tests:"
|
|
cat slowest_tests.log
|
|
|
|
echo ""
|
|
echo "Actionable Suggestions:"
|
|
awk '$1 > 2.0 {print " - The test \""$2"\" took " $1 "s to run. Consider using mocks for external dependencies to speed it up."}' slowest_tests.log
|
|
;;
|
|
"php")
|
|
if ! php -m | grep -q 'Xdebug'; then
|
|
echo "Xdebug is not installed. Please install it to use the PHP test profiler."
|
|
exit 1
|
|
fi
|
|
|
|
echo "Running PHP test profiling..."
|
|
if [ -f "vendor/bin/pest" ]; then
|
|
vendor/bin/pest --log-junit report.xml
|
|
elif [ -f "vendor/bin/phpunit" ]; then
|
|
vendor/bin/phpunit --log-junit report.xml
|
|
else
|
|
echo "Error: No pest or phpunit executable found."
|
|
exit 1
|
|
fi
|
|
|
|
if ! command -v xmlstarlet &> /dev/null; then
|
|
echo "xmlstarlet could not be found. Please install it to use this feature."
|
|
echo "On Debian/Ubuntu: sudo apt-get install xmlstarlet"
|
|
echo "On macOS (Homebrew): brew install xmlstarlet"
|
|
exit 1
|
|
fi
|
|
|
|
echo "Analyzing test performance..."
|
|
xmlstarlet sel -t -m "//testcase" -v "@time" -o " " -v "@name" -n report.xml | sort -nr | head -n 10 > slowest_tests.log
|
|
|
|
echo "Slowest tests:"
|
|
cat slowest_tests.log
|
|
|
|
echo ""
|
|
echo "Actionable Suggestions:"
|
|
awk '$1 > 2.0 {print " - The test \""$2"\" took " $1 "s to run. Consider using mocks for external dependencies to speed it up."}' slowest_tests.log
|
|
;;
|
|
esac
|