Skip to content

Instantly share code, notes, and snippets.

@pedrofaria
Created March 4, 2020 07:31
Show Gist options
  • Save pedrofaria/fc70cc3aa64ddbb1bbdbe20a2d2fb165 to your computer and use it in GitHub Desktop.
Save pedrofaria/fc70cc3aa64ddbb1bbdbe20a2d2fb165 to your computer and use it in GitHub Desktop.
package main
import (
"testing"
)
type everyThing struct{}
func (e *everyThing) FuncA() bool {
return true
}
func (e *everyThing) FuncB() bool {
return true
}
func (e *everyThing) FuncC() bool {
return true
}
func (e *everyThing) FuncD() bool {
return true
}
func (e *everyThing) FuncE() bool {
return true
}
func (e *everyThing) FuncF() bool {
return true
}
type smaller interface {
FuncA() bool
FuncB() bool
}
type bigger interface {
FuncA() bool
FuncB() bool
FuncC() bool
FuncD() bool
FuncE() bool
FuncF() bool
}
type serviceA struct {
dep smaller
}
func (s *serviceA) Do() bool {
return s.dep.FuncA() && s.dep.FuncB()
}
type serviceB struct {
dep bigger
}
func (s *serviceB) Do() bool {
return s.dep.FuncA() && s.dep.FuncB()
}
type serviceC struct {
dep *everyThing
}
func (s *serviceC) Do() bool {
return s.dep.FuncA() && s.dep.FuncB()
}
func BenchmarkSmallInterface(b *testing.B) {
s := serviceA{
dep: &everyThing{},
}
for i := 0; i < b.N; i++ {
s.Do()
}
}
func BenchmarkBigInterface(b *testing.B) {
s := serviceB{
dep: &everyThing{},
}
for i := 0; i < b.N; i++ {
s.Do()
}
}
func BenchmarkNoInterface(b *testing.B) {
s := serviceC{
dep: &everyThing{},
}
for i := 0; i < b.N; i++ {
s.Do()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment