Skip to content

Instantly share code, notes, and snippets.

@trickkiste
Created February 13, 2019 20:14
Show Gist options
  • Save trickkiste/8ddd7bf514fba2c1b57030b282b75df3 to your computer and use it in GitHub Desktop.
Save trickkiste/8ddd7bf514fba2c1b57030b282b75df3 to your computer and use it in GitHub Desktop.
package main
import (
"time"
"strconv"
"sync"
"fmt"
"log"
"os"
"bufio"
)
func main() {
scanner := bufio.NewScanner(os.Stdin)
iterations, _ := strconv.Atoi(os.Getenv("COUNT"))
var wg sync.WaitGroup
wg.Add((iterations))
go func() {
counter := 0
for scanner.Scan() {
//scanner.Text()
counter += 1
fmt.Fprintf(os.Stderr, "Received %d\n", counter)
wg.Done()
}
if err := scanner.Err(); err != nil {
log.Println(err)
}
}()
start := time.Now()
message := `{"topic":"default/test/benchmark","payload":`
for i := 0; i < iterations; i++ {
fmt.Printf("%s%d}\n", message, i+1)
}
wg.Wait()
stop := time.Now()
duration := stop.Sub(start)
m := `{"topic":"default/test/benchmark_result","payload":{"iterations":%d, "duration": "%s", "ops_per_sec": "%f", "duration_per_op":"%s"}}`
fmt.Printf(m + "\n", iterations, duration, float64(iterations) / duration.Seconds(), time.Duration(duration.Nanoseconds() / int64(iterations)))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment