Esempio n. 1
0
func main() {

	//mc := memcache.New("192.168.99.100:11211")
	//fmt.Printf("mc: %T\n", mc)
	mydb := memflash.DB{MEM_IP + MEM_PORT, "", nil}

	ref := mydb.New()

	// 	ref.Set(&memcache.Item{Key: "foo", Value: []byte(`{
	// "index": "1",
	// "index_start_at": "56",
	// "integer": "34",
	// "float": "17.2187",
	// "name": "Maxine",
	// "surname": "Chandler",
	// "fullname": "Harvey O",
	// "email": "*****@*****.**",
	// "bool": "true"
	// }`)})

	item, _ := ref.Get("0")
	if item != nil {
		fmt.Println("Result:", string(item.Value))
	}

}
Esempio n. 2
0
func main() {
	in := ""

	prefix := "MemFlash."
	stats := statsd.NewStatsdClient(STATSD_IP+STATSD_PORT, prefix)
	err := stats.CreateSocket()
	check(err)
	//stats:=statsd.NewStatsdBuffer(1*time.Second, statsdclient)

	//mc := memcache.New("192.168.99.100:11211")
	//fmt.Printf("mc: %T\n", mc)
	mydb := memflash.DB{MEM_IP + MEM_PORT, "", nil}

	ref := mydb.New()

	session, err := mgo.Dial(MONGO_IP + MONGO_PORT)
	if err != nil {
		panic(err)
	}

	defer session.Close()

	session.SetMode(mgo.Monotonic, true)
	c := session.DB("test").C("people")

	result := Person{}
	// s1 := rand.NewSource(time.Now().UnixNano())
	// r1 := rand.New(s1)

	start := time.Now()

	for i := 0; i < 10000; i++ {
		memCh := make(chan bool)
		mongoCh := make(chan bool)
		timeout := make(chan bool, 1)
		fmt.Println("Iteration: ", i)

		//time.Sleep(5*time.Millisecond)

		start1 := time.Now()
		go func() {
			time.Sleep(TIMEOUT * time.Millisecond)
			timeout <- true
		}()

		go func(i int) {

			item, err := ref.Get(strconv.Itoa(i))
			if err == memcache.ErrCacheMiss {
				//Cache MISS
			}
			if item != nil {
				//HIT

				memCh <- true

			}

		}(i)

		go func(i int) {
			err = c.Find(bson.M{"index": strconv.Itoa(i)}).One(&result)
			check(err)
			//time.Sleep(time.Duration(r1.Intn(5)) * time.Millisecond)

			mongoCh <- true

		}(i)

		select {
		case <-memCh:
			fmt.Println("Memcache WINS")
			fmt.Printf("*** time %s \n ", time.Since(start1))
			stats.Gauge("p1", int64(time.Since(start1)))
			<-timeout
		case <-mongoCh:
			fmt.Println("MONGO wins")
			fmt.Printf("*** time %s \n ", time.Since(start1))
			stats.Gauge("p1", int64(time.Since(start1)))
			fmt.Println(int64(time.Since(start1)))
			<-timeout
		case <-timeout:
			fmt.Println("Slow connection")
			continue
		}

	}

	fmt.Printf("****** Executed in time %s \n ", time.Since(start))

	fmt.Scanf("%s", &in)

}
Esempio n. 3
0
func main() {
	in := ""
	memCh := make(chan bool)
	mongoCh := make(chan bool)
	timeout := make(chan bool, 1)
	prefix := "MemFlash."
	statsdclient := statsd.NewStatsdClient(STATSD_IP+STATSD_PORT, prefix)
	err := statsdclient.CreateSocket()
	check(err)

	//mc := memcache.New("192.168.99.100:11211")
	//fmt.Printf("mc: %T\n", mc)
	mydb := memflash.DB{MEM_IP + MEM_PORT, "", nil}

	ref := mydb.New()

	session, err := mgo.Dial(MONGO_IP + MONGO_PORT)
	if err != nil {
		panic(err)
	}

	defer session.Close()

	session.SetMode(mgo.Monotonic, true)
	c := session.DB("test").C("people")

	result := Person{}

	start := time.Now()

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

		time.Sleep(10 * time.Millisecond)

		start1 := time.Now()
		go func() {
			time.Sleep(3 * time.Second)
			timeout <- true
		}()

		go func(i int) {

			item, err := ref.Get(strconv.Itoa(i))
			if err == memcache.ErrCacheMiss {
				//fmt.Println("CACHE MISS!!!!!")
				// err = c.Find(bson.M{"index": strconv.Itoa(i)}).One(&result)
				// //fmt.Println(err)
				// check(err)

				// //log.Println("Mongo Result:", result.Data)
				// //fmt.Println("MONGO RES ~~~~~~~~~~~~~~")
				// mongoCh <- true
			}
			if item != nil {
				//log.Println("*******************  Mem Result:", string(item.Value))
				//fmt.Println("HIT")
				memCh <- true

			}

		}(i)

		go func(i int) {
			err = c.Find(bson.M{"index": strconv.Itoa(i)}).One(&result)
			//fmt.Println(err)
			check(err)

			//log.Println("Mongo Result:", result.Data)
			//fmt.Println("MONGO RES ~~~~~~~~~~~~~~")
			mongoCh <- true

		}(i)

		select {
		case <-memCh:
			fmt.Println("MEMCACHE WINS!!")
			fmt.Printf("* time %s \n ", time.Since(start1))
			statsdclient.Absolute("p11", int64(time.Since(start1)))
		case <-mongoCh:
			fmt.Println("MONGO wins xxxxxxxxxxxxxxxxxx")
			fmt.Printf("* time %s \n ", time.Since(start1))
			statsdclient.Absolute("p11", int64(time.Since(start1)))
		case <-timeout:
			fmt.Println("TOO SLOW------------------------------------------")
			continue

		}

	}

	fmt.Printf("****** Executed in time %s \n ", time.Since(start))

	fmt.Scanf("%s", &in)

}
Esempio n. 4
0
func main() {

	session, err := mgo.Dial("192.168.99.100:27017")
	if err != nil {
		panic(err)
	}

	defer session.Close()

	session.SetMode(mgo.Monotonic, true)

	mydb := memflash.DB{MEM_IP + MEM_PORT, "", nil}

	ref := mydb.New()

	ref.Set(&memcache.Item{Key: "foo", Value: []byte(`{
"index": "1",
"index_start_at": "56",
"integer": "34",
"float": "17.2187",
"name": "Maxine",
"surname": "Chandler",
"fullname": "Harvey O",
"email": "*****@*****.**",
"bool": "true"
}`)})

	s1 := rand.NewSource(time.Now().UnixNano())
	r1 := rand.New(s1)

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

		jsondata := fmt.Sprintf(
			`{
	"index": "%d",
	"index_start_at": "%d",
	"integer": "34",
	"float": "17.2187",
	"name": "%s",
	"surname": "Chandler",
	"fullname": "Harvey O",
	"email": "*****@*****.**",
	"bool": "true"
	}`, i, r1.Intn(100), rune(65+r1.Intn(26)))

		//fmt.Println(jsondata)
		fmt.Println("Inserting data for offset index: ", i)

		c := session.DB("test").C("people")
		err = c.Insert(&Person{strconv.Itoa(i), jsondata})
		if err != nil {
			log.Fatal(err)
		}

		if i%2 == 0 {

			ref.Set(&memcache.Item{Key: strconv.Itoa(i), Value: []byte(jsondata)})

		}

	}

}