Guest User

Untitled

a guest
Dec 25th, 2017
916
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2. Compilation & execution instructions:
  3.  
  4. go get "github.com/satori/go.uuid"
  5. go build
  6. .\<binary name> -cpuprofile cpu.prof -memprofile mem.prof -resultcount 1000000
  7.  
  8. The -resultcount flag indicates how many elements to hold before
  9. clearing the collection. This is mainly useful in observing the overhead
  10. of a collection of objects.
  11. */
  12.  
  13. package main
  14.  
  15. import (
  16. "flag"
  17. "fmt"
  18. "github.com/satori/go.uuid"
  19. "log"
  20. "os"
  21. "runtime"
  22. "runtime/pprof"
  23. "time"
  24. )
  25.  
  26. type TcpEvent struct {
  27. Saddr int
  28. Daddr int
  29. Sport int
  30. Dport int
  31. Timestamp time.Time
  32. ProviderGuid uuid.UUID
  33. ProviderName string
  34. EventId int
  35. EventName string
  36. Level int
  37. Flags int
  38. Version int
  39. }
  40.  
  41. func producer(totalevents int64, ch chan<- TcpEvent, done chan<- bool) {
  42. var ii int64
  43. for ii = 0; ii <= totalevents; ii++ {
  44. e := TcpEvent{
  45. 12345,
  46. 67890,
  47. 12,
  48. 34,
  49. time.Now(),
  50. uuid.NewV1(),
  51. "Tater-Salad-City",
  52. 69,
  53. "Bad Tater",
  54. 0,
  55. 0,
  56. 0}
  57.  
  58. ch <- e
  59. }
  60.  
  61. done <- true
  62. }
  63.  
  64. func consumer(id int, ch <-chan TcpEvent, results chan<- TcpEvent) {
  65. var count int64
  66. count = 0
  67. for e := range ch {
  68. count++
  69. if count%100000 == 0 {
  70. fmt.Printf("[%d] Processed %d events\n", id, count)
  71. }
  72. a := TcpEvent{}
  73. a.Daddr = e.Daddr
  74. a.Saddr = e.Saddr
  75. a.Dport = e.Dport
  76. a.Sport = e.Sport
  77. a.EventId = e.EventId
  78. a.EventName = e.EventName
  79. a.Flags = e.Flags
  80. a.Version = e.Version
  81. a.Level = e.Level
  82. a.ProviderGuid = e.ProviderGuid
  83. a.ProviderName = e.ProviderName
  84. a.Timestamp = e.Timestamp
  85.  
  86. results <- a
  87. }
  88.  
  89. fmt.Printf("[%d] Done processing events. Total processed: %d\n", id, count)
  90. }
  91.  
  92. func resultHandler(ch <-chan TcpEvent, clearCount int64) {
  93. var results []TcpEvent
  94. var count int64
  95. count = 0
  96.  
  97. for e := range ch {
  98. count++
  99. results = append(results, e)
  100.  
  101. if count > clearCount {
  102. fmt.Printf("[result handler] Handled %d events, clearing...\n", count)
  103. results = results[:0]
  104. count = 0
  105. }
  106. }
  107. }
  108.  
  109. var cpuprofile = flag.String("cpuprofile", "", "write cpu profile `file`")
  110. var memprofile = flag.String("memprofile", "", "write memory profile to `file`")
  111. var resultcount = flag.Int64("resultcount", 100000, "count of elements to hold before clearing")
  112. var totalevents = flag.Int64("totalevents", 1000000000, "total number of events to pump")
  113.  
  114. func main() {
  115. flag.Parse()
  116.  
  117. if *cpuprofile != "" {
  118. f, err := os.Create(*cpuprofile)
  119. if err != nil {
  120. log.Fatal("could not create CPU profile: ", err)
  121. }
  122. if err := pprof.StartCPUProfile(f); err != nil {
  123. log.Fatal("could not start CPU profile: ", err)
  124. }
  125. defer pprof.StopCPUProfile()
  126. }
  127.  
  128. pool := make(chan TcpEvent, 3000)
  129. done := make(chan bool)
  130. results := make(chan TcpEvent, 10000)
  131. defer close(done)
  132. defer close(pool)
  133. defer close(results)
  134.  
  135. go resultHandler(results, *resultcount)
  136. go producer(*totalevents, pool, done)
  137.  
  138. for ii := 1; ii <= 10; ii++ {
  139. go consumer(ii, pool, results)
  140. }
  141.  
  142. <-done
  143. for len(pool) > 0 {
  144. fmt.Println("Waiting on consumers...")
  145. time.Sleep(time.Second * 2)
  146. }
  147.  
  148. if *memprofile != "" {
  149. f, err := os.Create(*memprofile)
  150. if err != nil {
  151. log.Fatal("could not create memory profile: ", err)
  152. }
  153. runtime.GC() // get up-to-date statistics
  154. if err := pprof.WriteHeapProfile(f); err != nil {
  155. log.Fatal("could not write memory profile: ", err)
  156. }
  157. f.Close()
  158. }
  159. }
RAW Paste Data