Merge branch 'refactor_enhance' into dev
[stompngo_examples.git] / sngecomm / environment.go
blob57a71458cef213021cc1a5be1d4d9c78415714bd
1 //
2 // Copyright © 2016 Guy M. Allard
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 // http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
18 Package sngecomm provides common functionality used in the stompngo_examples
19 project.
21 package sngecomm
23 import (
24 "bytes"
25 "log"
26 "os"
27 "strconv"
29 // "github.com/gmallard/stompngo"
32 var (
34 nqs = 1 // Default number of queues for multi-queue demo(s)
35 mdml = 1024 * 32 // Message data max length of variable message, 32K
36 md = make([]byte, 1) // Additional message data, primed during init()
37 pbc = 64 // Number of bytes to print (used in some
38 // // examples that receive).
40 sendFact float64 = 1.0 // Send sleep time factor
41 recvFact float64 = 1.0 // Receive sleep time factor
43 ackMode = "auto" // The default ack mode
45 pprof = false // Do not do profiling
48 // Initialization
49 func init() {
50 p := "_123456789ABCDEF"
51 c := mdml / len(p)
52 b := []byte(p)
53 md = bytes.Repeat(b, c) // A long string
57 // Number of queues
58 func Nqs() int {
60 if s := os.Getenv("STOMP_NQS"); s != "" {
61 i, e := strconv.ParseInt(s, 10, 32)
62 if nil != e {
63 log.Printf("v1:%v v2:%v\n", "NQS conversion error", e)
64 } else {
65 nqs = int(i)
68 return nqs
71 // Max Data Message Length
72 func Mdml() int {
73 if s := os.Getenv("STOMP_MDML"); s != "" {
74 i, e := strconv.ParseInt(s, 10, 32)
75 if nil != e {
76 log.Printf("v1:%v v2:%v\n", "MDML conversion error", e)
77 } else {
78 mdml = int(i)
79 p := "_123456789ABCDEF"
80 c := mdml / len(p)
81 b := []byte(p)
82 md = bytes.Repeat(b, c) // A long string
85 return mdml
88 // Use profiling or not
89 func Pprof() bool {
90 if am := os.Getenv("STOMP_PPROF"); am != "" {
91 pprof = true
93 return pprof
96 // ACK mode for those examples that use it.
97 func AckMode() string {
98 if am := os.Getenv("STOMP_ACKMODE"); am != "" {
99 if am == "auto" || am == "client" || am == "client-individual" {
100 ackMode = am
101 } else {
102 log.Printf("v1:%v v2:%v\n", "ACKMODE error", am)
105 return ackMode
108 // Get Send Sleep Factor
109 func SendFactor() float64 {
110 if s := os.Getenv("STOMP_SENDFACT"); s != "" {
111 f, e := strconv.ParseFloat(s, 64)
112 if nil != e {
113 log.Printf("v1:%v v2:%v\n", "SENDFACT conversion error", e)
114 } else {
115 sendFact = float64(f)
118 return sendFact
121 // Get Recv Sleep Factor
122 func RecvFactor() float64 {
123 if s := os.Getenv("STOMP_RECVFACT"); s != "" {
124 f, e := strconv.ParseFloat(s, 64)
125 if nil != e {
126 log.Printf("v1:%v v2:%v\n", "RECVFACT conversion error", e)
127 } else {
128 recvFact = float64(f)
131 return recvFact
134 // Get partial string, random length
135 func Partial() []byte {
136 r := int(ValueBetween(1, int64(mdml-1), 1.0))
137 return md[0:r]
140 // Print Byte Count
141 func Pbc() int {
142 if s := os.Getenv("STOMP_PBC"); s != "" {
143 i, e := strconv.ParseInt(s, 10, 32)
144 if nil != e {
145 log.Printf("v1:%v v2:%v\n", "PBC conversion error", e)
146 } else {
147 pbc = int(i)
150 return pbc
153 // Does receive wait to simulate message processing
154 func RecvWait() bool {
155 f := os.Getenv("STOMP_NORECVW")
156 if f == "" {
157 return true
159 return false
162 // Does send wait to simulate message building
163 func SendWait() bool {
164 f := os.Getenv("STOMP_NOSENDW")
165 if f == "" {
166 return true
168 return false
171 // True if max procs are to be set
172 func SetMAXPROCS() bool {
173 f := os.Getenv("STOMP_SETMAXPROCS")
174 if f == "" {
175 return false
177 return true