aboutsummaryrefslogtreecommitdiff
path: root/nexgb/examples/atoms
diff options
context:
space:
mode:
authorPřemysl Janouch <p@janouch.name>2018-09-08 16:54:17 +0200
committerPřemysl Janouch <p@janouch.name>2018-09-08 16:54:17 +0200
commit3173202cc1e08762c6e156a8fffd23269a5ddb2b (patch)
tree95c4a06f8384d41b15e9c22afac0a387de79dc51 /nexgb/examples/atoms
parent632b3ae494d45755525644fe5d04475c95aae364 (diff)
parent3906399e7c2a40fbaf355de572cf50a314083f64 (diff)
downloadhaven-3173202cc1e08762c6e156a8fffd23269a5ddb2b.tar.gz
haven-3173202cc1e08762c6e156a8fffd23269a5ddb2b.tar.xz
haven-3173202cc1e08762c6e156a8fffd23269a5ddb2b.zip
Merge aarzilli/xgb, branch xcb1.12 as nexgb
History has been linearized and rewritten to stay under the new subdirectory. I want to make changes incompatible to BurntSushi/xgb. The history begs for being thrown away entirely because of its quality and because it doesn't cover the Google period but it is still useful for copyright tracking.
Diffstat (limited to 'nexgb/examples/atoms')
-rw-r--r--nexgb/examples/atoms/.gitignore3
-rw-r--r--nexgb/examples/atoms/Makefile12
-rw-r--r--nexgb/examples/atoms/main.go91
3 files changed, 106 insertions, 0 deletions
diff --git a/nexgb/examples/atoms/.gitignore b/nexgb/examples/atoms/.gitignore
new file mode 100644
index 0000000..01e08a1
--- /dev/null
+++ b/nexgb/examples/atoms/.gitignore
@@ -0,0 +1,3 @@
+atoms
+*.info
+*.prof
diff --git a/nexgb/examples/atoms/Makefile b/nexgb/examples/atoms/Makefile
new file mode 100644
index 0000000..c192a37
--- /dev/null
+++ b/nexgb/examples/atoms/Makefile
@@ -0,0 +1,12 @@
+atoms:
+ go build
+
+test: atoms
+ ./atoms --requests 500000 --cpu 1 \
+ --cpuprof cpu1.prof --memprof mem1.prof > atoms1.info 2>&1
+ ./atoms --requests 500000 --cpu 2 \
+ --cpuprof cpu2.prof --memprof mem2.prof > atoms2.info 2>&1
+ ./atoms --requests 500000 --cpu 3 \
+ --cpuprof cpu3.prof --memprof mem3.prof > atoms3.info 2>&1
+ ./atoms --requests 500000 --cpu 6 \
+ --cpuprof cpu6.prof --memprof mem6.prof > atoms6.info 2>&1
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)
+}