2017-04-20 22:40:52 +00:00
|
|
|
// Copyright 2017 Vector Creations Ltd
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2017-03-14 17:05:02 +00:00
|
|
|
// Generate a list of matrix room events for load testing.
|
|
|
|
// Writes the events to stdout by default.
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/base64"
|
|
|
|
"encoding/json"
|
|
|
|
"flag"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"strings"
|
|
|
|
"time"
|
2017-07-13 10:41:30 +00:00
|
|
|
|
|
|
|
"github.com/matrix-org/dendrite/roomserver/api"
|
|
|
|
"github.com/matrix-org/gomatrixserverlib"
|
|
|
|
"golang.org/x/crypto/ed25519"
|
2017-03-14 17:05:02 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const usage = `Usage: %s
|
|
|
|
|
|
|
|
Generate a list of matrix room events for load testing.
|
|
|
|
Writes the events to stdout separated by new lines
|
|
|
|
|
|
|
|
Arguments:
|
|
|
|
|
|
|
|
`
|
|
|
|
|
|
|
|
var (
|
|
|
|
serverName = flag.String("server-name", "localhost", "The name of the matrix server to generate events for")
|
|
|
|
keyID = flag.String("key-id", "ed25519:auto", "The ID of the key used to sign the events")
|
|
|
|
privateKeyString = flag.String("private-key", defaultKey, "Base64 encoded private key to sign events with")
|
|
|
|
roomID = flag.String("room-id", "!roomid:$SERVER_NAME", "The room ID to generate events in")
|
|
|
|
userID = flag.String("user-id", "@userid:$SERVER_NAME", "The user ID to use as the event sender")
|
|
|
|
messageCount = flag.Int("message-count", 10, "The number of m.room.messsage events to generate")
|
|
|
|
format = flag.String("Format", "InputRoomEvent", "The output format to use for the messages: InputRoomEvent or Event")
|
2020-06-15 15:57:59 +00:00
|
|
|
ver = flag.String("version", string(gomatrixserverlib.RoomVersionV1), "Room version to generate events as")
|
2017-03-14 17:05:02 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// By default we use a private key of 0.
|
|
|
|
const defaultKey = "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
|
|
|
|
|
|
|
|
var privateKey ed25519.PrivateKey
|
|
|
|
var emptyString = ""
|
|
|
|
var now time.Time
|
|
|
|
var b gomatrixserverlib.EventBuilder
|
|
|
|
var eventID int
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
flag.Usage = func() {
|
|
|
|
fmt.Fprintf(os.Stderr, usage, os.Args[0])
|
|
|
|
flag.PrintDefaults()
|
|
|
|
}
|
|
|
|
|
|
|
|
flag.Parse()
|
|
|
|
*userID = strings.Replace(*userID, "$SERVER_NAME", *serverName, 1)
|
|
|
|
*roomID = strings.Replace(*roomID, "$SERVER_NAME", *serverName, 1)
|
|
|
|
|
|
|
|
// Decode the ed25519 private key.
|
|
|
|
privateKeyBytes, err := base64.RawStdEncoding.DecodeString(*privateKeyString)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
privateKey = ed25519.PrivateKey(privateKeyBytes)
|
|
|
|
|
|
|
|
// Build a m.room.create event.
|
|
|
|
b.Sender = *userID
|
|
|
|
b.RoomID = *roomID
|
|
|
|
b.Type = "m.room.create"
|
|
|
|
b.StateKey = &emptyString
|
2017-09-20 09:59:19 +00:00
|
|
|
b.SetContent(map[string]string{"creator": *userID}) // nolint: errcheck
|
2017-03-14 17:05:02 +00:00
|
|
|
create := buildAndOutput()
|
|
|
|
|
|
|
|
// Build a m.room.member event.
|
|
|
|
b.Type = "m.room.member"
|
|
|
|
b.StateKey = userID
|
2019-08-06 14:07:36 +00:00
|
|
|
b.SetContent(map[string]string{"membership": gomatrixserverlib.Join}) // nolint: errcheck
|
2017-03-14 17:05:02 +00:00
|
|
|
b.AuthEvents = []gomatrixserverlib.EventReference{create}
|
|
|
|
member := buildAndOutput()
|
|
|
|
|
|
|
|
// Build a number of m.room.message events.
|
|
|
|
b.Type = "m.room.message"
|
|
|
|
b.StateKey = nil
|
2017-09-20 09:59:19 +00:00
|
|
|
b.SetContent(map[string]string{"body": "Test Message"}) // nolint: errcheck
|
2017-03-14 17:05:02 +00:00
|
|
|
b.AuthEvents = []gomatrixserverlib.EventReference{create, member}
|
|
|
|
for i := 0; i < *messageCount; i++ {
|
|
|
|
buildAndOutput()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Build an event and write the event to the output.
|
|
|
|
func buildAndOutput() gomatrixserverlib.EventReference {
|
|
|
|
eventID++
|
|
|
|
now = time.Unix(0, 0)
|
2017-05-05 16:43:42 +00:00
|
|
|
name := gomatrixserverlib.ServerName(*serverName)
|
|
|
|
key := gomatrixserverlib.KeyID(*keyID)
|
|
|
|
|
2020-03-27 16:28:22 +00:00
|
|
|
event, err := b.Build(
|
|
|
|
now, name, key, privateKey,
|
2020-06-15 15:57:59 +00:00
|
|
|
gomatrixserverlib.RoomVersion(*ver),
|
2020-03-27 16:28:22 +00:00
|
|
|
)
|
2017-03-14 17:05:02 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
writeEvent(event)
|
|
|
|
reference := event.EventReference()
|
|
|
|
b.PrevEvents = []gomatrixserverlib.EventReference{reference}
|
|
|
|
b.Depth++
|
|
|
|
return reference
|
|
|
|
}
|
|
|
|
|
|
|
|
// Write an event to the output.
|
2020-11-16 15:44:53 +00:00
|
|
|
func writeEvent(event *gomatrixserverlib.Event) {
|
2017-03-14 17:05:02 +00:00
|
|
|
encoder := json.NewEncoder(os.Stdout)
|
|
|
|
if *format == "InputRoomEvent" {
|
|
|
|
var ire api.InputRoomEvent
|
|
|
|
ire.Kind = api.KindNew
|
2020-06-15 15:57:59 +00:00
|
|
|
ire.Event = event.Headered(gomatrixserverlib.RoomVersion(*ver))
|
2017-03-14 17:05:02 +00:00
|
|
|
authEventIDs := []string{}
|
2020-03-27 16:28:22 +00:00
|
|
|
for _, ref := range b.AuthEvents.([]gomatrixserverlib.EventReference) {
|
2017-03-14 17:05:02 +00:00
|
|
|
authEventIDs = append(authEventIDs, ref.EventID)
|
|
|
|
}
|
|
|
|
ire.AuthEventIDs = authEventIDs
|
|
|
|
if err := encoder.Encode(ire); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
} else if *format == "Event" {
|
|
|
|
if err := encoder.Encode(event); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
} else {
|
2019-05-21 20:56:55 +00:00
|
|
|
panic(fmt.Errorf("Format %q is not valid, must be %q or %q", *format, "InputRoomEvent", "Event"))
|
2017-03-14 17:05:02 +00:00
|
|
|
}
|
|
|
|
}
|