Skip to content

Commit

Permalink
Added benchmarking
Browse files Browse the repository at this point in the history
  • Loading branch information
brittonhayes committed Dec 8, 2020
1 parent a3553b2 commit 729344a
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 0 deletions.
18 changes: 18 additions & 0 deletions .github/workflows/benchmark.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
name: benchmark
on:
push:
branches:
- master
- main
pull_request:
jobs:
benchmark:
name: Run benchmarks
runs-on: ${{ matrix.os }}
strategy:
matrix:
os: [ macos-latest, ubuntu-latest, windows-latest ]
steps:
- uses: actions/checkout@v2
- name: Run benchmarks
run: go test -bench . -benchmem ./...
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

[![Go Report Card](https://goreportcard.com/badge/github.com/brittonhayes/pillager)](https://goreportcard.com/report/github.com/brittonhayes/pillager)

![Tests](https://github.com/brittonhayes/pillager/workflows/test/badge.svg)

![Latest Release](https://img.shields.io/github/v/release/brittonhayes/pillager?label=latest%20release)

## Table of Contents
Expand Down
85 changes: 85 additions & 0 deletions benchmarks_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
package main_test

import (
"github.com/brittonhayes/pillager/pkg/hunter"
"github.com/spf13/afero"
"log"
"os"
"testing"
)

// A benchmark of the Hunter Load Rules method
func BenchmarkHunterLoadRules(b *testing.B) {
for n := 0; n < b.N; n++ {
hunter.LoadRules("")
}
}

// A benchmark of the Hound Fetch method which
// prints results out in desired format
func BenchmarkHunterHoundFetch(b *testing.B) {
b.StopTimer()
h := hunter.NewHound(&hunter.Config{
System: afero.NewMemMapFs(),
Rules: hunter.LoadRules(""),
Format: hunter.JSONFormat,
})
h.Findings = []hunter.Finding{
{
Count: 1,
Message: "Found something juicy",
Path: "example.toml",
Loot: []string{"Token 1234560"},
},
}

b.StartTimer()
for n := 0; n < b.N; n++ {
defer quiet()()
h.Fetch()
}
}

// A benchmark of the Hunter Inspect method
func BenchmarkHunterInspect(b *testing.B) {
b.StopTimer()
fs := afero.NewMemMapFs()
f, err := fs.Create("fake.toml")
if err != nil {
panic(err)
}
defer f.Close()
_, err = f.Write([]byte(`[email protected]`))
if err != nil {
panic(err)
}

h := hunter.NewHunter(&hunter.Config{
System: fs,
Rules: hunter.LoadRules(""),
BasePath: ".",
Verbose: true,
Format: hunter.JSONFormat,
})

b.StartTimer()
for n := 0; n < b.N; n++ {
defer quiet()()
h.Inspect(f.Name(), h.Config.System)
}
}

func quiet() func() {
null, _ := os.Open(os.DevNull)
sout := os.Stdout
serr := os.Stderr
os.Stdout = null
os.Stderr = null
log.SetOutput(null)
return func() {
defer null.Close()
os.Stdout = sout
os.Stderr = serr
log.SetOutput(os.Stderr)
}
}

0 comments on commit 729344a

Please sign in to comment.