Beispiel #1
0
func Benchmark_Map_Get(b *testing.B) {
	tree := make(map[string]int)
	paths := make([]string, 0, count)
	for i := count; i > 0; i-- {
		path := fmt.String("/%d", i)
		paths = append(paths, path)
		tree[path] = i
	}

	l := len(paths)
	j := 0
	b.ReportAllocs()
	b.ResetTimer()

	for i := 0; i < b.N; i++ {
		_, ok := tree[paths[j]]
		if !ok {
			panic("should not happen")
		}
		j++
		if j >= l {
			j = 0
		}
	}
}
Beispiel #2
0
func (a *adminpanel) Start(ctx core.Context) error {
	http.HandleFunc("/admin", func(rsp http.ResponseWriter, req *http.Request) {
		ctx.Log().Print("got admin request!")
		fmt.Write(rsp, "core services\n")
		stats := core.ListServices()
		if len(stats) > 0 {
			fmt.Write(rsp, "Time           : %v\n\n", stats[0].Timestamp)
		}
		for _, s := range stats {
			fmt.Write(rsp, "Service Id     : %v\n", s.Id)
			fmt.Write(rsp, "Service Name   : %v\n", s.Name)
			fmt.Write(rsp, "Service Status : %v\n", s.Status)
			getcfg := core.NewGetConfig(s.Name)
			ctx.Bus().Post(core.ConfigServiceName, getcfg)
			cfg, _ := getcfg.Wait()
			fmt.Write(rsp, "Service Config :\n")
			keys := sortKeys(cfg.Keys())
			l := fmt.String("%v", maxlen(keys))
			format := "\t%-" + l + "v = %v\n"
			for _, key := range keys {
				val, _ := cfg.String(key)
				fmt.Write(rsp, format, key, val)
			}
			fmt.Write(rsp, "\n")
		}
	})
	return nil
}
Beispiel #3
0
func NewDebugIdGenerator(connId int64) chan string {
	msgIds := make(chan string, 64)
	id := 0
	go func() {
		for {
			id++
			msgIds <- fmt.String("@%d#%d", connId, id)
		}
	}()
	return msgIds
}
Beispiel #4
0
func (bus *Bus) startListener() (net.Listener, error) {
	bus.Lock()
	defer bus.Unlock()

	if bus.listener != nil {
		return nil, fmt.Error("Bus already listening")
	}

	l, err := net.Listen(bus.config.Net, fmt.String("%v:%v", bus.config.Address, bus.config.Port))
	if err != nil {
		return nil, fmt.Error("Error starting listener: %v", err)
	}
	bus.listener = l
	return l, nil
}
Beispiel #5
0
func (t *T) decorate(s string) string {
	_, file, line, ok := runtime.Caller(3)
	if ok {
		// Truncate file name at last file name separator.
		if index := strings.LastIndex(file, "/"); index >= 0 {
			file = file[index+1:]
		} else if index = strings.LastIndex(file, "\\"); index >= 0 {
			file = file[index+1:]
		}
	} else {
		file = "???"
		line = 1
	}

	return fmt.String("%s at \n%s:%d: assert #%d fact: %+v", s, file, line, t.counter, t.Fact)
}
Beispiel #6
0
func (bus *Bus) newwsClient(conn *ws.Conn) *Client {
	bus.Lock()
	defer bus.Unlock()

	id := bus.nextClientId()
	channel := fmt.String("/bus/clients/%d", id)
	client := &Client{
		id:        id,
		name:      channel,
		bus:       bus,
		transport: &wsTransport{conn},
		shutdown:  make(chan struct{}),
	}
	ch := &ClientHandler{channel: channel, bus: bus, client: client}
	r, _ := bus.router.Register(channel, ch)
	client.Registration = r
	return client
}
Beispiel #7
0
func Test_String(test *testing.T) {
	// this test is just to cheat on the coverage percentages for Tree.String()
	t := NewT(test)
	tree := NewTree()
	count := 0
	for i := 0; i < 3; i++ {
		for j := 3; j > 0; j-- {
			path := fmt.String("/%d/%d", i, j)
			tree.Put(path, i*10+j)
			count++
		}
	}
	tree.String()

	tree.EmitPaths(func(p string, h int) {
		count--
	})
	t.Equal(0, count)
	//t.Log(tree.String())
}
Beispiel #8
0
func Benchmark_Put(b *testing.B) {
	tree := NewTree()
	paths := make([]string, 0, count)
	for i := count; i >= 0; i-- {
		path := fmt.String("/%d", i)
		paths = append(paths, path)
	}

	l := len(paths)
	j := 0
	b.ReportAllocs()
	b.ResetTimer()
	for i := 0; i < b.N; i++ {
		tree.Put(paths[j], j)
		j++
		if j >= l {
			j = 0
		}
	}

}
Beispiel #9
0
func Benchmark_Map_Put(b *testing.B) {
	tree := make(map[string]int)
	paths := make([]string, 0, count)
	for i := count; i > 0; i-- {
		path := fmt.String("/%d", i)
		paths = append(paths, path)
	}

	l := len(paths)
	j := 0
	b.ReportAllocs()
	b.ResetTimer()

	for i := 0; i < b.N; i++ {
		tree[paths[j]] = j

		j++
		if j >= l {
			j = 0
		}
	}
}
Beispiel #10
0
func runclient() {
	origin := fmt.String("http://%v/", *addr)
	url := fmt.String("ws://%v:%v/jsonbus", *addr, *port)
	conn, err := ws.Dial(url, "", origin)
	if err != nil {
		fmt.Panic("Error connecting to server: %v", err)
	}

	defer conn.Close()
	err = ws.JSON.Send(conn, NewMessage("", "", &Hello{}))
	if err != nil {
		fmt.Panic("Error sending hello: " + err.Error())
	}

	msg := receive(conn)
	if msg == nil {
		return
	}

	if msg.Type != "HelloAck" {
		fmt.Panic("Protocol error, expected HelloAck")

	}

	ack := new(HelloAck)
	err = json.Unmarshal(msg.Body, ack)
	if err != nil {
		fmt.Panic(err.Error())
	}

	msgIds := GenerateMessageIds(ack.ConnId)
	shutdown := make(chan struct{})
	go func() {
		defer close(shutdown)
		for {
			if receive(conn) == nil {
				return
			}
		}
	}()

	if sub != nil && *sub {
		err := ws.JSON.Send(conn, NewMessage(<-msgIds, "", &Subscribe{Topic: *topic}))
		if err != nil {
			fmt.Panic("Error sending subscription: " + err.Error())
		}
	}

	if pub != nil && *pub {
		count := 1
		start := time.Now()
		spamcount := 100000
		if *spam {
			count = spamcount
		}

		for ; count > 0; count-- {
			err := ws.JSON.Send(conn, NewMessage(<-msgIds, "", &Publish{Topic: *topic, Message: *message}))
			if err != nil {
				fmt.Panic("Error sending subscription: " + err.Error())
			}
		}

		if *spam {
			d := time.Since(start)
			fmt.Printline("Spamming done %v in %v: %v/second.", spamcount, d, float64(spamcount)/d.Seconds())
		}

	}
	<-shutdown
}
Beispiel #11
0
func newLog(id int64) *log.Logger {
	prefix := fmt.String("[%3d] ", id)
	return log.New(os.Stderr, prefix, log.LstdFlags)
}
Beispiel #12
0
func (t *T) assert(b bool, format string, msg ...interface{}) {
	t.counter++
	if !b {
		t.Errorf(t.decorate(fmt.String(format, msg...)))
	}
}
Beispiel #13
0
func (args *argcfg) String() string {
	return fmt.String("%#v", args)
}