pub.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. // Copyright 2021 EMQ Technologies Co., Ltd.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. // +build benchmark
  15. //Not necessary to build the file, until for the edgex benchmark test
  16. package main
  17. import (
  18. "context"
  19. "encoding/json"
  20. "fmt"
  21. v2 "github.com/edgexfoundry/go-mod-core-contracts/v2/common"
  22. "github.com/edgexfoundry/go-mod-core-contracts/v2/dtos"
  23. "github.com/edgexfoundry/go-mod-messaging/v2/messaging"
  24. "github.com/edgexfoundry/go-mod-messaging/v2/pkg/types"
  25. "log"
  26. "os"
  27. "strconv"
  28. "sync"
  29. "time"
  30. )
  31. var msgConfig1 = types.MessageBusConfig{
  32. PublishHost: types.HostInfo{
  33. Host: "172.31.1.144",
  34. Port: 5563,
  35. Protocol: "tcp",
  36. },
  37. Type: messaging.ZeroMQ,
  38. }
  39. type data struct {
  40. temperature int
  41. humidity int
  42. }
  43. var mockup = []data{
  44. {temperature: 10, humidity: 15},
  45. {temperature: 15, humidity: 20},
  46. {temperature: 20, humidity: 25},
  47. {temperature: 25, humidity: 30},
  48. {temperature: 30, humidity: 35},
  49. {temperature: 35, humidity: 40},
  50. {temperature: 40, humidity: 45},
  51. {temperature: 45, humidity: 50},
  52. {temperature: 50, humidity: 55},
  53. {temperature: 55, humidity: 60},
  54. }
  55. func pubEventClientZeroMq(count int, wg *sync.WaitGroup) {
  56. defer wg.Done()
  57. if msgClient, err := messaging.NewMessageClient(msgConfig1); err != nil {
  58. log.Fatal(err)
  59. } else {
  60. if ec := msgClient.Connect(); ec != nil {
  61. log.Fatal(ec)
  62. } else {
  63. index := 0
  64. for i := 0; i < count; i++ {
  65. if i%10 == 0 {
  66. index = 0
  67. }
  68. testEvent := dtos.NewEvent("demoProfile", "demo", "demoSource")
  69. err := testEvent.AddSimpleReading("Temperature", v2.ValueTypeInt32, int32(mockup[index].temperature))
  70. if err != nil {
  71. fmt.Errorf("Add reading error for Temperature: %v\n", int32(mockup[index].temperature))
  72. }
  73. testEvent.Readings[0].DeviceName = "Temperature device"
  74. err = testEvent.AddSimpleReading("Humidity", v2.ValueTypeInt32, int32(mockup[index].humidity))
  75. if err != nil {
  76. fmt.Errorf("Add reading error for Humidity: %v\n", int32(mockup[index].temperature))
  77. }
  78. testEvent.Readings[1].DeviceName = "Humidity device"
  79. index++
  80. data, err := json.Marshal(testEvent)
  81. if err != nil {
  82. fmt.Errorf("unexpected error MarshalEvent %v", err)
  83. }
  84. env := types.NewMessageEnvelope([]byte(data), context.Background())
  85. env.ContentType = "application/json"
  86. if e := msgClient.Publish(env, "events"); e != nil {
  87. log.Fatal(e)
  88. } else {
  89. //fmt.Printf("%d - %s\n", index, string(data))
  90. }
  91. time.Sleep(100 * time.Nanosecond)
  92. }
  93. }
  94. }
  95. }
  96. func main() {
  97. start := time.Now()
  98. count := 1000
  99. if len(os.Args) == 2 {
  100. v := os.Args[1]
  101. if c, err := strconv.Atoi(v); err != nil {
  102. fmt.Errorf("%s\n", err)
  103. } else {
  104. count = c
  105. }
  106. }
  107. var wg sync.WaitGroup
  108. for i := 0; i < 1; i++ {
  109. wg.Add(1)
  110. go pubEventClientZeroMq(count, &wg)
  111. }
  112. wg.Wait()
  113. t := time.Now()
  114. elapsed := t.Sub(start)
  115. fmt.Printf("elapsed %2fs\n", elapsed.Seconds())
  116. }