main.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. package main
  2. import (
  3. "bufio"
  4. "flag"
  5. "fmt"
  6. "log"
  7. "os"
  8. "runtime/pprof"
  9. "sync"
  10. "time"
  11. s "github.com/mikefaille/goStockDemo/stock"
  12. u "github.com/mikefaille/goStockDemo/util"
  13. )
  14. import _ "net/http/pprof"
  15. //var cpuprofile = flag.String("cpuprofile", "", "write cpu profile to file")
  16. var min s.Stock
  17. var max s.Stock
  18. type dataToCompute struct {
  19. current chan float64
  20. next chan float64
  21. }
  22. var mutex sync.Mutex
  23. var elapsed time.Time
  24. var start time.Time
  25. func main() {
  26. var cpuprofile = flag.String("cpuprofile", "", "write cpu profile to file")
  27. var file = flag.String("file", "stockprices_sample_1000000.csv", "Filename")
  28. var lineNb = flag.Int64("nbline", 200, "Nombre de ligne")
  29. flag.Parse()
  30. if *cpuprofile != "" {
  31. f, err := os.Create(*cpuprofile)
  32. if err != nil {
  33. log.Fatal(err)
  34. }
  35. pprof.StartCPUProfile(f)
  36. defer pprof.StopCPUProfile()
  37. }
  38. start = time.Now()
  39. var wg2 sync.WaitGroup
  40. var wg sync.WaitGroup
  41. var wg3 sync.WaitGroup
  42. chanLine := make(chan []byte, *lineNb)
  43. accumulator := make(chan s.Stock, *lineNb)
  44. wg3.Add(1)
  45. wg2.Add(1)
  46. wg.Add(1)
  47. go func() {
  48. for data := range chanLine {
  49. // data := make([]byte, len(line))
  50. // copy(data, line)
  51. if len(data) < 29 {
  52. //skip this
  53. } else {
  54. thisStock := new(s.Stock)
  55. virgule := 28
  56. // fmt.Println(virgule)
  57. thisStock.Date = data[0 : virgule-1]
  58. var err error
  59. thisStock.Value, err = u.Float64frombytes3(data[virgule:])
  60. // fmt.Println("Process", thisStock.value)
  61. if err != nil {
  62. // if len(data) == 29 {
  63. // wg.Add(1)
  64. // accumulator <- *thisStock
  65. // } else {
  66. // fmt.Println(string(data))
  67. panic(err)
  68. } else {
  69. // fmt.Println("le nombre", thisStock.value)
  70. accumulator <- *thisStock
  71. }
  72. }
  73. }
  74. wg.Done()
  75. }()
  76. go func() {
  77. inFile, err := os.Open(*file)
  78. // inFile, err := os.Open("stock.cvs")
  79. defer inFile.Close()
  80. u.Check(err)
  81. r := bufio.NewReader(inFile)
  82. scanner := bufio.NewScanner(r)
  83. var outFinal []byte
  84. for scanner.Scan() {
  85. out := scanner.Bytes()
  86. if out != nil {
  87. outFinal = make([]byte, len(out))
  88. copy(outFinal, out)
  89. chanLine <- outFinal
  90. }
  91. }
  92. wg2.Done()
  93. }()
  94. // transactions := []s.Transaction{}
  95. go func() {
  96. var nextStock s.Stock
  97. var currentStock s.Stock
  98. // transactions := new(s.Transactions)
  99. min.Value = 9999999999
  100. max.Value = 0
  101. for nextStock = range accumulator {
  102. if currentStock.Value == 0 {
  103. currentStock = nextStock
  104. } else {
  105. // transaction := new(s.Transaction)
  106. switch {
  107. case currentStock.Value < nextStock.Value && currentStock.Value < min.Value:
  108. min = currentStock
  109. break
  110. case currentStock.Value > nextStock.Value && currentStock.Value > max.Value:
  111. max = currentStock
  112. break
  113. default:
  114. break
  115. }
  116. currentStock = nextStock
  117. }
  118. }
  119. switch {
  120. case nextStock.Value < min.Value:
  121. min = currentStock
  122. break
  123. case nextStock.Value > max.Value:
  124. max = currentStock
  125. break
  126. }
  127. wg3.Done()
  128. }()
  129. go func() {
  130. wg2.Wait()
  131. close(chanLine)
  132. }()
  133. go func() {
  134. wg.Wait()
  135. close(accumulator)
  136. wg3.Wait()
  137. }()
  138. wg.Wait()
  139. wg2.Wait()
  140. wg3.Wait()
  141. redraw_all()
  142. }
  143. func redraw_all() {
  144. delay := time.Since(start).Nanoseconds()
  145. fmt.Printf("Profit maximal de [%.3f]\n", max.Value-min.Value)
  146. fmt.Printf("Achat des actions [%.3f] @ [%s]\n", min.Value, min.Date)
  147. fmt.Printf("Vente des actions [%.3f] @ [%s]\n", max.Value, max.Date)
  148. fmt.Printf("Temps d'exécution [%d]ms\n", delay/int64(time.Millisecond))
  149. fmt.Printf("Temps d'exécution [%d]ns\n", delay)
  150. }