Created
July 9, 2019 12:37
-
-
Save ukautz/b3d0fcb983bdf73316e53a360706dfed to your computer and use it in GitHub Desktop.
Benchmark channel usage with struct values vs struct references
This file contains hidden or 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
package play | |
import "testing" | |
type ( | |
aDataType struct { | |
A string | |
B int | |
C []byte | |
} | |
) | |
func channelViaValue() { | |
c := make(chan aDataType) | |
go func() { | |
defer close(c) | |
for i := 0; i < 100000; i++ { | |
c <- aDataType{"A", i, []byte{'C'}} | |
} | |
}() | |
for range c { | |
} | |
} | |
func channelViaRef() { | |
c := make(chan *aDataType) | |
go func() { | |
defer close(c) | |
for i := 0; i < 100000; i++ { | |
c <- &aDataType{"A", i, []byte{'C'}} | |
} | |
}() | |
for range c { | |
} | |
} | |
func BenchmarkChannelViaValue(b *testing.B) { | |
for n := 0; n < b.N; n++ { | |
channelViaValue() | |
} | |
} | |
func BenchmarkChannelViaRef(b *testing.B) { | |
for n := 0; n < b.N; n++ { | |
channelViaRef() | |
} | |
} |
This file contains hidden or 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
$ go test -bench=. -benchtime=10s ⛅ !EXPIRED! s24-image-scaling | |
goos: linux | |
goarch: amd64 | |
pkg: github.com/Scout24/image-delivery-architecture-review/src/play | |
BenchmarkChannelViaValue-4 1000 21246004 ns/op | |
BenchmarkChannelViaRef-4 500 28882823 ns/op | |
PASS | |
ok github.com/Scout24/image-delivery-architecture-review/src/play 40.609s |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment