Skip to content

Commit 15b0628

Browse files
committed
better organization
1 parent 89af8c3 commit 15b0628

8 files changed

Lines changed: 941 additions & 1039 deletions

src/addBenchmarkEntry.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ export function addBenchmarkEntry(
2121
} else {
2222
const suites = entries[benchName];
2323

24-
// Use git-graph aware logic to find previous benchmark
24+
// Find previous benchmark using git ancestry
2525
core.debug(`Finding previous benchmark for commit: ${benchEntry.commit.id}`);
2626

2727
prevBench = gitAnalyzer.findPreviousBenchmark(suites, benchEntry.commit.id);

src/config.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import * as os from 'os';
44
import * as path from 'path';
55

66
export type ToolType = typeof VALID_TOOLS[number];
7+
78
export interface Config {
89
name: string;
910
tool: ToolType;

src/default_index_html.ts

Lines changed: 2 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -127,19 +127,6 @@ export const DEFAULT_INDEX_HTML = String.raw`<!DOCTYPE html>
127127
};
128128
129129
function init() {
130-
// By default, we use the order provided by the JSON data (which is sorted by git topology)
131-
// We disable client-side timestamp sorting to avoid breaking the topological order
132-
// Future work: make this configurable via a JSON flag
133-
function sortEntriesByTimestamp(entries) {
134-
// Sort benchmarks by commit timestamp instead of execution time
135-
// This provides better git graph ordering for visualization
136-
return [...entries].sort((a, b) => {
137-
const timestampA = new Date(a.commit.timestamp).getTime();
138-
const timestampB = new Date(b.commit.timestamp).getTime();
139-
return timestampA - timestampB;
140-
});
141-
}
142-
143130
function collectBenchesPerTestCase(entries) {
144131
const map = new Map();
145132
for (const entry of entries) {
@@ -154,17 +141,6 @@ export const DEFAULT_INDEX_HTML = String.raw`<!DOCTYPE html>
154141
}
155142
}
156143
}
157-
// Sort each benchmark's data points by commit timestamp to ensure consistent ordering
158-
// DISABLED: strictly use server-side order
159-
/*
160-
for (const [benchName, arr] of map.entries()) {
161-
arr.sort((a, b) => {
162-
const timestampA = new Date(a.commit.timestamp).getTime();
163-
const timestampB = new Date(b.commit.timestamp).getTime();
164-
return timestampA - timestampB;
165-
});
166-
}
167-
*/
168144
return map;
169145
}
170146
@@ -185,14 +161,12 @@ export const DEFAULT_INDEX_HTML = String.raw`<!DOCTYPE html>
185161
a.click();
186162
};
187163
188-
// Prepare data points for charts
164+
// Prepare data points for charts (uses server-side ordering)
189165
return Object.keys(data.entries).map(name => {
190166
const entries = data.entries[name];
191-
// const sortedEntries = sortEntriesByTimestamp(entries);
192-
const sortedEntries = entries;
193167
return {
194168
name,
195-
dataSet: collectBenchesPerTestCase(sortedEntries),
169+
dataSet: collectBenchesPerTestCase(entries),
196170
};
197171
});
198172
}

src/gitGraph.ts

Lines changed: 10 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import * as github from '@actions/github';
21
import { execSync } from 'child_process';
32
import * as core from '@actions/core';
43
import { Benchmark } from './extract';
@@ -16,46 +15,16 @@ export class GitGraphAnalyzer {
1615
}
1716

1817
/**
19-
* Get current branch from GitHub context
18+
* Check if git CLI is available
2019
*/
21-
getCurrentBranch(): string {
22-
const context = github.context;
23-
24-
// For pull requests, get the head branch
25-
if (context.payload.pull_request) {
26-
return context.payload.pull_request.head.ref;
27-
}
28-
29-
// Try to get branch from git CLI first if available
30-
if (this.gitCliAvailable) {
31-
try {
32-
const branch = execSync('git rev-parse --abbrev-ref HEAD', {
33-
encoding: 'utf8',
34-
cwd: process.env.GITHUB_WORKSPACE ?? process.cwd(),
35-
}).trim();
36-
37-
if (branch && branch !== 'HEAD') {
38-
return branch;
39-
}
40-
} catch (e) {
41-
core.debug(`Failed to get branch from git CLI: ${e}`);
42-
}
43-
}
44-
45-
// For pushes, get the branch from ref
46-
if (context.ref) {
47-
// Remove 'refs/heads/' prefix if present
48-
return context.ref.replace('refs/heads/', '');
49-
}
50-
51-
// Fallback to 'main' if we can't determine branch
52-
return 'main';
20+
isGitAvailable(): boolean {
21+
return this.gitCliAvailable;
5322
}
5423

5524
/**
56-
* Get git ancestry using topological order (only works in GitHub Actions environment)
25+
* Get git ancestry using topological order
5726
*/
58-
getBranchAncestry(ref: string): string[] {
27+
getAncestry(ref: string): string[] {
5928
if (!this.gitCliAvailable) {
6029
core.warning('Git CLI not available, cannot determine ancestry');
6130
return [];
@@ -78,10 +47,11 @@ export class GitGraphAnalyzer {
7847
}
7948

8049
/**
81-
* Find previous benchmark commit based on git graph structure
50+
* Find previous benchmark commit based on git ancestry.
51+
* Falls back to execution time ordering if git ancestry is not available.
8252
*/
8353
findPreviousBenchmark(suites: Benchmark[], currentSha: string): Benchmark | null {
84-
const ancestry = this.getBranchAncestry(currentSha);
54+
const ancestry = this.getAncestry(currentSha);
8555

8656
if (ancestry.length === 0) {
8757
core.warning(`No ancestry found for commit ${currentSha}, falling back to execution time ordering`);
@@ -111,37 +81,16 @@ export class GitGraphAnalyzer {
11181
return null;
11282
}
11383

114-
/**
115-
* Sort benchmark data by commit timestamp (for GitHub Pages visualization)
116-
* This doesn't need git CLI - just uses the commit timestamps already stored
117-
*/
118-
sortByGitOrder(suites: Benchmark[]): Benchmark[] {
119-
if (suites.length === 0) return suites;
120-
121-
// For GitHub Pages, we don't have git CLI, so sort by commit timestamp
122-
// This gives a reasonable approximation of git order
123-
const sortedSuites = [...suites].sort((a, b) => {
124-
const timestampA = new Date(a.commit.timestamp ?? '1970-01-01T00:00:00Z').getTime();
125-
const timestampB = new Date(b.commit.timestamp ?? '1970-01-01T00:00:00Z').getTime();
126-
return timestampA - timestampB;
127-
});
128-
129-
core.debug('Sorted benchmarks by commit timestamp (GitHub Pages mode)');
130-
return sortedSuites;
131-
}
132-
13384
/**
13485
* Find the insertion index for a new benchmark entry based on git ancestry.
135-
* Returns the index after which the new entry should be inserted.
136-
* If no ancestor is found, returns -1 (insert at beginning) or suites.length (append to end).
86+
* Inserts after the most recent ancestor in the existing suites.
13787
*/
13888
findInsertionIndex(suites: Benchmark[], newCommitSha: string): number {
13989
if (!this.gitCliAvailable || suites.length === 0) {
140-
// Fallback: append to end
14190
return suites.length;
14291
}
14392

144-
const ancestry = this.getBranchAncestry(newCommitSha);
93+
const ancestry = this.getAncestry(newCommitSha);
14594
if (ancestry.length === 0) {
14695
core.debug('No ancestry found, appending to end');
14796
return suites.length;

src/write.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -490,7 +490,7 @@ async function writeBenchmarkToExternalJson(
490490
jsonFilePath: string,
491491
config: Config,
492492
): Promise<{ prevBench: Benchmark | null; normalizedCurrentBench: Benchmark }> {
493-
const { name, maxItemsInChart, saveDataFile } = config;
493+
const { name, saveDataFile, maxItemsInChart } = config;
494494
const data = await loadDataJson(jsonFilePath);
495495
const { prevBench, normalizedCurrentBench } = addBenchmarkToDataJson(name, bench, data, maxItemsInChart);
496496

test/addBenchmarkEntryGitGraph.test.ts

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
// Mock modules before imports
22
const mockDebug = jest.fn();
3-
const mockGetCurrentBranch = jest.fn();
43
const mockFindPreviousBenchmark = jest.fn();
54
const mockFindInsertionIndex = jest.fn();
65

@@ -10,7 +9,6 @@ jest.mock('@actions/core', () => ({
109

1110
jest.mock('../src/gitGraph', () => ({
1211
GitGraphAnalyzer: jest.fn().mockImplementation(() => ({
13-
getCurrentBranch: mockGetCurrentBranch,
1412
findPreviousBenchmark: mockFindPreviousBenchmark,
1513
findInsertionIndex: mockFindInsertionIndex,
1614
})),
@@ -52,16 +50,12 @@ describe('addBenchmarkEntry with Git Graph', () => {
5250
[benchName]: [existingEntry],
5351
};
5452

55-
mockGetCurrentBranch.mockReturnValue('feature-branch');
5653
mockFindPreviousBenchmark.mockReturnValue(existingEntry);
5754
mockFindInsertionIndex.mockReturnValue(1);
5855

5956
const result = addBenchmarkEntry(benchName, benchEntry, entries, null);
6057

61-
expect(mockFindPreviousBenchmark).toHaveBeenCalledWith(
62-
expect.arrayContaining([existingEntry]),
63-
'abc123',
64-
);
58+
expect(mockFindPreviousBenchmark).toHaveBeenCalledWith(expect.arrayContaining([existingEntry]), 'abc123');
6559
expect(mockDebug).toHaveBeenCalledWith('Finding previous benchmark for commit: abc123');
6660
expect(mockDebug).toHaveBeenCalledWith('Found previous benchmark: def456');
6761
expect(result.prevBench).toBe(existingEntry);
@@ -75,7 +69,6 @@ describe('addBenchmarkEntry with Git Graph', () => {
7569
[benchName]: [existingEntry],
7670
};
7771

78-
mockGetCurrentBranch.mockReturnValue('main');
7972
mockFindPreviousBenchmark.mockReturnValue(null);
8073
mockFindInsertionIndex.mockReturnValue(1);
8174

@@ -91,7 +84,6 @@ describe('addBenchmarkEntry with Git Graph', () => {
9184
const benchEntry = createMockBenchmark('abc123');
9285
const entries: any = {};
9386

94-
mockGetCurrentBranch.mockReturnValue('main');
9587
mockFindPreviousBenchmark.mockReturnValue(null);
9688

9789
const result = addBenchmarkEntry(benchName, benchEntry, entries, null);
@@ -112,7 +104,6 @@ describe('addBenchmarkEntry with Git Graph', () => {
112104
[benchName]: [existingEntry],
113105
};
114106

115-
mockGetCurrentBranch.mockReturnValue('main');
116107
mockFindPreviousBenchmark.mockReturnValue(existingEntry);
117108
mockFindInsertionIndex.mockReturnValue(1);
118109

@@ -136,7 +127,6 @@ describe('addBenchmarkEntry with Git Graph', () => {
136127
[benchName]: oldEntries,
137128
};
138129

139-
mockGetCurrentBranch.mockReturnValue('main');
140130
mockFindPreviousBenchmark.mockReturnValue(oldEntries[oldEntries.length - 1]);
141131
mockFindInsertionIndex.mockReturnValue(3);
142132

@@ -168,7 +158,6 @@ describe('addBenchmarkEntry with Git Graph', () => {
168158
[benchName]: oldEntries,
169159
};
170160

171-
mockGetCurrentBranch.mockReturnValue('main');
172161
mockFindPreviousBenchmark.mockReturnValue(oldEntries[0]);
173162
mockFindInsertionIndex.mockReturnValue(1);
174163

0 commit comments

Comments
 (0)