Changes
This commit is contained in:
parent
9eac960763
commit
da9196f389
22 changed files with 302 additions and 76 deletions
209
workloadGenerator/workloadGenerator.go
Normal file
209
workloadGenerator/workloadGenerator.go
Normal file
|
@ -0,0 +1,209 @@
|
|||
package workloadgenerator
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"crypto/tls"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"log"
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
"git.nutfactory.org/hoernschen/Matrix/entities/room"
|
||||
"git.nutfactory.org/hoernschen/Matrix/entities/user"
|
||||
"git.nutfactory.org/hoernschen/Matrix/utils"
|
||||
)
|
||||
|
||||
var httpString = "http"
|
||||
var servers = []string{
|
||||
"localhost",
|
||||
}
|
||||
|
||||
var userIds []string
|
||||
var accessTokens map[string]string
|
||||
var roomId string
|
||||
|
||||
var users = []string{
|
||||
"user1",
|
||||
"user2",
|
||||
"user3",
|
||||
"user4",
|
||||
"user5",
|
||||
"user6",
|
||||
}
|
||||
|
||||
func main() {
|
||||
http.DefaultTransport.(*http.Transport).TLSClientConfig = &tls.Config{InsecureSkipVerify: true}
|
||||
accessTokens = make(map[string]string)
|
||||
err := createUsers()
|
||||
if err != nil {
|
||||
log.Printf("Error in User-Creation: %s", err)
|
||||
return
|
||||
}
|
||||
// Create Room
|
||||
err = createRoom(userIds[0])
|
||||
if err != nil {
|
||||
log.Printf("Error in Room-Creation: %s", err)
|
||||
return
|
||||
}
|
||||
|
||||
// Join to Room
|
||||
err = joinRoom(userIds[1:])
|
||||
if err != nil {
|
||||
log.Printf("Error joining Room: %s", err)
|
||||
return
|
||||
}
|
||||
|
||||
// Send Messages with Timer
|
||||
}
|
||||
|
||||
func reset() (err error) {
|
||||
// TODO: Implement
|
||||
return
|
||||
}
|
||||
|
||||
func createRoom(userId string) (err error) {
|
||||
accessToken := accessTokens[userId]
|
||||
requestUrl := fmt.Sprintf("%s://%s/_matrix/client/r0/createRoom", httpString, strings.Split(userId, ":")[1])
|
||||
request := room.CreateRoomRequest{
|
||||
Visibility: "public",
|
||||
Name: "Testraum",
|
||||
Topic: "Raum für die Energieeffizienz-Tests",
|
||||
RoomVersion: "1",
|
||||
CreationContent: room.CreationContent{
|
||||
Federated: true,
|
||||
},
|
||||
Preset: "public_chat",
|
||||
IsDirect: true,
|
||||
}
|
||||
reqBody, err := json.Marshal(request)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
client := &http.Client{}
|
||||
req, err := http.NewRequest(http.MethodPost, requestUrl, bytes.NewBuffer(reqBody))
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
req.Header["Content-Type"] = []string{"application/json"}
|
||||
req.Header["Authorization"] = []string{fmt.Sprintf("Bearer %s", accessToken)}
|
||||
res, err := client.Do(req)
|
||||
if res.StatusCode != http.StatusOK {
|
||||
handleError(res)
|
||||
} else {
|
||||
response := room.CreateRoomResponse{}
|
||||
decoder := json.NewDecoder(res.Body)
|
||||
err = decoder.Decode(&response)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
roomId = response.RoomId
|
||||
log.Printf("Room created: %s", roomId)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func joinRoom(userIdsToJoin []string) (err error) {
|
||||
for _, userId := range userIdsToJoin {
|
||||
err = joinRoomUser(userId)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func joinRoomUser(userId string) (err error) {
|
||||
accessToken := accessTokens[userId]
|
||||
requestUrl := fmt.Sprintf("%s://%s/_matrix/client/r0/rooms/%s/join", httpString, strings.Split(userId, ":")[1], roomId)
|
||||
request := room.JoinRoomUserRequest{}
|
||||
reqBody, err := json.Marshal(request)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
client := &http.Client{}
|
||||
req, err := http.NewRequest(http.MethodPost, requestUrl, bytes.NewBuffer(reqBody))
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
req.Header["Content-Type"] = []string{"application/json"}
|
||||
req.Header["Authorization"] = []string{fmt.Sprintf("Bearer %s", accessToken)}
|
||||
res, err := client.Do(req)
|
||||
if res.StatusCode != http.StatusOK {
|
||||
handleError(res)
|
||||
} else {
|
||||
response := room.JoinRoomUserResponse{}
|
||||
decoder := json.NewDecoder(res.Body)
|
||||
err = decoder.Decode(&response)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
roomId = response.RoomId
|
||||
log.Printf("%s joined Room", userId)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func createUsers() (err error) {
|
||||
for _, userToCreate := range users {
|
||||
var userId string
|
||||
var accessToken string
|
||||
userId, accessToken, err = createUser(userToCreate, "localhost")
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
if userId != "" && accessToken != "" {
|
||||
log.Printf("%s: %s", userId, accessToken)
|
||||
accessTokens[userId] = accessToken
|
||||
userIds = append(userIds, userId)
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func createUser(userToCreate string, homeserver string) (userId string, accessToken string, err error) {
|
||||
requestUrl := fmt.Sprintf("%s://%s/_matrix/client/r0/register", httpString, homeserver)
|
||||
request := user.RegisterRequest{
|
||||
Auth: user.AuthentificationData{
|
||||
LoginType: "m.login.password",
|
||||
},
|
||||
Username: userToCreate,
|
||||
Password: "password",
|
||||
DeviceName: fmt.Sprintf("%s's device", userToCreate),
|
||||
}
|
||||
reqBody, err := json.Marshal(request)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
client := &http.Client{}
|
||||
req, err := http.NewRequest(http.MethodPost, requestUrl, bytes.NewBuffer(reqBody))
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
req.Header["Content-Type"] = []string{"application/json"}
|
||||
res, err := client.Do(req)
|
||||
if res.StatusCode != http.StatusOK {
|
||||
handleError(res)
|
||||
} else {
|
||||
response := user.RegisterResponse{}
|
||||
decoder := json.NewDecoder(res.Body)
|
||||
err = decoder.Decode(&response)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
userId = response.UserId
|
||||
accessToken = response.AccessToken
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func handleError(res *http.Response) {
|
||||
response := utils.ErrorResponse{}
|
||||
decoder := json.NewDecoder(res.Body)
|
||||
err := decoder.Decode(&response)
|
||||
if err != nil {
|
||||
log.Printf("Error not parseable")
|
||||
return
|
||||
}
|
||||
log.Printf("%s (%s)", response.ErrorMessage, response.ErrorCode)
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue