-
Notifications
You must be signed in to change notification settings - Fork 4
/
test5_printserver.go
115 lines (84 loc) · 2.51 KB
/
test5_printserver.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
/*
*
* Program to do a load test, and validate the response team of our api
*
* @package main
* @author @jeffotoni
* @size 29/07/2017
*
*/
// _[Rate limiting](http://en.wikipedia.org/wiki/Rate_limiting)_
// is an important mechanism for controlling resource
// utilization and maintaining quality of service. Go
// elegantly supports rate limiting with goroutines,
// channels, and [tickers](tickers).
package main
import (
"fmt"
Shoot "github.com/jeffotoni/printserver/pkg"
// "os"
"strings"
"time"
)
func main() {
endPoinToken := "http://localhost:9001/token"
endPoint1 := "http://localhost:9001/ping"
//
// get token
//
TokenString := Shoot.GeToken(endPoinToken, "MjEyMzJmMjk3YTU3YTVhNzQzODk0YTBlNGE4MDFmYzM=", "OTcyZGFkZGNhY2YyZmVhMjUzZmRhODY5NTY0ODUxMTU=")
TokenString = strings.TrimSpace(strings.Trim(TokenString, " "))
fmt.Println("Token: ", TokenString)
// os.Exit(1)
// if len(os.Args) > 2 {
// //fmt.Println(os.Args[1])
// token = os.Args[1]
// endPoint1 += os.Args[2]
// } else {
// fmt.Println("Passes the token as argument!")
// os.Exit(1)
// }
// fmt.Println(endPoint1)
// os.Exit(1)
// curl -X POST -H "Content-Type: application/json" -H "Authorization: Basic MjEyMzJmMjk3YTU3YTVhNzQzODk0YTBlNGE4MDFmYzM=:OTcyZGFkZGNhY2YyZmVhMjUzZmRhODY5NTY0ODUxMTU=" localhost:9001/token
// os.Exit(1)
//endPoint1 := "http://localhost:9001/ping"
//endPoint2 = "http://localhost:9001/ping2"
// First we'll look at basic rate limiting. Suppose
// we want to limit our handling of incoming requests.
// We'll serve these requests off a channel of the
// same name.
requests := make(chan int, 50)
for i := 1; i <= 50; i++ {
println("Loading requests: ", fmt.Sprintf("%d", i))
time.Sleep(time.Millisecond * 40)
requests <- i
}
close(requests)
// This `limiter` channel will receive a value
// every 100 or 300 milliseconds. This is the regulator in
// our rate limiting scheme.
limiter := time.Tick(time.Millisecond * 35)
// time start
//
//
time1 := time.Now()
// By blocking on a receive from the `limiter` channel
// before serving each request, we limit ourselves to
// 1 request every 200 milliseconds.
for req := range requests {
<-limiter
msg := Shoot.ShootUrl(endPoint1, TokenString)
fmt.Println("request: ", req, "msg: ", msg)
if req == 200 {
fmt.Println("pause 2 segs")
time.Sleep(time.Second * 2)
}
}
time2 := time.Now()
diff := time2.Sub(time1)
fmt.Println(diff)
fmt.Println("Enter enter to finish")
var input string
fmt.Scanln(&input)
}