Skip to content

Commit

Permalink
add Go support
Browse files Browse the repository at this point in the history
  • Loading branch information
rhysd committed Nov 9, 2019
1 parent e337320 commit 6425d89
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 8 deletions.
2 changes: 1 addition & 1 deletion action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ inputs:
required: true
default: 'Benchmark'
tool:
description: 'Tool to use get benchmark output. One of "cargo", ... (TODO)'
description: 'Tool to use get benchmark output. One of "cargo", "go"... (TODO)'
required: true
output-file-path:
description: 'A path to file which contains the benchmark output'
Expand Down
14 changes: 7 additions & 7 deletions config.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
import * as core from '@actions/core';
import {promises as fs, Stats} from 'fs';
import { promises as fs, Stats } from 'fs';
import * as os from 'os';
import * as path from 'path';

export type ToolType = 'cargo';
export type ToolType = 'cargo' | 'go';
export interface Config {
name: string,
name: string;
tool: ToolType;
outputFilePath: string;
ghPagesBranch: string;
benchmarkDataDirPath: string;
}

const VALID_TOOLS = ['cargo'];
const VALID_TOOLS: ToolType[] = ['cargo', 'go'];

function validateToolType(tool: string): asserts tool is ToolType {
if (VALID_TOOLS.includes(tool)) {
if ((VALID_TOOLS as string[]).includes(tool)) {
return;
}
throw new Error(`Invalid value '${tool}' for 'tool' input. It must be one of ${VALID_TOOLS}`);
Expand All @@ -36,7 +36,7 @@ async function statPath(p: string): Promise<[Stats, string]> {
p = resolvePath(p);
try {
return [await fs.stat(p), p];
} catch(e) {
} catch (e) {
throw new Error(`Cannot stat '${p}': ${e}`);
}
}
Expand All @@ -63,7 +63,7 @@ function validateGhPagesBranch(branch: string) {
function validateBenchmarkDataDirPath(dirPath: string): string {
try {
return resolvePath(dirPath);
} catch(e) {
} catch (e) {
throw new Error(`Invalid value for 'benchmark-data-dir-path': ${e}`);
}
}
Expand Down
8 changes: 8 additions & 0 deletions examples/go/fib.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package fib

func Fib(u uint) uint {
if u <= 1 {
return 1
}
return Fib(u-2) + Fib(u-1)
}
17 changes: 17 additions & 0 deletions examples/go/fib_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package fib

import (
"testing"
)

func BenchmarkFib10(b *testing.B) {
for i := 0; i < b.N; i++ {
var _ = Fib(10)
}
}

func BenchmarkFib20(b *testing.B) {
for i := 0; i < b.N; i++ {
var _ = Fib(20)
}
}
28 changes: 28 additions & 0 deletions extract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ export interface Benchmark {
function extractCargoResult(output: string): BenchmarkResult[] {
const lines = output.split('\n');
const ret = [];
// Example:
// test bench_fib_20 ... bench: 37,174 ns/iter (+/- 7,527)
const reExtract = /^test (\w+)\s+\.\.\. bench:\s+([0-9,]+) ns\/iter \(\+\/- ([0-9,]+)\)$/;
const reComma = /,/g;

Expand All @@ -61,6 +63,29 @@ function extractCargoResult(output: string): BenchmarkResult[] {
return ret;
}

function extractGoResult(output: string): BenchmarkResult[] {
const lines = output.split('\n');
const ret = [];
// Example:
// BenchmarkFib20-8 30000 41653 ns/op
const reExtract = /^(Benchmark\w+)\S*\s+\d+\s+(\d+)\s+(.+)$/;

for (const line of lines) {
const m = line.match(reExtract);
if (m === null) {
continue;
}

const name = m[1];
const value = parseInt(m[2], 10);
const unit = m[3];

ret.push({ name, value, unit });
}

return ret;
}

export async function extractResult(config: Config): Promise<Benchmark> {
const output = await fs.readFile(config.outputFilePath, 'utf8');
const { tool } = config;
Expand All @@ -70,6 +95,9 @@ export async function extractResult(config: Config): Promise<Benchmark> {
case 'cargo':
benches = extractCargoResult(output);
break;
case 'go':
benches = extractGoResult(output);
break;
default:
throw new Error(`FATAL: Unexpected tool: '${tool}'`);
}
Expand Down

0 comments on commit 6425d89

Please sign in to comment.