-
Notifications
You must be signed in to change notification settings - Fork 0
/
interface.go
80 lines (68 loc) · 2.19 KB
/
interface.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
package main
import (
"bytes"
"encoding/json"
"fmt"
"math/rand"
"net/http"
"time"
log "github.com/sirupsen/logrus"
)
const (
apiURL = "http://192.168.10.201:8077/university/open/kt2/blockchain/performance"
clientID = "1"
clientSecret = "1111_test"
)
type RequestBody struct {
MaxTransactionsPS int `json:"maxTransactionsPS"`
AverageThroughput int `json:"averageThroughput"`
NumOfDataEntries int `json:"numOfDataEntries"`
NumOfBCInstructForDrones int `json:"numOfBCInstructForDrones"`
NumOfDroneSA int `json:"numOfDroneSA"`
}
func ScheduledPush(logger *log.Logger) {
// Define a ticker, triggering every 3 seconds
ticker := time.NewTicker(20 * time.Second)
// Start an infinite loop
for {
// Wait for the ticker to trigger
<-ticker.C
// Prepare data for the POST request
maxTransactionsPS := 180 //每秒最大交易数量
averageThroughput := rand.Intn(100) //平均吞吐率
numOfDataEntries := 946 // 区块链上链的数据数量
numOfBCInstructForDrones := 269 //无人机区块链指令数量
numOfDroneSA := 4 //无人机安全关联的数量
// Prepare data for the POST request
requestData := RequestBody{
MaxTransactionsPS: maxTransactionsPS,
AverageThroughput: averageThroughput,
NumOfDataEntries: numOfDataEntries,
NumOfBCInstructForDrones: numOfBCInstructForDrones,
NumOfDroneSA: numOfDroneSA,
}
// Convert data to JSON format
jsonData, err := json.Marshal(requestData)
if err != nil {
fmt.Println("JSON marshaling failed:", err)
continue
}
req, err := http.NewRequest("POST", apiURL, bytes.NewBuffer(jsonData))
if err != nil {
fmt.Println("Error creating request:", err)
continue
}
req.Header.Set("Content-Type", "application/json")
req.Header.Set("clientId", clientID)
req.Header.Set("clientSecret", clientSecret)
// Send a POST request
resp, err := http.DefaultClient.Do(req)
if err != nil {
fmt.Println("POST request failed:", err)
continue
}
// Print response information
logger.Infof("POST request Yunfeng push message success, status:%v\n", resp.Status)
resp.Body.Close()
}
}