Skip to content

Instantly share code, notes, and snippets.

@satoru-takeuchi
Created February 13, 2021 13:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save satoru-takeuchi/ec9850b22b11543cfcb600cefc1f679e to your computer and use it in GitHub Desktop.
Save satoru-takeuchi/ec9850b22b11543cfcb600cefc1f679e to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
"log"
"os"
"strconv"
"sync"
"time"
)
const (
nloop_for_estimation int64 = 1000000000
)
var nloop_per_msec int64
func estimate_nloop_per_msec() int64 {
start := time.Now()
var i int64
for i = 0; i < nloop_for_estimation; i++ {
}
end := time.Now()
return nloop_for_estimation * 1000000 / end.Sub(start).Nanoseconds()
}
func load() {
var i int64
for i = 0; i < nloop_per_msec; i++ {
}
}
func main() {
if len(os.Args) < 3 {
fmt.Fprintf(os.Stderr, "usage: %s <nproc> <total>\n", os.Args[0])
os.Exit(1)
}
nproc, err := strconv.Atoi(os.Args[1])
if err != nil {
log.Fatal(err)
}
total, err := strconv.Atoi(os.Args[2])
if err != nil {
log.Fatal(err)
}
if nproc < 1 {
fmt.Fprintf(os.Stderr, "<nproc>(%d) should be >= 1\n", nproc)
os.Exit(1)
}
if total < 1 {
fmt.Fprintf(os.Stderr, "<total>(%d) should be >= 1\n", total)
os.Exit(1)
}
fmt.Println("estimating the workload which takes just one milli-second...")
nloop_per_msec = estimate_nloop_per_msec()
fmt.Println("end estimation")
records := make([][]time.Time, nproc)
for i := 0; i < len(records); i++ {
records[i] = make([]time.Time, total)
}
var wg sync.WaitGroup
start := time.Now()
for i := 0; i < nproc; i++ {
wg.Add(1)
go func(record []time.Time) {
defer wg.Done()
for j := 0; j < len(record); j++ {
load()
record[j] = time.Now()
}
}(records[i])
}
wg.Wait()
for i := 0; i < nproc; i++ {
for j := 0; j < total; j++ {
fmt.Printf("%d\t%d\t%d\n", i, records[i][j].Sub(start).Milliseconds(), (j+1)*100/total)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment