Beispiel #1
0
func workerGetSetOrg(client *memcache_org.Client, wg *sync.WaitGroup, ch <-chan int, stats *Stats) {
	defer wg.Done()
	var item memcache_org.Item
	for _ = range ch {
		n := rand.Intn(*itemsCount)
		item.Key = fmt.Sprintf("%s_%d", key, n)
		startTime := time.Now()
		if rand.Float64() < *getRatio {
			_, err := client.Get(item.Key)
			if err == memcache_org.ErrCacheMiss {
				stats.cacheMissCount++
				continue
			}
			if err != nil {
				stats.errorsCount++
				continue
			}
			stats.cacheHitCount++
			updateResponseTimeHistogram(stats, startTime)
		} else {
			item.Value = value
			if err := client.Set(&item); err != nil {
				stats.errorsCount++
				continue
			}
			updateResponseTimeHistogram(stats, startTime)
		}
	}
}
Beispiel #2
0
func workerSetOrg(client *memcache_org.Client, wg *sync.WaitGroup, ch <-chan int, stats *Stats) {
	defer wg.Done()
	var item memcache_org.Item
	for _ = range ch {
		n := rand.Intn(*itemsCount)
		item.Key = fmt.Sprintf("%s_%d", key, n)
		item.Value = value
		startTime := time.Now()
		if err := client.Set(&item); err != nil {
			stats.errorsCount++
			continue
		}
		updateResponseTimeHistogram(stats, startTime)
	}
}
Beispiel #3
0
func setTestSingle(ch chan bool, mc *memcache.Client, cn, n int) {
	var err error
	v := make([]byte, dataSize)
	for i := 0; i < n; i++ {
		start := time.Now()
		err = mc.Set(&memcache.Item{Key: topicName, Value: v})
		if err != nil {
			log.Printf("set error: c%d %v", cn, err)
		} else {
			end := time.Now()
			duration := end.Sub(start).Seconds()
			log.Printf("set succ: %s spend: %.3fms", topicName, duration*1000)
		}
	}
	ch <- true
}
Beispiel #4
0
// Store the result of an API request in memcached.
func SetCachedRequest(m *memcache.Client, key string, value *ApiResult) {
	// Don't cache errors
	if value.Success == 0 {
		return
	}
	result_json, err := json.MarshalIndent(value, "", "  ")
	if err != nil {
		log.Println(err)
	}
	m.Set(
		&memcache.Item{
			Key:        key,
			Value:      result_json,
			Expiration: 600,
		},
	)
}
Beispiel #5
0
func precreateItemsOrg(client *memcache_org.Client) {
	n := *itemsCount / *workersCount
	workerFunc := func(wg *sync.WaitGroup, start int) {
		defer wg.Done()
		item := memcache_org.Item{
			Value: value,
		}
		for i := start; i < start+n; i++ {
			item.Key = fmt.Sprintf("%s_%d", key, i)
			if err := client.Set(&item); err != nil {
				log.Fatalf("Error in Client.Set(): [%s]", err)
			}
		}
	}

	var wg sync.WaitGroup
	defer wg.Wait()
	for i := 0; i < *workersCount; i++ {
		wg.Add(1)
		go workerFunc(&wg, i*n)
	}
}
Beispiel #6
0
func SavePaste(paste Paste, ren render.Render, r *http.Request, cf *swift.Connection, mc *memcache.Client) {
	paste.PasteId = genPasteId()
	payload, _ := json.Marshal(paste)
	seconds, err := getTTL(paste.PasteTtl)
	PanicIf(err)
	headers := swift.Headers{}
	now := time.Now()
	pasteIndex := 9999999999 - now.Unix()
	indexKey := fmt.Sprintf("cfpaste-%d", pasteIndex)
	headers["x-object-meta-pastetype"] = paste.PasteType
	headers["x-object-meta-pasteid"] = paste.PasteId
	headers["x-object-meta-pasteindex"] = fmt.Sprintf("%d", pasteIndex)
	if seconds != 0 {
		headers["x-delete-after"] = fmt.Sprintf("%d", seconds)
	}
	buf := bytes.NewBuffer(payload)
	_, err = cf.ObjectPut("go-cfpaste", paste.PasteId, buf, true, "", "application/json; charset=utf-8", headers)
	PanicIf(err)
	// gholt's listing index hack so that he can spy on pastes
	_, err = cf.ObjectPut("go-cfpaste", indexKey, bytes.NewBuffer([]byte("")), true, "", "application/json; charset=utf-8", headers)
	PanicIf(err)
	mc.Set(&memcache.Item{Key: paste.PasteId, Value: payload})
	ren.JSON(200, map[string]interface{}{"pasteid": paste.PasteId})
}