pub.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. // Copyright 2021-2023 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. //go:build benchmark
  15. // +build benchmark
  16. // Not necessary to build the file, until for the edgex benchmark test
  17. package main
  18. import (
  19. "context"
  20. "encoding/json"
  21. "fmt"
  22. "log"
  23. "os"
  24. "strconv"
  25. "sync"
  26. "time"
  27. v3 "github.com/edgexfoundry/go-mod-core-contracts/v3/common"
  28. "github.com/edgexfoundry/go-mod-core-contracts/v3/dtos"
  29. "github.com/edgexfoundry/go-mod-messaging/v3/messaging"
  30. "github.com/edgexfoundry/go-mod-messaging/v3/pkg/types"
  31. )
  32. var msgConfig1 = types.MessageBusConfig{
  33. Broker: types.HostInfo{
  34. Host: "172.31.1.144",
  35. Port: 6379,
  36. Protocol: "redis",
  37. },
  38. Type: messaging.Redis,
  39. }
  40. type data struct {
  41. temperature int
  42. humidity int
  43. }
  44. var mockup = []data{
  45. {temperature: 10, humidity: 15},
  46. {temperature: 15, humidity: 20},
  47. {temperature: 20, humidity: 25},
  48. {temperature: 25, humidity: 30},
  49. {temperature: 30, humidity: 35},
  50. {temperature: 35, humidity: 40},
  51. {temperature: 40, humidity: 45},
  52. {temperature: 45, humidity: 50},
  53. {temperature: 50, humidity: 55},
  54. {temperature: 55, humidity: 60},
  55. }
  56. func pubEventClientRedis(count int, wg *sync.WaitGroup) {
  57. defer wg.Done()
  58. if msgClient, err := messaging.NewMessageClient(msgConfig1); err != nil {
  59. log.Fatal(err)
  60. } else {
  61. if ec := msgClient.Connect(); ec != nil {
  62. log.Fatal(ec)
  63. } else {
  64. index := 0
  65. for i := 0; i < count; i++ {
  66. if i%10 == 0 {
  67. index = 0
  68. }
  69. testEvent := dtos.NewEvent("demoProfile", "demo", "demoSource")
  70. err := testEvent.AddSimpleReading("Temperature", v3.ValueTypeInt32, int32(mockup[index].temperature))
  71. if err != nil {
  72. fmt.Errorf("Add reading error for Temperature: %v\n", int32(mockup[index].temperature))
  73. }
  74. testEvent.Readings[0].DeviceName = "Temperature device"
  75. err = testEvent.AddSimpleReading("Humidity", v3.ValueTypeInt32, int32(mockup[index].humidity))
  76. if err != nil {
  77. fmt.Errorf("Add reading error for Humidity: %v\n", int32(mockup[index].temperature))
  78. }
  79. testEvent.Readings[1].DeviceName = "Humidity device"
  80. index++
  81. data, err := json.Marshal(testEvent)
  82. if err != nil {
  83. fmt.Errorf("unexpected error MarshalEvent %v", err)
  84. }
  85. env := types.NewMessageEnvelope([]byte(data), context.Background())
  86. env.ContentType = "application/json"
  87. if e := msgClient.Publish(env, "events"); e != nil {
  88. log.Fatal(e)
  89. } else {
  90. // fmt.Printf("%d - %s\n", index, string(data))
  91. }
  92. time.Sleep(100 * time.Nanosecond)
  93. }
  94. }
  95. }
  96. }
  97. func main() {
  98. start := time.Now()
  99. count := 1000
  100. if len(os.Args) == 2 {
  101. v := os.Args[1]
  102. if c, err := strconv.Atoi(v); err != nil {
  103. fmt.Errorf("%s\n", err)
  104. } else {
  105. count = c
  106. }
  107. }
  108. var wg sync.WaitGroup
  109. for i := 0; i < 1; i++ {
  110. wg.Add(1)
  111. go pubEventClientRedis(count, &wg)
  112. }
  113. wg.Wait()
  114. t := time.Now()
  115. elapsed := t.Sub(start)
  116. fmt.Printf("elapsed %2fs\n", elapsed.Seconds())
  117. }