Last active
August 29, 2015 14:01
-
-
Save crowsonkb/8423ca8378a7b4d0b126 to your computer and use it in GitHub Desktop.
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
package main | |
import ( | |
"crypto/sha256" | |
"flag" | |
"fmt" | |
"runtime" | |
"time" | |
) | |
var batchSize int64 | |
var sampleTime int64 | |
var threads int | |
func init() { | |
flag.Int64Var(&batchSize, "batch_size", 1000, "The batch size.") | |
flag.Int64Var(&sampleTime, "sample_time", 1000, "The sampling time (ms).") | |
flag.IntVar(&threads, "threads", 0, "The number of threads (0 = auto).") | |
} | |
func spin(ch chan<- int64) { | |
var arr [sha256.Size]byte | |
for { | |
for i := int64(0); i < batchSize; i++ { | |
arr = sha256.Sum256(arr[:]) | |
} | |
ch <- batchSize | |
} | |
} | |
func main() { | |
flag.Parse() | |
if threads == 0 { | |
threads = runtime.NumCPU() | |
} | |
runtime.GOMAXPROCS(threads) | |
chCount := make(chan int64, threads) | |
fmt.Printf("Starting %d thread(s).\n", threads) | |
tick := time.Tick(time.Duration(sampleTime) * time.Millisecond) | |
for i := 0; i < threads; i++ { | |
go spin(chCount) | |
} | |
var count int64 | |
var lastCount int64 | |
for { | |
select { | |
case n := <-chCount: | |
count += n | |
case <-tick: | |
fmt.Printf("%d khash/s\n", (count-lastCount)/sampleTime) | |
lastCount = count | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment