Esempio n. 1
0
func main() {

	flag.Parse()

	pool := jack.NewConnectionPool(*address, *port, *numClients)

	nc := int(*numClients)
	clients := []*jack.Connection{}
	for i := 0; i < nc; i++ {
		c, _ := pool.Connect()
		clients = append(clients, c)
	}

	var wg sync.WaitGroup
	work := 1000000 / nc
	startTime := time.Now().UnixNano()
	for i := 0; i < nc; i++ {
		wg.Add(1)
		go func(groupId int) {
			for j := 0; j < work; j++ {
				key := strconv.Itoa(groupId) + ":" + strconv.Itoa(j)
				clients[groupId].Set(key, "abc")
			}
			wg.Done()
		}(i)
	}
	wg.Wait()
	stopTime := time.Now().UnixNano()

	tme := float64(stopTime-startTime) / 1000000000
	fmt.Printf("1000000 SETS across %d clients: %.2f seconds\n", nc, tme)
}
Esempio n. 2
0
func main() {

	flag.Parse()

	pool := jack.NewConnectionPool(*address, *port, *poolsize)

	conn, err := pool.Connect()
	if err != nil {
		panic(err)
	}

	// handle termination signals
	sigs := make(chan os.Signal, 1)
	signal.Notify(sigs, os.Interrupt, os.Kill)
	go func() {
		<-sigs
		fmt.Println()
		os.Exit(0)
	}()

	cmdPrompt := "\033[34;1mjack " + *address + ":" + strconv.FormatUint(uint64(*port), 10) + ">\033[0m "
	syntaxError := "\033[31;1mSyntaxError: %s\033[0m\n"

	fmt.Printf("use help for more command.\n")
	var tokens []string
	for {
		fmt.Print(cmdPrompt)
		tokens = readLine()

		// handle empty string
		if len(tokens) == 0 {
			continue
		}

		cmd := strings.ToUpper(tokens[0])

		switch cmd {
		case "HELP":
			// help message for more command
			msgHelp := fmt.Sprintf("\tGET key [key ...]\n\tSET key value\n" +
				"\tDEL key [key ...]\n\tPUB key value\n\tSUB key [key ...]\n" +
				"\tQUIT")
			fmt.Println(msgHelp)

		case "GET":

			// handle syntax error
			if len(tokens) == 1 {
				fmt.Printf(syntaxError, "GET key [key ...]")
				continue
			}

			// get each key
			for _, key := range tokens[1:] {
				value := conn.Get(key)
				if len(value) > 0 {
					fmt.Printf("%s := %s\n", key, value)
				}
			}

		case "SET":

			// handle syntax error
			if len(tokens) != 3 {
				fmt.Printf(syntaxError, "SET key value")
				continue
			}

			// set key
			fmt.Println(conn.Set(tokens[1], tokens[2]))

		case "DEL":

			// handle syntax error
			if len(tokens) == 1 {
				fmt.Printf(syntaxError, "DEL key [key ...]")
				continue
			}

			// delete each key
			for _, key := range tokens[1:] {
				fmt.Println(conn.Delete(key))
			}

		case "PUB":

			// handle syntax error
			if len(tokens) != 3 {
				fmt.Printf(syntaxError, "PUB key value")
				continue
			}

			// publish key
			fmt.Println(conn.Publish(tokens[1], tokens[2]))

		case "SUB":

			// handle syntax error
			if len(tokens) == 1 {
				fmt.Printf(syntaxError, "SUB key [key ...]")
				continue
			}

			// subscribe to each key
			recv := make(chan string)
			for _, key := range tokens[1:] {
				go func(k string) {
					c, err := pool.Connect()
					if err != nil {
						panic(err)
					}
					middle := make(chan string)
					go c.Subscribe(k, middle)
					for {
						recv <- k + " := " + <-middle
					}
				}(key)
			}

			for {
				fmt.Println(<-recv)
			}

		case "QUIT":
			err := conn.Close()
			if err != nil {
				fmt.Printf("close error: %q", err)
			}
			os.Exit(0)

		case "":
			// skip empty line
			continue

		default:
			fmt.Printf(syntaxError, cmd)
		}
	}
}