Skip to content

Commit

Permalink
fix: improve error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
art049 committed Nov 21, 2022
1 parent debb0e5 commit 8aa08e1
Show file tree
Hide file tree
Showing 5 changed files with 124 additions and 128 deletions.
12 changes: 6 additions & 6 deletions dist/index.js

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions dist/index.js.map

Large diffs are not rendered by default.

39 changes: 20 additions & 19 deletions src/prepare.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,29 @@
import * as core from "@actions/core";
import {exec} from "@actions/exec";

const prepare = async (): Promise<void> =>
core.group("Prepare environment", async () => {
const prepare = async (): Promise<void> => {
core.startGroup("Prepare environment");
try {
await exec("sudo apt-get install -y valgrind", [], {
silent: true,
});
try {
await exec("sudo apt-get install -y valgrind", [], {
await exec("pip show pytest-codspeed", [], {
silent: true,
});
} catch (e) {
core.warning(
"pytest-codspeed is not installed in your environment. Installing it..."
);
await exec("pip install pytest-codspeed", [], {
silent: true,
});
try {
await exec("pip show pytest-codspeed", [], {
silent: true,
});
} catch (e) {
core.warning(
"pytest-codspeed is not installed in your environment. Installing it..."
);
await exec("pip install pytest-codspeed", [], {
silent: true,
});
}
} catch (error) {
throw new Error(`Failed to prepare environment: ${error}`);
}
core.info("Environment ready");
});
} catch (error) {
throw new Error(`Failed to prepare environment: ${error}`);
}
core.info("Environment ready");
core.endGroup();
};

export default prepare;
95 changes: 45 additions & 50 deletions src/run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,54 +40,49 @@ const outputListener = (line: string): void => {
}
};

const run = async (inputs: ActionInputs): Promise<{profilePath: string}> =>
core.group("Run benchmarks", async () => {
const arch = await getArch();
const profilePath = getTempFile();
const valgrindOptions = [
"-q",
"--tool=callgrind",
"--cache-sim=yes",
"--I1=32768,8,64",
"--D1=32768,8,64",
"--LL=8388608,16,64",
"--instr-atstart=no",
"--compress-strings=no",
"--combine-dumps=yes",
"--dump-line=no",
`--callgrind-out-file=${profilePath}`,
];
try {
await exec(
[
"setarch",
arch,
"-R",
"valgrind",
...valgrindOptions,
inputs.run,
].join(" "),
[],
{
env: {
...process.env,
PYTHONMALLOC: "malloc",
PYTHONHASHSEED: "0",
ARCH: arch,
CODSPEED_ENV: "github",
},
silent: true,
listeners: {
stdline: outputListener,
errline: outputListener,
},
}
);
} catch (error) {
core.debug(`Error: ${error}`);
throw new Error("Failed to run benchmarks");
}
return {profilePath};
});

const run = async (inputs: ActionInputs): Promise<{profilePath: string}> => {
core.startGroup("Run benchmarks");
const arch = await getArch();
const profilePath = getTempFile();
const valgrindOptions = [
"-q",
"--tool=callgrind",
"--cache-sim=yes",
"--I1=32768,8,64",
"--D1=32768,8,64",
"--LL=8388608,16,64",
"--instr-atstart=no",
"--compress-strings=no",
"--combine-dumps=yes",
"--dump-line=no",
`--callgrind-out-file=${profilePath}`,
];
try {
await exec(
["setarch", arch, "-R", "valgrind", ...valgrindOptions, inputs.run].join(
" "
),
[],
{
env: {
...process.env,
PYTHONMALLOC: "malloc",
PYTHONHASHSEED: "0",
ARCH: arch,
CODSPEED_ENV: "github",
},
silent: true,
listeners: {
stdline: outputListener,
errline: outputListener,
},
}
);
} catch (error) {
core.debug(`Error: ${error}`);
throw new Error("Failed to run benchmarks");
}
core.endGroup();
return {profilePath};
};
export default run;
102 changes: 51 additions & 51 deletions src/upload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,61 +43,61 @@ const upload = async (
inputs: ActionInputs,
profilePath: string
): Promise<void> => {
core.group("Upload Results", async () => {
const uploadMetadata = await getUploadMetadata({profilePath, inputs});
core.debug("Upload metadata:");
core.debug(JSON.stringify(uploadMetadata, null, 2));
const hash = crypto
.createHash("sha256")
.update(JSON.stringify(uploadMetadata))
.digest("hex");
if (inputs.tokenless) {
core.info(`CodSpeed Run Hash: "${hash}"`);
}
core.startGroup("Upload Results");
const uploadMetadata = await getUploadMetadata({profilePath, inputs});
core.debug("Upload metadata:");
core.debug(JSON.stringify(uploadMetadata, null, 2));
const hash = crypto
.createHash("sha256")
.update(JSON.stringify(uploadMetadata))
.digest("hex");
if (inputs.tokenless) {
core.info(`CodSpeed Run Hash: "${hash}"`);
}

core.info("Preparing upload");
let response: TypedResponse<PostResponse>;
try {
const headers = inputs.tokenless
? undefined
: {
Authorization: inputs.token,
};
response = await http.postJson<PostResponse>(
inputs.uploadUrl,
uploadMetadata,
headers
);
} catch (e) {
const err = e as httpm.HttpClientError;
throw new Error(
`Upload preparation failed (${err.statusCode}): ${err.message}`
);
}
if (!response.result) {
throw new Error("Upload preparation failed: no result");
}
core.info("Uploading profile data");
const profile = fs.readFileSync(profilePath);
core.debug(`Uploading ${profile.length} bytes...`);
const uploadResponse = await http.request(
"PUT",
response.result.uploadUrl,
Readable.from(profile),
{
"Content-Type": "application/octet-stream",
"Content-Length": profile.length,
"Content-MD5": uploadMetadata.profileMd5,
}
core.info("Preparing upload");
let response: TypedResponse<PostResponse>;
try {
const headers = inputs.tokenless
? undefined
: {
Authorization: inputs.token,
};
response = await http.postJson<PostResponse>(
inputs.uploadUrl,
uploadMetadata,
headers
);
if (uploadResponse.message.statusCode !== 200) {
throw new Error(
`Upload failed with status ${uploadResponse.message.statusCode}: ${uploadResponse.message.statusMessage}`
);
} catch (e) {
const err = e as httpm.HttpClientError;
throw new Error(
`Upload preparation failed (${err.statusCode}): ${err.message}`
);
}
if (!response.result) {
throw new Error("Upload preparation failed: no result");
}
core.info("Uploading profile data...");
const profile = fs.readFileSync(profilePath);
core.debug(`Uploading ${profile.length} bytes...`);
const uploadResponse = await http.request(
"PUT",
response.result.uploadUrl,
Readable.from(profile),
{
"Content-Type": "application/octet-stream",
"Content-Length": profile.length,
"Content-MD5": uploadMetadata.profileMd5,
}
);
if (uploadResponse.message.statusCode !== 200) {
throw new Error(
`Upload failed with status ${uploadResponse.message.statusCode}: ${uploadResponse.message.statusMessage}`
);
}

core.info("Results uploaded.");
});
core.info("Results uploaded.");
core.endGroup();
};

export default upload;

0 comments on commit 8aa08e1

Please sign in to comment.