test(server): add basic benchmark test
This commit is contained in:
parent
86a29d632c
commit
d293ed52cc
2 changed files with 141 additions and 4 deletions
137
src/server/server_bench_test.go
Normal file
137
src/server/server_bench_test.go
Normal file
|
@ -0,0 +1,137 @@
|
||||||
|
package server
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
"os"
|
||||||
|
"sync"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/ihexxa/quickshare/src/client"
|
||||||
|
q "github.com/ihexxa/quickshare/src/handlers"
|
||||||
|
)
|
||||||
|
|
||||||
|
func BenchmarkUploadAndDownload(b *testing.B) {
|
||||||
|
addr := "http://127.0.0.1:8686"
|
||||||
|
rootPath := "tmpTestData"
|
||||||
|
config := `{
|
||||||
|
"users": {
|
||||||
|
"enableAuth": true,
|
||||||
|
"minUserNameLen": 2,
|
||||||
|
"minPwdLen": 4,
|
||||||
|
"captchaEnabled": false,
|
||||||
|
"uploadSpeedLimit": 409600,
|
||||||
|
"downloadSpeedLimit": 409600,
|
||||||
|
"spaceLimit": 1024,
|
||||||
|
"limiterCapacity": 1000,
|
||||||
|
"limiterCyc": 1000
|
||||||
|
},
|
||||||
|
"server": {
|
||||||
|
"debug": true,
|
||||||
|
"host": "127.0.0.1"
|
||||||
|
},
|
||||||
|
"fs": {
|
||||||
|
"root": "tmpTestData"
|
||||||
|
},
|
||||||
|
"db": {
|
||||||
|
"dbPath": "tmpTestData/quickshare"
|
||||||
|
}
|
||||||
|
}`
|
||||||
|
|
||||||
|
adminName := "qs"
|
||||||
|
adminPwd := "quicksh@re"
|
||||||
|
setUpEnv(b, rootPath, adminName, adminPwd)
|
||||||
|
defer os.RemoveAll(rootPath)
|
||||||
|
|
||||||
|
srv := startTestServer(config)
|
||||||
|
defer srv.Shutdown()
|
||||||
|
if !isServerReady(addr) {
|
||||||
|
b.Fatal("fail to start server")
|
||||||
|
}
|
||||||
|
|
||||||
|
adminUsersCli := client.NewUsersClient(addr)
|
||||||
|
resp, _, errs := adminUsersCli.Login(adminName, adminPwd)
|
||||||
|
if len(errs) > 0 {
|
||||||
|
b.Fatal(errs)
|
||||||
|
} else if resp.StatusCode != 200 {
|
||||||
|
b.Fatal(resp.StatusCode)
|
||||||
|
}
|
||||||
|
adminToken := client.GetCookie(resp.Cookies(), q.TokenCookie)
|
||||||
|
|
||||||
|
userCount := 5
|
||||||
|
userPwd := "1234"
|
||||||
|
users := addUsers(b, addr, userPwd, userCount, adminToken)
|
||||||
|
|
||||||
|
getFilePath := func(name string, i int) string {
|
||||||
|
return fmt.Sprintf("%s/files/home_file_%d", name, i)
|
||||||
|
}
|
||||||
|
|
||||||
|
filesCount := 10
|
||||||
|
clientErrs := []error{}
|
||||||
|
mockClient := func(name, pwd string, wg *sync.WaitGroup) {
|
||||||
|
defer wg.Done()
|
||||||
|
|
||||||
|
userUsersCli := client.NewUsersClient(addr)
|
||||||
|
resp, _, errs := userUsersCli.Login(name, pwd)
|
||||||
|
if len(errs) > 0 {
|
||||||
|
clientErrs = append(clientErrs, errs...)
|
||||||
|
return
|
||||||
|
} else if resp.StatusCode != 200 {
|
||||||
|
clientErrs = append(clientErrs, fmt.Errorf("failed to login"))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
files := map[string]string{}
|
||||||
|
content := "12345678"
|
||||||
|
for i := range make([]int, filesCount, filesCount) {
|
||||||
|
files[getFilePath(name, i)] = content
|
||||||
|
}
|
||||||
|
|
||||||
|
userToken := userUsersCli.Token()
|
||||||
|
for filePath, content := range files {
|
||||||
|
assertUploadOK(b, filePath, content, addr, userToken)
|
||||||
|
assertDownloadOK(b, filePath, content, addr, userToken)
|
||||||
|
}
|
||||||
|
|
||||||
|
filesCl := client.NewFilesClient(addr, userToken)
|
||||||
|
resp, lsResp, errs := filesCl.ListHome()
|
||||||
|
if len(errs) > 0 {
|
||||||
|
clientErrs = append(clientErrs, errs...)
|
||||||
|
return
|
||||||
|
} else if resp.StatusCode != 200 {
|
||||||
|
clientErrs = append(clientErrs, errors.New("failed to list home"))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if lsResp.Cwd != fmt.Sprintf("%s/files", name) {
|
||||||
|
clientErrs = append(clientErrs, fmt.Errorf("incorrct cwd (%s)", lsResp.Cwd))
|
||||||
|
return
|
||||||
|
|
||||||
|
} else if len(lsResp.Metadatas) != len(files) {
|
||||||
|
clientErrs = append(clientErrs, fmt.Errorf("incorrct metadata size (%d)", len(lsResp.Metadatas)))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
resp, _, errs = filesCl.Delete(getFilePath(name, 0))
|
||||||
|
if len(errs) > 0 {
|
||||||
|
clientErrs = append(clientErrs, errs...)
|
||||||
|
return
|
||||||
|
} else if resp.StatusCode != 200 {
|
||||||
|
clientErrs = append(clientErrs, errors.New("failed to add user"))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for i := 0; i < b.N; i++ {
|
||||||
|
var wg sync.WaitGroup
|
||||||
|
for userName := range users {
|
||||||
|
wg.Add(1)
|
||||||
|
go mockClient(userName, userPwd, &wg)
|
||||||
|
}
|
||||||
|
|
||||||
|
wg.Wait()
|
||||||
|
if len(clientErrs) > 0 {
|
||||||
|
b.Fatal(joinErrs(clientErrs))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -44,7 +44,7 @@ func startTestServer(config string) *Server {
|
||||||
return srv
|
return srv
|
||||||
}
|
}
|
||||||
|
|
||||||
func setUpEnv(t *testing.T, rootPath string, adminName, adminPwd string) {
|
func setUpEnv(t testing.TB, rootPath string, adminName, adminPwd string) {
|
||||||
os.Setenv("DEFAULTADMIN", adminName)
|
os.Setenv("DEFAULTADMIN", adminName)
|
||||||
os.Setenv("DEFAULTADMINPWD", adminPwd)
|
os.Setenv("DEFAULTADMINPWD", adminPwd)
|
||||||
os.RemoveAll(rootPath)
|
os.RemoveAll(rootPath)
|
||||||
|
@ -58,7 +58,7 @@ func getUserName(id int) string {
|
||||||
return fmt.Sprintf("user_%d", id)
|
return fmt.Sprintf("user_%d", id)
|
||||||
}
|
}
|
||||||
|
|
||||||
func addUsers(t *testing.T, addr, userPwd string, userCount int, adminToken *http.Cookie) map[string]string {
|
func addUsers(t testing.TB, addr, userPwd string, userCount int, adminToken *http.Cookie) map[string]string {
|
||||||
adminUsersCli := client.NewUsersClient(addr)
|
adminUsersCli := client.NewUsersClient(addr)
|
||||||
adminUsersCli.SetToken(adminToken)
|
adminUsersCli.SetToken(adminToken)
|
||||||
users := map[string]string{}
|
users := map[string]string{}
|
||||||
|
@ -115,7 +115,7 @@ func compareFileContent(fs fspkg.ISimpleFS, uid, filePath string, expectedConten
|
||||||
return string(gotContent) == expectedContent, nil
|
return string(gotContent) == expectedContent, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func assertUploadOK(t *testing.T, filePath, content, addr string, token *http.Cookie) bool {
|
func assertUploadOK(t testing.TB, filePath, content, addr string, token *http.Cookie) bool {
|
||||||
cl := client.NewFilesClient(addr, token)
|
cl := client.NewFilesClient(addr, token)
|
||||||
|
|
||||||
fileSize := int64(len([]byte(content)))
|
fileSize := int64(len([]byte(content)))
|
||||||
|
@ -141,7 +141,7 @@ func assertUploadOK(t *testing.T, filePath, content, addr string, token *http.Co
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
func assertDownloadOK(t *testing.T, filePath, content, addr string, token *http.Cookie) bool {
|
func assertDownloadOK(t testing.TB, filePath, content, addr string, token *http.Cookie) bool {
|
||||||
var (
|
var (
|
||||||
res *http.Response
|
res *http.Response
|
||||||
body string
|
body string
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue