SJA工艺
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

227 lines
4.2 KiB

3 years ago
  1. package Engine
  2. import (
  3. "LAPP_SJA_ME/conf"
  4. "fmt"
  5. "log"
  6. "os"
  7. "testing"
  8. "time"
  9. )
  10. func TestEngine(t *testing.T){
  11. conf, err := conf.ReadYamlConfig("D:\\Go\\gopath\\src\\LAPP_PLATFORM\\conf\\config.yaml")
  12. if err != nil {
  13. t.Errorf("failed to read yaml config due to: %v", err)
  14. return
  15. }
  16. //set logfile Stdout
  17. logFile, logErr := os.OpenFile(conf.Log, os.O_CREATE|os.O_RDWR|os.O_APPEND, 0666)
  18. if logErr != nil {
  19. fmt.Println("Fail to the log file:", conf.Log)
  20. os.Exit(1)
  21. }
  22. defer logFile.Close()
  23. log.SetOutput(logFile)
  24. log.SetFlags(log.Ldate | log.Ltime | log.Lshortfile)
  25. fmt.Printf("conf is :%v",conf)
  26. RunEngine(conf)
  27. time.Sleep(60*time.Second)
  28. }
  29. // 测试不同服务之间的GoRouting通信
  30. /**
  31. |-- 管道 --| --> 1号打印机 --|
  32. Fetcher--|--管道--|Scheduler --->| |--管道--|--> Saver
  33. |-- 管道 --| --> 2号打印机 --|
  34. 引擎管理对象
  35. 1. Fetcher
  36. 2. Fetcher -> Scheduler 管道
  37. 3. Scheduler
  38. 4. Printer列表 (每个Printer包括接收数据的管道和缓存数据的队列
  39. 5. 打印机 -> Saver的管道
  40. 6. Saver带有缓存数据队列
  41. **/
  42. func TestEngineRoutine(t *testing.T) {
  43. fschan := make(chan int) // 数据获取服务到调度器的管道
  44. sp1chan := make(chan int) // 调度器到1号打印机的管道
  45. sp2chan := make(chan int) // 调度器到2号打印机的管道
  46. pschan := make(chan int) // 打印机到保存服务的管道
  47. var t1, t2 time.Time
  48. var sp1q []int
  49. var sp2q [] int
  50. t1 = time.Now()
  51. fmt.Println("start time = ",t1)
  52. // 模拟数据获取服务
  53. go func(){
  54. for i := 0; i < 40000; i++ {
  55. fschan <- i
  56. }
  57. }()
  58. // 启动调度器服务
  59. go func(){
  60. for {
  61. i := <-fschan
  62. if i%2 == 0 {
  63. sp1chan <- i
  64. } else {
  65. sp2chan <- i
  66. }
  67. }
  68. }()
  69. // 启动打印机1服务
  70. go func(){
  71. for {
  72. i := <-sp1chan
  73. //fmt.Println("打印机1接收打印任务:", i)
  74. sp1q = append(sp1q, i)
  75. }
  76. }()
  77. // 启动打印机2服务
  78. go func(){
  79. for {
  80. i := <-sp2chan
  81. //fmt.Println("打印机2接收打印任务:", i)
  82. sp2q = append(sp2q, i)
  83. }
  84. }()
  85. go func(){
  86. for {
  87. if len(sp1q) > 0 {
  88. i := sp1q[0]
  89. sp1q = sp1q[1:]
  90. pschan <- i
  91. fmt.Println("打印机1:任务:",i,"完成")
  92. }
  93. }
  94. }()
  95. go func(){
  96. for {
  97. if len(sp2q) > 0 {
  98. i := sp2q[0]
  99. sp2q = sp2q[1:]
  100. pschan <- i
  101. fmt.Println("打印机2:任务:",i,"完成")
  102. }
  103. }
  104. }()
  105. // 启动保存服务
  106. go func(){
  107. for {
  108. i := <-pschan
  109. fmt.Println("任务:", i, " 被保存")
  110. if i >= 39998 {
  111. t2 = time.Now()
  112. fmt.Println("end time = ",t2)
  113. }
  114. }
  115. }()
  116. time.Sleep(10*time.Second)
  117. sub := t2.Sub(t1)
  118. fmt.Println("Duration = ",sub.Seconds())
  119. }
  120. func TestEngineRoutineWithQueue(t *testing.T) {
  121. fschan := make(chan int) // 数据获取服务到调度器的管道
  122. sp1chan := make(chan int) // 调度器到1号打印机的管道
  123. sp2chan := make(chan int) // 调度器到2号打印机的管道
  124. pschan := make(chan int) // 打印机到保存服务的管道
  125. var t1, t2 time.Time
  126. t1 = time.Now()
  127. fmt.Println("start time = ",t1)
  128. // 模拟数据获取服务
  129. go func(){
  130. for i := 0; i < 40000; i++ {
  131. fschan <- i
  132. }
  133. }()
  134. // 启动调度器服务
  135. go func(){
  136. for {
  137. i := <-fschan
  138. if i%2 == 0 {
  139. sp1chan <- i
  140. } else {
  141. sp2chan <- i
  142. }
  143. }
  144. }()
  145. // 启动打印机1服务
  146. go func(){
  147. var sp1q []int
  148. var t int
  149. for {
  150. if len(sp1q) > 0 {
  151. t = sp1q[0]
  152. }else{
  153. continue
  154. }
  155. select{
  156. case i := <-sp1chan:
  157. sp1q = append(sp1q, i)
  158. fmt.Println("打印机1接收打印任务:", i)
  159. case pschan <- t:
  160. sp1q = sp1q[1:]
  161. fmt.Println("打印机1发送保存任务:", t)
  162. }
  163. }
  164. }()
  165. // 启动打印机2服务
  166. go func(){
  167. var sp2q []int
  168. var t int
  169. for {
  170. if len(sp2q) > 0 {
  171. t = sp2q[0]
  172. }else{
  173. continue
  174. }
  175. select{
  176. case i := <-sp2chan:
  177. sp2q = append(sp2q, i)
  178. fmt.Println("打印机2接收打印任务:", i)
  179. case pschan <- t:
  180. sp2q = sp2q[1:]
  181. fmt.Println("打印机2发送保存任务:", t)
  182. }
  183. }
  184. }()
  185. // 启动保存服务
  186. go func(){
  187. for {
  188. i := <-pschan
  189. fmt.Println("任务:", i, " 被保存")
  190. if i >= 39998 {
  191. t2 = time.Now()
  192. fmt.Println("end time = ",t2)
  193. }
  194. }
  195. }()
  196. time.Sleep(10*time.Second)
  197. sub := t2.Sub(t1)
  198. fmt.Println("Duration = ",sub.Seconds())
  199. }