-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a3553b2
commit 729344a
Showing
3 changed files
with
105 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ./... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |