Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

automate branch and commit hash CLI options #211

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/slow-dolphins-shop.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@callstack/reassure-cli': minor
---

Automatically get branch and commit hash CLI options from Git if not passed
10 changes: 8 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,12 @@ command for the first time.

> **Note:** You can add `.reassure/` folder to your `.gitignore` file to avoid accidentally committing your results.

Reassure CLI will automatically try to detect your source code branch name and commit hash when you are using Git. You can override these options, e.g. if you are using different version control system:

```sh
yarn reassure --branch [branch name] --commit-hash [commit hash]
```

### Write performance testing script

In order to detect performance changes, you need to measure the performance of two versions of your code
Expand All @@ -184,12 +190,12 @@ git fetch origin
# Gather baseline perf measurements
git switch "$BASELINE_BRANCH"
yarn install --force
yarn reassure --baseline --branch $(git branch --show-current) --commitHash $(git rev-parse HEAD)
yarn reassure --baseline

# Gather current perf measurements & compare results
git switch --detach -
yarn install --force
yarn reassure --branch $(git branch --show-current) --commitHash $(git rev-parse HEAD)
yarn reassure
```

### CI integration
Expand Down
4 changes: 2 additions & 2 deletions examples/native/reassure-tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ git switch "$BASELINE_BRANCH"
pushd ../.. && yarn install --force && yarn turbo run build && popd

yarn install --force
yarn reassure --baseline --branch $(git branch --show-current) --commitHash $(git rev-parse HEAD)
yarn reassure --baseline

# Gather current perf measurements & compare results
git switch --detach -
Expand All @@ -22,4 +22,4 @@ git switch --detach -
pushd ../.. && yarn install --force && yarn turbo run build && popd

yarn install --force
yarn reassure --branch $(git branch --show-current) --commitHash $(git rev-parse HEAD)
yarn reassure --branch
1 change: 1 addition & 0 deletions packages/reassure-cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
"homepage": "https://github.com/callstack/reassure#readme",
"dependencies": {
"@callstack/reassure-compare": "0.1.1",
"simple-git": "^3.14.1",
"yargs": "^17.6.0"
},
"devDependencies": {
Expand Down
33 changes: 33 additions & 0 deletions packages/reassure-cli/src/commands/git.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import simpleGit from 'simple-git';

export async function getGitBranch() {
try {
const git = simpleGit();
const isRepo = await git.checkIsRepo();
if (!isRepo) {
return undefined;
}

const branch = await git.revparse(['--abbrev-ref', 'HEAD']);
return branch.trim() ? branch : undefined;
} catch (error) {
console.log('Failed to get git branch', error);
return undefined;
}
}

export async function getGitCommitHash() {
try {
const git = simpleGit();
const isRepo = await git.checkIsRepo();
if (!isRepo) {
return undefined;
}

const commitHash = await git.revparse(['HEAD']);
return commitHash.trim() ? commitHash : undefined;
} catch (error) {
console.log('Failed to get git commit hash', error);
return undefined;
}
}
22 changes: 12 additions & 10 deletions packages/reassure-cli/src/commands/measure.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import { resolve } from 'path';
import { spawnSync } from 'child_process';
import type { CommandModule } from 'yargs';
import { compare, formatMetadata } from '@callstack/reassure-compare';
import type { PerformanceMetadata } from '@callstack/reassure-compare/lib/typescript/types';
import { getGitBranch, getGitCommitHash } from './git';

const RESULTS_DIRECTORY = '.reassure';
const RESULTS_FILE = '.reassure/current.perf';
Expand All @@ -15,23 +17,23 @@ type MeasureOptions = {
commitHash?: string;
};

export function run(options: MeasureOptions) {
export async function run(options: MeasureOptions) {
const measurementType = options.baseline ? 'Baseline' : 'Current';

const metadata: PerformanceMetadata = {
branch: options?.branch ?? (await getGitBranch()),
commitHash: options?.commitHash ?? (await getGitCommitHash()),
};

console.log(`\n❇️ Running performance tests:`);
console.log(` - ${measurementType}: ${formatMetadata(options)}\n`);
console.log(` - ${measurementType}: ${formatMetadata(metadata)}\n`);

mkdirSync(RESULTS_DIRECTORY, { recursive: true });

const outputFile = options.baseline ? BASELINE_FILE : RESULTS_FILE;
rmSync(outputFile, { force: true });

const header = {
metadata: {
branch: options.branch,
commitHash: options.commitHash,
},
};

const header = { metadata };
writeFileSync(outputFile, JSON.stringify(header) + '\n');

const testRunnerPath = process.env.TEST_RUNNER_PATH ?? 'node_modules/.bin/jest';
Expand Down Expand Up @@ -102,7 +104,7 @@ export const command: CommandModule<{}, MeasureOptions> = {
type: 'string',
describe: 'Branch name of current code to be included in the report',
})
.option('commitHash', {
.option('commit-hash', {
type: 'string',
describe: 'Commit hash of current code to be included in the report',
});
Expand Down
4 changes: 2 additions & 2 deletions packages/reassure/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -184,12 +184,12 @@ git fetch origin
# Gather baseline perf measurements
git switch "$BASELINE_BRANCH"
yarn install --force
yarn reassure --baseline --branch $(git branch --show-current) --commitHash $(git rev-parse HEAD)
yarn reassure --baseline

# Gather current perf measurements & compare results
git switch --detach -
yarn install --force
yarn reassure --branch $(git branch --show-current) --commitHash $(git rev-parse HEAD)
yarn reassure
```

### CI integration
Expand Down
21 changes: 21 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1753,6 +1753,18 @@
"@jridgewell/resolve-uri" "^3.0.3"
"@jridgewell/sourcemap-codec" "^1.4.10"

"@kwsites/file-exists@^1.1.1":
version "1.1.1"
resolved "https://registry.yarnpkg.com/@kwsites/file-exists/-/file-exists-1.1.1.tgz#ad1efcac13e1987d8dbaf235ef3be5b0d96faa99"
integrity sha512-m9/5YGR18lIwxSFDwfE3oA7bWuq9kdau6ugN4H2rJeyhFQZcG9AgSHkQtSD15a8WvTgfz9aikZMrKPHvbpqFiw==
dependencies:
debug "^4.1.1"

"@kwsites/promise-deferred@^1.1.1":
version "1.1.1"
resolved "https://registry.yarnpkg.com/@kwsites/promise-deferred/-/promise-deferred-1.1.1.tgz#8ace5259254426ccef57f3175bc64ed7095ed919"
integrity sha512-GaHYm+c0O9MjZRu0ongGBRbinu8gVAMd2UZjji6jVmqKtZluZnptXGWhz1E8j8D2HJ3f/yMxKAUC0b+57wncIw==

"@manypkg/find-root@^1.1.0":
version "1.1.0"
resolved "https://registry.yarnpkg.com/@manypkg/find-root/-/find-root-1.1.0.tgz#a62d8ed1cd7e7d4c11d9d52a8397460b5d4ad29f"
Expand Down Expand Up @@ -8084,6 +8096,15 @@ signal-exit@^3.0.0, signal-exit@^3.0.2, signal-exit@^3.0.3, signal-exit@^3.0.7:
resolved "https://registry.yarnpkg.com/signal-exit/-/signal-exit-3.0.7.tgz#a9a1767f8af84155114eaabd73f99273c8f59ad9"
integrity sha512-wnD2ZE+l+SPC/uoS0vXeE9L1+0wuaMqKlfz9AMUo38JsyLSBWSFcHR1Rri62LZc12vLr1gb3jl7iwQhgwpAbGQ==

simple-git@^3.14.1:
version "3.14.1"
resolved "https://registry.yarnpkg.com/simple-git/-/simple-git-3.14.1.tgz#68018a5f168f8a568862e30b692004b37c3b5ced"
integrity sha512-1ThF4PamK9wBORVGMK9HK5si4zoGS2GpRO7tkAFObA4FZv6dKaCVHLQT+8zlgiBm6K2h+wEU9yOaFCu/SR3OyA==
dependencies:
"@kwsites/file-exists" "^1.1.1"
"@kwsites/promise-deferred" "^1.1.1"
debug "^4.3.4"

sisteransi@^1.0.5:
version "1.0.5"
resolved "https://registry.yarnpkg.com/sisteransi/-/sisteransi-1.0.5.tgz#134d681297756437cc05ca01370d3a7a571075ed"
Expand Down