Skip to content

Instantly share code, notes, and snippets.

@husobee
Created December 9, 2015 13:15
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save husobee/e7613552a3c22fdb680b to your computer and use it in GitHub Desktop.
Save husobee/e7613552a3c22fdb680b to your computer and use it in GitHub Desktop.
bcrypt concurrency
package main
import (
"flag"
"fmt"
"time"
"golang.org/x/crypto/bcrypt"
)
var (
concurrency int
bufsize int
workerpool int
mechanism string
)
func init() {
// parse the command line flags
flag.IntVar(&concurrency, "c", 15, "concurrency to run bcrypts")
flag.StringVar(&mechanism, "m", "max_out", "mechanism")
flag.IntVar(&bufsize, "b", 15, "buffer size")
flag.IntVar(&workerpool, "w", 1, "worker pool size")
flag.Parse()
}
func main() {
// create a bcrypted password
passwords := make(chan string, bufsize)
hash, _ := bcrypt.GenerateFromPassword([]byte("super Secret!!!"), 10)
total := make(chan int)
count := 0
mainStart := time.Now()
go func() {
for {
count += <-total
}
}()
if mechanism != "max_out" {
for i := 0; i < workerpool; i++ {
go func() {
for p := range passwords {
bcrypt.CompareHashAndPassword(hash, []byte(p))
total <- 1
}
}()
}
}
// hashcompare a bunch of becrypted passwords
for i := 0; i < concurrency; i++ {
go func() {
// create anonymous goroutines up to number of concurrency
for {
// forever, make this goroutine just do
if mechanism == "max_out" {
bcrypt.CompareHashAndPassword(hash, []byte("some string"))
total <- 1
} else {
passwords <- "some string"
}
}
}()
}
time.Sleep(5 * time.Second)
fmt.Printf("number of compares: %d\n", count)
fmt.Printf("time taken: %v\n", time.Now().Sub(mainStart))
}
package main
import (
"testing"
"golang.org/x/crypto/bcrypt"
)
func BenchmarkSerial(b *testing.B) {
hash, _ := bcrypt.GenerateFromPassword([]byte("super Secret!!!"), 10)
for i := 0; i < b.N; i++ {
bcrypt.CompareHashAndPassword(hash, []byte("some string"))
}
}
func BenchmarkAsManyGoroutinesPossible(b *testing.B) {
var done = make(chan bool)
hash, _ := bcrypt.GenerateFromPassword([]byte("super Secret!!!"), 10)
for i := 0; i < b.N; i++ {
go func() {
bcrypt.CompareHashAndPassword(hash, []byte("some string"))
done <- true
}()
}
for i := 0; i < b.N; i++ {
<-done
}
}
func BenchmarkOneWorkersMaxBuffer(b *testing.B) {
BufferedChannel(b, b.N, 1)
}
func BenchmarkTwoWorkersMaxBuffer(b *testing.B) {
BufferedChannel(b, b.N, 2)
}
func BenchmarkThreeWorkersMaxBuffer(b *testing.B) {
BufferedChannel(b, b.N, 3)
}
func BenchmarkFourWorkersMaxBuffer(b *testing.B) {
BufferedChannel(b, b.N, 4)
}
func BenchmarkFiveWorkersMaxBuffer(b *testing.B) {
BufferedChannel(b, b.N, 5)
}
func BenchmarkOneWorkersBufferTenBuffer(b *testing.B) {
BufferedChannel(b, 10, 1)
}
func BenchmarkTwoWorkersBufferTenBuffer(b *testing.B) {
BufferedChannel(b, 10, 2)
}
func BenchmarkThreeWorkersBufferTenBuffer(b *testing.B) {
BufferedChannel(b, 10, 3)
}
func BenchmarkFourWorkersBufferTenBuffer(b *testing.B) {
BufferedChannel(b, 10, 4)
}
func BenchmarkFiveWorkersBufferTenBuffer(b *testing.B) {
BufferedChannel(b, 10, 5)
}
func BenchmarkOneWorkersBufferFiveBuffer(b *testing.B) {
BufferedChannel(b, 5, 1)
}
func BenchmarkTwoWorkersBufferFiveBuffer(b *testing.B) {
BufferedChannel(b, 5, 2)
}
func BenchmarkThreeWorkersBufferFiveBuffer(b *testing.B) {
BufferedChannel(b, 5, 3)
}
func BenchmarkFourWorkersBufferFiveBuffer(b *testing.B) {
BufferedChannel(b, 5, 4)
}
func BenchmarkFiveWorkersBufferFiveBuffer(b *testing.B) {
BufferedChannel(b, 5, 5)
}
func BenchmarkOneWorkersBufferTwoBuffer(b *testing.B) {
BufferedChannel(b, 2, 1)
}
func BenchmarkTwoWorkersBufferTwoBuffer(b *testing.B) {
BufferedChannel(b, 2, 2)
}
func BenchmarkThreeWorkersBufferTwoBuffer(b *testing.B) {
BufferedChannel(b, 2, 3)
}
func BenchmarkFourWorkersBufferTwoBuffer(b *testing.B) {
BufferedChannel(b, 2, 4)
}
func BenchmarkFiveWorkersBufferTwoBuffer(b *testing.B) {
BufferedChannel(b, 2, 5)
}
func BufferedChannel(b *testing.B, bufferSize, numWorkers int) {
var done = make(chan bool, b.N)
var password = make(chan string, bufferSize)
hash, _ := bcrypt.GenerateFromPassword([]byte("super Secret!!!"), 10)
for i := 0; i < numWorkers; i++ {
go func() {
for {
bcrypt.CompareHashAndPassword(hash, []byte(<-password))
done <- true
}
}()
}
for i := 0; i < b.N; i++ {
go func() {
password <- "super Secret!!!"
}()
}
for i := 0; i < b.N; i++ {
<-done
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment