Skip to content

Commit

Permalink
Add benchmark for list topologies API.
Browse files Browse the repository at this point in the history
  • Loading branch information
tomwilkie committed Jan 25, 2016
1 parent b1a3a15 commit 470860e
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 24 deletions.
28 changes: 15 additions & 13 deletions app/api_topologies.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,23 +189,25 @@ func (r *registry) walk(f func(APITopologyDesc)) {
// makeTopologyList returns a handler that yields an APITopologyList.
func (r *registry) makeTopologyList(rep Reporter) func(w http.ResponseWriter, r *http.Request) {
return func(w http.ResponseWriter, req *http.Request) {
var (
rpt = rep.Report()
topologies = []APITopologyDesc{}
)
r.walk(func(desc APITopologyDesc) {
renderer := renderedForRequest(req, desc)
desc.Stats = decorateWithStats(rpt, renderer)
for i := range desc.SubTopologies {
renderer := renderedForRequest(req, desc.SubTopologies[i])
desc.SubTopologies[i].Stats = decorateWithStats(rpt, renderer)
}
topologies = append(topologies, desc)
})
topologies := r.renderTopologies(rep.Report(), req)
respondWith(w, http.StatusOK, topologies)
}
}

func (r *registry) renderTopologies(rpt report.Report, req *http.Request) []APITopologyDesc {
topologies := []APITopologyDesc{}
r.walk(func(desc APITopologyDesc) {
renderer := renderedForRequest(req, desc)
desc.Stats = decorateWithStats(rpt, renderer)
for i := range desc.SubTopologies {
renderer := renderedForRequest(req, desc.SubTopologies[i])
desc.SubTopologies[i].Stats = decorateWithStats(rpt, renderer)
}
topologies = append(topologies, desc)
})
return topologies
}

func decorateWithStats(rpt report.Report, renderer render.Renderer) topologyStats {
var (
nodes int
Expand Down
12 changes: 1 addition & 11 deletions render/render.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,42 +3,32 @@ package render
import (
"fmt"
"reflect"
"strings"

"github.com/bluele/gcache"

"github.com/weaveworks/scope/report"
)

var renderCache = gcache.New(100).LRU().Build()
var depth = 0

func memoisedRender(r Renderer, rpt report.Report) RenderableNodes {
fmt.Printf("%sRendering: %#v\n", strings.Repeat(" ", depth), r)

key := ""
v := reflect.ValueOf(r)
switch v.Kind() {
case reflect.Ptr, reflect.Func:
key = fmt.Sprintf("%s-%x", rpt.ID, v.Pointer())
default:
fmt.Printf("%s- Cannot memoise: %v\n", strings.Repeat(" ", depth), r)
return r.Render(rpt)
}
if result, err := renderCache.Get(key); err == nil {
fmt.Printf("%s- Hit %s\n", strings.Repeat(" ", depth), key)
return result.(RenderableNodes)
}
fmt.Printf("%s- Miss %s\n", strings.Repeat(" ", depth), key)

depth += 1
output := r.Render(rpt)
depth -= 1

renderCache.Set(key, output)
return output
}

// ResetCache blows away the rendered node cache.
func ResetCache() {
renderCache.Purge()
}
Expand Down

0 comments on commit 470860e

Please sign in to comment.