Esempio n. 1
0
func main() {
	flag.Parse()
	rand.Seed(time.Now().UnixNano())
	var wg sync.WaitGroup

	memServer := fmt.Sprintf("%s:%d", *server, *port)

	log.Printf(" Connected to server %v", memServer)

	var c []*memcached.Client

	for j := 0; j < *threadCount; j++ {
		client, err := memcached.Connect("tcp", memServer)
		if err != nil {
			log.Printf(" Unable to connect to %v, error %v", memServer, err)
			return
		}

		c = append(c, client)
	}

	data := RandStringRunes(*size)
	now := time.Now()
	docPerThread := *documentCount / runtime.GOMAXPROCS(0)

	for j := 0; j < *threadCount; j++ {
		wg.Add(1)
		go func(offset int) {
			defer wg.Done()
			client := c[offset]

			for i := 0; i < docPerThread; i++ {
				docid := fmt.Sprintf("doc-%d", i+offset*docPerThread)
				res, err := client.Set(0, docid, 0, 0, []byte(data))
				if err != nil || res.Status != gomemcached.SUCCESS {
					log.Printf("Set failed. Error %v", err)
					return
				}

				for k := 0; k < *readRatio; k++ {
					res, err := client.Get(0, docid)
					if err != nil || res.Status != gomemcached.SUCCESS {
						log.Printf("Get failed. Error %v", err)
						return
					}
				}
			}

		}(j)
	}

	wg.Wait()
	elapsed := time.Since(now)
	ops := runtime.GOMAXPROCS(0) * docPerThread
	log.Printf("sets:%d, gets:%d time_taken:%v\n", ops, ops**readRatio, elapsed)

	//log.Printf("Get returned %v", res)
}
Esempio n. 2
0
File: http_api.go Progetto: 40a/cbfs
func proxyViewRequest(w http.ResponseWriter, req *http.Request,
	path string) {

	nodes := couchbase.Nodes()
	node := nodes[rand.Intn(len(nodes))]
	u, err := url.Parse(node.CouchAPIBase)
	if err != nil {
		w.WriteHeader(http.StatusBadGateway)
		return
	}

	u.Path = "/" + path
	u.RawQuery = req.URL.RawQuery

	client := &http.Client{
		Transport: TimeoutTransport(*viewTimeout),
	}

	res, err := client.Get(u.String())
	if err != nil {
		w.WriteHeader(http.StatusBadGateway)
		return
	}
	defer res.Body.Close()

	for k, vs := range res.Header {
		w.Header()[k] = vs
	}

	output := io.Writer(w)

	if canGzip(req) {
		w.Header().Set("Content-Encoding", "gzip")
		w.Header().Del("Content-Length")
		gz := gzip.NewWriter(w)
		defer gz.Close()
		output = gz
	}
	w.WriteHeader(res.StatusCode)

	io.Copy(output, res.Body)
}
Esempio n. 3
0
func main() {

	flag.Parse()
	var wg sync.WaitGroup

	runtime.GOMAXPROCS(4)

	memServer := fmt.Sprintf("%s:%d", *server, *port)

	log.Printf(" Connected to server %v", memServer)

	var c []*memcached.Client

	data := RandStringRunes(512)

	for j := 0; j < runtime.GOMAXPROCS(0); j++ {
		client, err := memcached.Connect("tcp", memServer)
		if err != nil {
			log.Printf(" Unable to connect to %v, error %v", memServer, err)
			return
		}

		c = append(c, client)
	}
	n := 10000000
	now := time.Now()
	for j := 0; j < runtime.GOMAXPROCS(0); j++ {
		wg.Add(1)
		go func(offset int) {
			defer wg.Done()
			client := c[offset]

			for i := 0; i < n; i++ {

				res, err := client.Set(0, "test"+string(i), 0, 0, []byte(data))
				if err != nil || res.Status != gomemcached.SUCCESS {
					log.Printf("Set failed. Error %v", err)
					return
				}
			}

			for {
				i := rand.Intn(1000000)
				res, err := client.Get(0, "test"+string(i))
				if err != nil || res.Status != gomemcached.SUCCESS {
					log.Printf("Get failed. Error %v", err)
					return
				}

				//                              log.Printf(" Time %v", elapsed)
			}
		}(j)
	}

	wg.Wait()
	elapsed := time.Since(now)
	ops := runtime.GOMAXPROCS(0) * n
	log.Printf("sets:%d, gets:%d time_taken:%v\n", ops, ops, elapsed)

	//log.Printf("Get returned %v", res)

}