This repository has been archived by the owner on May 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
work.go
103 lines (80 loc) · 1.7 KB
/
work.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
package main
import (
"fmt"
"github.com/flachnetz/alwaysprofile/pprof"
"github.com/flachnetz/alwaysprofile/pprof/sender"
"io"
"io/ioutil"
"math/rand"
"net/http"
"net/url"
"sort"
"time"
)
func main() {
config := pprof.Config{
ServiceName: "demo-5",
Tags: map[string]string{
"version": "v1.0.0",
},
SampleFrequencyHz: 500,
Sender: sender.New(sender.Config{
BaseURL: &url.URL{Scheme: "http", Host: "localhost:3080", Path: "/v1/profile"},
Timeout: 1 * time.Second,
}),
}
defer pprof.Start(config).Stop()
http.DefaultClient.Transport = &http.Transport{
MaxConnsPerHost: 128,
MaxIdleConns: 128,
MaxIdleConnsPerHost: 128,
}
fmt.Println("Starting random workload.")
limitCh := make(chan bool, 2)
for {
limitCh <- true
go func() {
defer func() { <-limitCh }()
lookForRandomPrimes()
}()
}
}
func lookForRandomPrimes() []int {
var primes []int
for idx := 0; idx < 1000; idx++ {
n := rand.Intn(1024 * 1024)
if isPrimeNumber(n) {
primes = append(primes, n)
reportPrimeNumber(n)
}
// time.Sleep(100 * time.Millisecond)
}
sort.Ints(primes)
return primes
}
func isPrimeNumber(n int) bool {
if isSimplePrime(n) {
return true
}
for idx := 2; idx < n; idx++ {
if isDivideableBy(n, idx) {
return false
}
}
return true
}
func isSimplePrime(n int) bool {
primes := map[int]bool{2: true, 3: true, 5: true, 7: true, 11: true, 13: true, 17: true}
return primes[n]
}
func isDivideableBy(n int, m int) bool {
return n%m == 0
}
func reportPrimeNumber(n int) {
response, err := http.Get(fmt.Sprintf("http://localhost:8000/prime?n=%d", n))
if err != nil {
return
}
_, _ = io.Copy(ioutil.Discard, response.Body)
_ = response.Body.Close()
}