Last active
October 9, 2018 07:02
-
-
Save osamu/384537bb3ff59c0d6d7c5e6e77cc7c69 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 ( | |
"flag" | |
"github.com/bradfitz/gomemcache/memcache" | |
"log" | |
"math/rand" | |
"sync/atomic" | |
"time" | |
) | |
var nreq uint64 | |
func work(server string, n_bytes int, do_get bool, sleep int) { | |
mc := memcache.New(server) | |
mc.Timeout = 500 * time.Millisecond | |
v := make([]byte, n_bytes) | |
for { | |
rand.Read(v) | |
err := mc.Set(&memcache.Item{Key: "foo", Value: v}) | |
if err != nil { | |
log.Println("Set Error" + err.Error()) | |
return | |
} | |
atomic.AddUint64(&nreq, 1) | |
time.Sleep(time.Duration(rand.Intn(sleep)) * time.Millisecond) | |
if do_get { | |
_, err = mc.Get("foo") | |
if err != nil { | |
log.Println("Get Error" + err.Error()) | |
return | |
} | |
atomic.AddUint64(&nreq, 1) | |
} | |
} | |
} | |
func main() { | |
var server string | |
flag.StringVar(&server, "server", "10.3.112.3:11211", "Target memcached server") | |
n_workersPtr := flag.Int("nworker", 20000, "Number of worker") | |
n_bytesPtr := flag.Int("byes", 8000, "Size of Value to write") | |
do_getPtr := flag.Bool("writeon", true, "a bool for write or not") | |
sleepsecPtr := flag.Int("sleep", 1000, "How long sleep in worker") | |
flag.Parse() | |
go func() { | |
worker := make(chan bool, *n_workersPtr) | |
for { | |
worker <- true | |
go func() { | |
defer func() { <-worker }() | |
work(server, *n_bytesPtr, *do_getPtr, *sleepsecPtr) | |
}() | |
time.Sleep(time.Duration(rand.Intn(20)) * time.Millisecond) | |
} | |
}() | |
for { | |
t_now := time.Now() | |
t_now_counter := nreq | |
time.Sleep(time.Second) | |
log.Printf("Request per sec: %f", float64(nreq-t_now_counter)/time.Since(t_now).Seconds()) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment