Tweak UX for branch and PR names, commit messages, and timestamp displays

This commit is contained in:
afik.cohen 2024-01-31 13:09:57 -08:00
parent 3ab4e39764
commit 4dc465a4f6
9 changed files with 51 additions and 28 deletions

View file

@ -8,7 +8,7 @@
<env name="PYTHONUNBUFFERED" value="1" />
</envs>
<option name="SDK_HOME" value="" />
<option name="SDK_NAME" value="codeflash311" />
<option name="SDK_NAME" value="$USER_HOME$/miniforge3/envs/codeflash311" />
<option name="WORKING_DIRECTORY" value="$PROJECT_DIR$/cli" />
<option name="IS_MODULE_SDK" value="false" />
<option name="ADD_CONTENT_ROOTS" value="true" />

View file

@ -8,7 +8,7 @@
<env name="PYTHONUNBUFFERED" value="1" />
</envs>
<option name="SDK_HOME" value="" />
<option name="SDK_NAME" value="$USER_HOME$/miniforge3/envs/codeflash" />
<option name="SDK_NAME" value="$USER_HOME$/miniforge3/envs/codeflash311" />
<option name="WORKING_DIRECTORY" value="$PROJECT_DIR$/cli" />
<option name="IS_MODULE_SDK" value="false" />
<option name="ADD_CONTENT_ROOTS" value="true" />

View file

@ -13,7 +13,8 @@ class PrComment:
original_runtime: int
function_name: str
relative_file_path: str
speedup: float
speedup_x: str
speedup_pct: str
winning_test_results: TestResults
def to_json(self) -> dict[str, Union[str, dict[str, dict[str, int]]]]:
@ -23,8 +24,8 @@ class PrComment:
"original_runtime": f"{(self.original_runtime / 1000):.2f}",
"function_name": self.function_name,
"file_path": self.relative_file_path,
"speedup_x": f"{self.speedup:.2f}",
"speedup_pct": f"{self.speedup * 100:.2f}",
"speedup_x": self.speedup_x,
"speedup_pct": self.speedup_pct,
"report_table": {
test_type.to_name(): result
for test_type, result in self.winning_test_results.get_test_pass_fail_report_by_type().items()

View file

@ -7,7 +7,7 @@ from codeflash.cli_cmds.cli import process_cmd_args
from codeflash.cli_cmds.cmd_init import CODEFLASH_LOGO
from codeflash.code_utils.instrument_existing_tests import inject_profiling_into_existing_test
from codeflash.code_utils.linter import lint_code
from codeflash.result.create_pr import create_pr
from codeflash.result.create_pr import check_create_pr
from codeflash.result.explanation import Explanation
logging.basicConfig(level=logging.INFO, format="[%(levelname)s] %(message)s", stream=sys.stdout)
@ -327,11 +327,9 @@ class Optimizer:
f"\n{generated_original_test_source}"
)
logging.info(
f"⚡️ Optimization successful! 📄 {function_name} in {path} 📈 "
f"{explanation_final.speedup * 100:.2f}% ({explanation_final.speedup:.2f}x) faster"
)
create_pr(
logging.info(f"⚡️ Optimization successful! 📄 {function_name} in {path} 📈")
logging.info(explanation_final.perf_improvement_line)
check_create_pr(
optimize_all=self.args.all,
path=path,
original_code=original_code,

View file

@ -9,7 +9,7 @@ from codeflash.github.PrComment import FileDiffContent, PrComment
from codeflash.result.explanation import Explanation
def create_pr(
def check_create_pr(
optimize_all: bool,
path: str,
original_code: str,
@ -39,7 +39,8 @@ def create_pr(
original_runtime=explanation.original_runtime_ns,
function_name=explanation.function_name,
relative_file_path=relative_path,
speedup=explanation.speedup,
speedup_x=explanation.speedup_x,
speedup_pct=explanation.speedup_pct,
winning_test_results=explanation.winning_test_results,
),
generated_tests=generated_original_test_source,
@ -73,7 +74,8 @@ def create_pr(
original_runtime=explanation.original_runtime_ns,
function_name=explanation.function_name,
relative_file_path=relative_path,
speedup=explanation.speedup,
speedup_x=explanation.speedup_x,
speedup_pct=explanation.speedup_pct,
winning_test_results=explanation.winning_test_results,
),
generated_tests=generated_original_test_source,

View file

@ -13,10 +13,22 @@ class Explanation:
function_name: str
path: str
@property
def perf_improvement_line(self) -> str:
return f"{self.speedup_pct} improvement ({self.speedup_x} faster)."
@property
def speedup(self) -> float:
return (self.original_runtime_ns / self.best_runtime_ns) - 1
@property
def speedup_x(self) -> str:
return f"{self.speedup:,.2f}x"
@property
def speedup_pct(self) -> str:
return f"{self.speedup * 100:,.0f}%"
def to_console_string(self) -> str:
# TODO: After doing the best optimization, remove the test cases that errored on the new code, because they might be failing because of syntax errors and such.
# TODO: Sometimes the explanation says something similar to "This is the code that was optimized", remove such parts
@ -24,13 +36,14 @@ class Explanation:
best_runtime_human = humanize_runtime(self.best_runtime_ns)
explanation = (
f"Function {self.function_name} in file {self.path}:\n"
f"Performance went up by {self.speedup:.2f}x ({self.speedup * 100:.2f}%). Runtime went down from {original_runtime_human} to {best_runtime_human} \n\n"
+ "Optimization explanation:\n"
f"Optimized {self.function_name} in {self.path}\n"
f"{self.perf_improvement_line}\n"
f"Runtime went down from {original_runtime_human} to {best_runtime_human} \n\n"
+ "Explanation:\n"
+ self.raw_explanation_message
+ " \n\n"
+ "The code has been tested for correctness.\n"
+ f"Test Results for the best optimized code:- {TestResults.report_to_string(self.winning_test_results.get_test_pass_fail_report_by_type())}\n"
+ "The new optimized code was tested for correctness. The results are listed below.\n"
+ f"{TestResults.report_to_string(self.winning_test_results.get_test_pass_fail_report_by_type())}\n"
)
return explanation

View file

@ -193,7 +193,7 @@ export async function createStandalonePullRequest(
const prCommentBody = buildResultDetails(prCommentFields)
const prCommentTestReport = buildResultTestReport(prCommentFields, generatedTests)
const title: string = `CodeFlash Optimizations for \`${prCommentFields.function_name}()\` in \`${prCommentFields.file_path}\` ⚡️`
const title: string = `${prCommentFields.speedup_pct} faster ⚡️ Optimize \`${prCommentFields.function_name}()\``
const body: string = `${prCommentHeader}\n` + `${prCommentBody}\n` + `${prCommentTestReport}`
return await createNewPullRequest(
@ -222,7 +222,7 @@ export async function createDependentPullRequest(
const prCommentBody = buildResultDetails(prCommentFields)
const prCommentTestReport = buildResultTestReport(prCommentFields, generatedTests)
const title = `CodeFlash Optimizations for PR #${origPrNumber} ⚡️`
const title = `${prCommentFields.speedup_pct} faster ⚡️ Optimize \`${prCommentFields.function_name}()\` in PR #${origPrNumber} (\`${baseBranch}\`)`
const body =
`## ⚡️ This pull request contains optimizations for PR #${origPrNumber}
If you approve this dependent PR, these changes will be merged into the original PR branch \`${baseBranch}\`.

View file

@ -1,5 +1,5 @@
### 📄 `{function_name}()` in `{file_path}`
📈 Performance went up by **`{speedup_pct}%`** (**`{speedup_x}x`**)
📈 Performance went up by **`{speedup_pct}`** (**`{speedup_x}`**)
⏱️ Runtime went down from **`{original_runtime}μs`** to **`{best_runtime}μs`**

View file

@ -191,7 +191,13 @@ appExpress.post("/cfapi/suggest-pr-changes", async (req, res) => {
)
console.log(`Making a new dependent PR...`)
const newBranchName = `codeflash-optimizations-for-pr${pullNumber}-${Date.now()}`
// a timestamp format like 2024-01-31-12.59.48
const timestamp = new Date()
.toISOString()
.replace(/:/g, ".")
.replace(/\.\d+Z$/, "")
const newBranchName = `codeflash-optimize-pr${pullNumber}-${timestamp}`
// Get the head branch of the original pull request
const originalPrData = await installationOctokit.rest.pulls.get({
@ -208,7 +214,7 @@ appExpress.post("/cfapi/suggest-pr-changes", async (req, res) => {
newBranchName,
baseBranch,
diffContentsMap,
`Code Optimizations for PR #${pullNumber}`,
`${prCommentFields.speedup_pct} faster ⚡️ Optimize ${prCommentFields.function_name} in PR #${pullNumber}`,
)
if (!branchCreated) {
throw new Error(`Failed to create branch ${newBranchName}`)
@ -335,9 +341,12 @@ appExpress.post("/cfapi/create-pr", async (req, res) => {
const diffContentsMap: Map<string, FileDiffContent> = fileDiffsToMap(diffContents)
const newBranchName = `codeflash-optimizations-for-function-${
prCommentFields.function_name
}-${Date.now()}`
// a timestamp format like 2024-01-31-12.59.48
const timestamp = new Date()
.toISOString()
.replace(/:/g, ".")
.replace(/\.\d+Z$/, "")
const newBranchName = `codeflash-optimize-function-${prCommentFields.function_name}-${timestamp}`
const branchCreated = await createNewBranchFromDiffContents(
installationOctokit,
owner,
@ -345,7 +354,7 @@ appExpress.post("/cfapi/create-pr", async (req, res) => {
newBranchName,
baseBranch,
diffContentsMap,
`Code Optimizations for ${prCommentFields.function_name}`,
`${prCommentFields.speedup_pct} faster ⚡️ Optimize ${prCommentFields.function_name}`,
)
if (!branchCreated) {
throw new Error(`Failed to create branch ${newBranchName}`)