-
-
Save chipaca/579fdb70cf1293cc5309 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"log" | |
"os" | |
"path/filepath" | |
"runtime/pprof" | |
"sort" | |
"strconv" | |
"time" | |
"launchpad.net/snappy/dirs" | |
"launchpad.net/snappy/pkg" | |
"launchpad.net/snappy/pkg/husk" | |
"launchpad.net/snappy/snappy" | |
) | |
func init() { | |
ds, _ := filepath.Glob("/tmp/bnchroot*") | |
if len(ds) != 1 { | |
panic("missing or too many /tmp/bnchroot*") | |
} | |
dirs.SetRootDir(ds[0]) | |
} | |
type byQN []snappy.Part | |
func (ps byQN) Len() int { return len(ps) } | |
func (ps byQN) Swap(a, b int) { ps[a], ps[b] = ps[b], ps[a] } | |
func (ps byQN) Less(a, b int) bool { | |
return snappy.QualifiedName(ps[a]) < snappy.QualifiedName(ps[b]) | |
} | |
func activeNamesFromHusks() []string { | |
var names []string | |
for k, v := range husk.All() { | |
if v.ActiveIndex() > -1 { | |
names = append(names, k) | |
} | |
} | |
return names | |
} | |
func activeNamesFromParts() []string { | |
names, _ := snappy.ActiveSnapIterByType(snappy.FullName, pkg.TypeApp, pkg.TypeFramework) | |
return names | |
} | |
func mapFromHusk() map[string]map[string]string { | |
husks := husk.All() | |
results := make(map[string]map[string]string) | |
for k, v := range husks { | |
results[k] = v.Map(nil) | |
} | |
return results | |
} | |
// this is essentially parts2map from the trunk's daemon/api.go | |
func parts2map(parts []snappy.Part) map[string]string { | |
if len(parts) == 0 { | |
return nil | |
} | |
// TODO: handle multiple results in parts; set rollback_available; set update_available | |
part := parts[0] | |
var status string | |
if part.IsInstalled() { | |
if part.IsActive() { | |
status = "active" | |
} else { | |
// can't really happen | |
status = "installed" | |
} | |
} else { | |
status = "not installed" | |
} | |
result := map[string]string{ | |
"icon": part.Icon(), | |
"name": part.Name(), | |
"origin": part.Origin(), | |
"status": status, | |
"type": string(part.Type()), | |
"vendor": part.Vendor(), | |
"version": part.Version(), | |
"description": part.Description(), | |
"installed_size": strconv.FormatInt(part.InstalledSize(), 10), | |
"download_size": strconv.FormatInt(part.DownloadSize(), 10), | |
} | |
return result | |
} | |
// this is essentially trunk's daemon/api.go's getPackagesInfo | |
func mapFromPart() map[string]map[string]string { | |
repo := snappy.NewMetaLocalRepository() | |
found, err := repo.All() | |
if err != nil { | |
panic(err) | |
} | |
if len(found) == 0 { | |
panic("empty?") | |
} | |
sort.Sort(byQN(found)) | |
results := make(map[string]map[string]string) | |
var current []snappy.Part | |
var oldName string | |
var oldOrigin string | |
for i := range found { | |
name := found[i].Name() | |
origin := found[i].Origin() | |
if (name != oldName || origin != oldOrigin) && len(current) > 0 { | |
results[oldName+"."+oldOrigin] = parts2map(current) | |
current = nil | |
} | |
oldName = name | |
oldOrigin = origin | |
current = append(current, found[i]) | |
} | |
if len(current) > 0 { | |
results[oldName+"."+oldOrigin] = parts2map(current) | |
} | |
return results | |
} | |
func main() { | |
f, err := os.Create("/tmp/bnch.prof") | |
if err != nil { | |
panic(err) | |
} | |
tf := time.Now().Add(time.Second * 10) | |
pprof.StartCPUProfile(f) | |
for time.Now().Before(tf) { | |
if l := len(mapFromHusk()); l < 10 { | |
log.Fatalf("only %d husks?!?", l) | |
} | |
} | |
pprof.StopCPUProfile() | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import "testing" | |
func BenchmarkHusk(b *testing.B) { | |
for n := 0; n < b.N; n++ { | |
mapFromHusk() | |
} | |
} | |
func BenchmarkPart(b *testing.B) { | |
for n := 0; n < b.N; n++ { | |
mapFromPart() | |
} | |
} | |
func BenchmarkActiveHusk(b *testing.B) { | |
for n := 0; n < b.N; n++ { | |
activeNamesFromHusks() | |
} | |
} | |
func BenchmarkActivePart(b *testing.B) { | |
for n := 0; n < b.N; n++ { | |
activeNamesFromParts() | |
} | |
} | |
func TestEqualNames(t *testing.T) { | |
fromHusks := activeNamesFromHusks() | |
fromParts := activeNamesFromParts() | |
if len(fromHusks) != len(fromParts) { | |
t.Errorf("fromHusk != fromPart: %d vs %d", len(fromHusks), len(fromParts)) | |
} | |
hd := map[string]bool{} | |
for _, v := range fromHusks { | |
hd[v] = true | |
} | |
pd := map[string]bool{} | |
for _, v := range fromParts { | |
if !hd[v] { | |
t.Errorf("fromHusks missing %s", v) | |
} | |
pd[v] = true | |
} | |
for _, v := range fromHusks { | |
if !pd[v] { | |
t.Errorf("fromParts missing %s", v) | |
} | |
} | |
} | |
func TestEqualMap(t *testing.T) { | |
fromHusk := mapFromHusk() | |
fromPart := mapFromPart() | |
if len(fromHusk) < 10 { | |
t.Errorf("fromHusk too short: %d", len(fromHusk)) | |
} | |
if len(fromPart) < 10 { | |
t.Errorf("fromPart too short: %d", len(fromPart)) | |
} | |
for dk, hd := range fromHusk { | |
pd, ok := fromPart[dk] | |
if !ok { | |
t.Errorf("fromPart missing %s", dk) | |
continue | |
} | |
for k, v := range hd { | |
if pd[k] != v { | |
t.Errorf("in %s, %s: part: %s, husk: %s", | |
dk, k, pd[k], v) | |
} | |
} | |
} | |
for dk := range fromPart { | |
if _, ok := fromHusk[dk]; !ok { | |
t.Errorf("fromHusk missing %s", dk) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment