aboutsummaryrefslogtreecommitdiff
path: root/nexgb/examples/atoms/main.go
diff options
context:
space:
mode:
authorAndrew Gallant (Ocelot) <Andrew.Gallant@tufts.edu>2012-06-02 12:59:44 -0400
committerPřemysl Janouch <p@janouch.name>2018-09-08 16:49:13 +0200
commit8ee0ea98997eaa967f862da029cc04e5ae18b90e (patch)
tree20d78e4c78ad17f4de4369fa277b68470fab8249 /nexgb/examples/atoms/main.go
parente960f4d34effe41b3d0ff327001f7df833f69cba (diff)
downloadhaven-8ee0ea98997eaa967f862da029cc04e5ae18b90e.tar.gz
haven-8ee0ea98997eaa967f862da029cc04e5ae18b90e.tar.xz
haven-8ee0ea98997eaa967f862da029cc04e5ae18b90e.zip
A pathological example for profiling purposes. Use 'make test'.
Diffstat (limited to 'nexgb/examples/atoms/main.go')
-rw-r--r--nexgb/examples/atoms/main.go91
1 files changed, 91 insertions, 0 deletions
diff --git a/nexgb/examples/atoms/main.go b/nexgb/examples/atoms/main.go
new file mode 100644
index 0000000..8985768
--- /dev/null
+++ b/nexgb/examples/atoms/main.go
@@ -0,0 +1,91 @@
+package main
+
+import (
+ "flag"
+ "fmt"
+ "log"
+ "os"
+ "runtime"
+ "runtime/pprof"
+ "time"
+
+ "github.com/BurntSushi/xgb"
+ "github.com/BurntSushi/xgb/xproto"
+)
+
+var (
+ flagRequests int
+ flagGOMAXPROCS int
+ flagCpuProfName string
+ flagMemProfName string
+)
+
+func init() {
+ flag.IntVar(&flagRequests, "requests", 100000, "Number of atoms to intern.")
+ flag.IntVar(&flagGOMAXPROCS, "cpu", 1, "Value of GOMAXPROCS.")
+ flag.StringVar(&flagCpuProfName, "cpuprof", "cpu.prof",
+ "Name of CPU profile file.")
+ flag.StringVar(&flagMemProfName, "memprof", "mem.prof",
+ "Name of memory profile file.")
+
+ flag.Parse()
+
+ runtime.GOMAXPROCS(flagGOMAXPROCS)
+}
+
+func seqNames(n int) []string {
+ names := make([]string, n)
+ for i := range names {
+ names[i] = fmt.Sprintf("NAME%d", i)
+ }
+ return names
+}
+
+func main() {
+ X, err := xgb.NewConn()
+ if err != nil {
+ log.Fatal(err)
+ }
+
+ names := seqNames(flagRequests)
+
+ fcpu, err := os.Create(flagCpuProfName)
+ if err != nil {
+ log.Fatal(err)
+ }
+ defer fcpu.Close()
+ pprof.StartCPUProfile(fcpu)
+ defer pprof.StopCPUProfile()
+
+ start := time.Now()
+ cookies := make([]xproto.InternAtomCookie, flagRequests)
+ for i := 0; i < flagRequests; i++ {
+ cookies[i] = xproto.InternAtom(X,
+ false, uint16(len(names[i])), names[i])
+ }
+ for _, cookie := range cookies {
+ cookie.Reply()
+ }
+ fmt.Printf("Exec time: %s\n\n", time.Since(start))
+
+ fmem, err := os.Create(flagMemProfName)
+ if err != nil {
+ log.Fatal(err)
+ }
+ defer fmem.Close()
+ pprof.WriteHeapProfile(fmem)
+
+ memStats := &runtime.MemStats{}
+ runtime.ReadMemStats(memStats)
+
+ // This isn't right. I'm not sure what's wrong.
+ lastGcTime := time.Unix(int64(memStats.LastGC/1000000000),
+ int64(memStats.LastGC-memStats.LastGC/1000000000))
+
+ fmt.Printf("Alloc: %d\n", memStats.Alloc)
+ fmt.Printf("TotalAlloc: %d\n", memStats.TotalAlloc)
+ fmt.Printf("LastGC: %s\n", lastGcTime)
+ fmt.Printf("PauseTotalNs: %d\n", memStats.PauseTotalNs)
+ fmt.Printf("PauseNs: %d\n", memStats.PauseNs)
+ fmt.Printf("NumGC: %d\n", memStats.NumGC)
+}