load tests with Locust/Python, fixes

This commit is contained in:
Sergey Chubaryan 2024-08-17 15:09:29 +03:00
parent 73e7a25b11
commit 8dbb9de61a
14 changed files with 352 additions and 37 deletions

7
.gitignore vendored
View File

@ -1,6 +1,9 @@
# If you prefer the allow list template instead of the deny list, see community template: # If you prefer the allow list template instead of the deny list, see community template:
# https://github.com/github/gitignore/blob/main/community/Golang/Go.AllowList.gitignore # https://github.com/github/gitignore/blob/main/community/Golang/Go.AllowList.gitignore
#
# Ide
.vscode/*
# Binaries for programs and plugins # Binaries for programs and plugins
*.exe *.exe
*.exe~ *.exe~
@ -26,7 +29,7 @@ go.work.sum
# env file # env file
.env .env
.pyenv
.run .run
# temporary # temporary

16
.vscode/launch.json vendored
View File

@ -1,16 +0,0 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Launch Package",
"type": "go",
"request": "launch",
"mode": "auto",
"program": "${workspaceFolder}",
"args": ["-c", "./config_example/config.yaml", "-o", "./log.txt"]
}
]
}

2
go.mod
View File

@ -46,7 +46,7 @@ require (
github.com/ugorji/go/codec v1.2.12 // indirect github.com/ugorji/go/codec v1.2.12 // indirect
golang.org/x/arch v0.8.0 // indirect golang.org/x/arch v0.8.0 // indirect
golang.org/x/net v0.27.0 // indirect golang.org/x/net v0.27.0 // indirect
golang.org/x/sys v0.22.0 // indirect golang.org/x/sys v0.24.0 // indirect
golang.org/x/text v0.16.0 // indirect golang.org/x/text v0.16.0 // indirect
google.golang.org/protobuf v1.34.2 // indirect google.golang.org/protobuf v1.34.2 // indirect
gopkg.in/alexcesaro/quotedprintable.v3 v3.0.0-20150716171945-2caba252f4dc // indirect gopkg.in/alexcesaro/quotedprintable.v3 v3.0.0-20150716171945-2caba252f4dc // indirect

2
go.sum
View File

@ -116,6 +116,8 @@ golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.12.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.12.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.22.0 h1:RI27ohtqKCnwULzJLqkv897zojh5/DwS/ENaMzUOaWI= golang.org/x/sys v0.22.0 h1:RI27ohtqKCnwULzJLqkv897zojh5/DwS/ENaMzUOaWI=
golang.org/x/sys v0.22.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= golang.org/x/sys v0.22.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/sys v0.24.0 h1:Twjiwq9dn6R1fQcyiK+wQyHWfaz/BJB+YIpzU/Cv3Xg=
golang.org/x/sys v0.24.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/text v0.16.0 h1:a94ExnEXNtEwYLGJSIUxnWoxoRz/ZcCsV63ROupILh4= golang.org/x/text v0.16.0 h1:a94ExnEXNtEwYLGJSIUxnWoxoRz/ZcCsV63ROupILh4=
golang.org/x/text v0.16.0/go.mod h1:GhwF1Be+LQoKShO3cGOHzqOgRrGaYc9AvblQOmPVHnI= golang.org/x/text v0.16.0/go.mod h1:GhwF1Be+LQoKShO3cGOHzqOgRrGaYc9AvblQOmPVHnI=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4= golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4=

162
load_tests/.gitignore vendored Normal file
View File

@ -0,0 +1,162 @@
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class
# C extensions
*.so
# Distribution / packaging
.Python
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
wheels/
share/python-wheels/
*.egg-info/
.installed.cfg
*.egg
MANIFEST
# PyInstaller
# Usually these files are written by a python script from a template
# before PyInstaller builds the exe, so as to inject date/other infos into it.
*.manifest
*.spec
# Installer logs
pip-log.txt
pip-delete-this-directory.txt
# Unit test / coverage reports
htmlcov/
.tox/
.nox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*.cover
*.py,cover
.hypothesis/
.pytest_cache/
cover/
# Translations
*.mo
*.pot
# Django stuff:
*.log
local_settings.py
db.sqlite3
db.sqlite3-journal
# Flask stuff:
instance/
.webassets-cache
# Scrapy stuff:
.scrapy
# Sphinx documentation
docs/_build/
# PyBuilder
.pybuilder/
target/
# Jupyter Notebook
.ipynb_checkpoints
# IPython
profile_default/
ipython_config.py
# pyenv
# For a library or package, you might want to ignore these files since the code is
# intended to run in multiple environments; otherwise, check them in:
# .python-version
# pipenv
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
# However, in case of collaboration, if having platform-specific dependencies or dependencies
# having no cross-platform support, pipenv may install dependencies that don't work, or not
# install all needed dependencies.
#Pipfile.lock
# poetry
# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
# This is especially recommended for binary packages to ensure reproducibility, and is more
# commonly ignored for libraries.
# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
#poetry.lock
# pdm
# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
#pdm.lock
# pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it
# in version control.
# https://pdm.fming.dev/latest/usage/project/#working-with-version-control
.pdm.toml
.pdm-python
.pdm-build/
# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
__pypackages__/
# Celery stuff
celerybeat-schedule
celerybeat.pid
# SageMath parsed files
*.sage.py
# Environments
.env
.venv
env/
venv/
ENV/
env.bak/
venv.bak/
# Spyder project settings
.spyderproject
.spyproject
# Rope project settings
.ropeproject
# mkdocs documentation
/site
# mypy
.mypy_cache/
.dmypy.json
dmypy.json
# Pyre type checker
.pyre/
# pytype static type analyzer
.pytype/
# Cython debug symbols
cython_debug/
# PyCharm
# JetBrains specific template is maintained in a separate JetBrains.gitignore that can
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
# and can be added to the global gitignore or merged into this file. For a more nuclear
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
#.idea/

54
load_tests/locustfile.py Normal file
View File

@ -0,0 +1,54 @@
import random
import string
from locust import HttpUser, task
class DummyRoute(HttpUser):
@task
def dummy_test(self):
self.client.get("/dummy")
# @task
# def user_create_test(self):
# randEmail = ''.join(random.choices(string.ascii_lowercase + string.digits, k=10)) + '@test.test'
# randPassword = ''.join(random.choices(string.ascii_letters + string.digits, k=10))
# randName = ''.join(random.choices(string.ascii_letters, k=10))
# self.client.post(
# "/user/create",
# json={
# "email":randEmail,
# "password":randPassword,
# "name": randName,
# },
# )
def on_start(self):
randEmail = ''.join(random.choices(string.ascii_lowercase + string.digits, k=10)) + '@test.test'
randPassword = ''.join(random.choices(string.ascii_letters + string.digits, k=10))
randName = ''.join(random.choices(string.ascii_letters, k=10))
response = self.client.post(
"/user/create",
json={
"email":randEmail,
"password":randPassword,
"name": randName,
},
)
if response.status_code != 200:
raise AssertionError('can not create user')
response = self.client.post(
"/user/login",
json={
"email":randEmail,
"password":randPassword,
},
)
if response.status_code != 200:
raise AssertionError('can not login user')
token = response.json()['token']
if token == '':
raise AssertionError('empty user token')
self.client.headers = {"X-Auth": token}

15
load_tests/makefile Normal file
View File

@ -0,0 +1,15 @@
SHELL := /bin/bash
all: venv install
venv:
python3 -m pip install virtualenv
virtualenv .venv
# source .venv/bin/activate
install:
source .venv/bin/activate
python3 -m pip install -r requirements.txt
run-web:
locust --host http://localhost:8080

View File

@ -0,0 +1,27 @@
blinker==1.8.2
Brotli==1.1.0
certifi==2024.7.4
charset-normalizer==3.3.2
click==8.1.7
ConfigArgParse==1.7
Flask==3.0.3
Flask-Cors==4.0.1
Flask-Login==0.6.3
gevent==24.2.1
geventhttpclient==2.3.1
greenlet==3.0.3
idna==3.7
itsdangerous==2.2.0
Jinja2==3.1.4
locust==2.31.3
MarkupSafe==2.1.5
msgpack==1.0.8
psutil==6.0.0
pyzmq==26.1.0
requests==2.32.3
tomli==2.0.1
typing_extensions==4.12.2
urllib3==2.2.2
Werkzeug==3.0.3
zope.event==5.0
zope.interface==7.0.1

64
main.go
View File

@ -11,12 +11,17 @@ import (
"backend/src/logger" "backend/src/logger"
"backend/src/server/handlers" "backend/src/server/handlers"
"backend/src/server/middleware" "backend/src/server/middleware"
"context"
"crypto/rsa" "crypto/rsa"
"crypto/x509" "crypto/x509"
"database/sql" "database/sql"
"encoding/pem" "encoding/pem"
"fmt" "fmt"
"net"
"os" "os"
"os/signal"
"runtime/pprof"
"syscall"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"github.com/jackc/pgx" "github.com/jackc/pgx"
@ -40,6 +45,22 @@ func main() {
} }
logger.Log().Msg("initializing service...") logger.Log().Msg("initializing service...")
defer logger.Log().Msg("service stopped")
signals := []os.Signal{
os.Kill,
os.Interrupt,
syscall.SIGINT,
syscall.SIGTERM,
syscall.SIGKILL,
syscall.SIGSTOP,
syscall.SIGQUIT,
syscall.SIGABRT,
syscall.SIGHUP,
}
ctx, stop := signal.NotifyContext(context.Background(), signals...)
defer stop()
conf, err := config.NewFromFile(args.GetConfigPath()) conf, err := config.NewFromFile(args.GetConfigPath())
if err != nil { if err != nil {
@ -99,14 +120,16 @@ func main() {
}, },
) )
if !debugMode { // if !debugMode {
gin.SetMode(gin.ReleaseMode) gin.SetMode(gin.ReleaseMode)
} // }
r := gin.New() r := gin.New()
r.Use(middleware.NewRequestLogMiddleware(logger)) r.Use(middleware.NewRequestLogMiddleware(logger))
r.Use(gin.Recovery()) r.Use(gin.Recovery())
r.Static("/webapp", "./webapp")
linkGroup := r.Group("/s") linkGroup := r.Group("/s")
linkGroup.POST("/new", handlers.NewShortlinkCreateHandler(linkService)) linkGroup.POST("/new", handlers.NewShortlinkCreateHandler(linkService))
linkGroup.GET("/:linkId", handlers.NewShortlinkResolveHandler(linkService)) linkGroup.GET("/:linkId", handlers.NewShortlinkResolveHandler(linkService))
@ -116,17 +139,46 @@ func main() {
userGroup.POST("/login", handlers.NewUserLoginHandler(userService)) userGroup.POST("/login", handlers.NewUserLoginHandler(userService))
dummyGroup := r.Group("/dummy") dummyGroup := r.Group("/dummy")
dummyGroup.Use(middleware.NewAuthMiddleware(userService)) {
dummyGroup.GET("/", handlers.NewDummyHandler()) dummyGroup.Use(middleware.NewAuthMiddleware(userService))
dummyGroup.GET("", handlers.NewDummyHandler())
}
lpGroup := r.Group("/pooling") lpGroup := r.Group("/pooling")
lpGroup.GET("/", handlers.NewLongPoolingHandler(clientNotifier)) lpGroup.GET("/", handlers.NewLongPoolingHandler(clientNotifier))
if args.GetProfilePath() != "" {
pprofFile, err := os.Create(args.GetProfilePath())
if err != nil {
logger.Fatal().Err(err).Msg("can not create profile file")
}
if err := pprof.StartCPUProfile(pprofFile); err != nil {
logger.Fatal().Err(err).Msg("can not start cpu profiling")
}
defer func() {
logger.Log().Msg("stopping profiling...")
pprof.StopCPUProfile()
pprofFile.Close()
}()
}
listenAddr := fmt.Sprintf(":%d", conf.GetPort()) listenAddr := fmt.Sprintf(":%d", conf.GetPort())
logger.Log().Msgf("server listening on %s", listenAddr) logger.Log().Msgf("server listening on %s", listenAddr)
err = r.Run(listenAddr) listener, err := (&net.ListenConfig{}).Listen(ctx, "tcp", listenAddr)
if err != nil { if err != nil {
logger.Fatal().Err(err).Msg("can not create network listener")
}
go func() {
<-ctx.Done()
logger.Log().Msg("stopping service...")
listener.Close()
}()
err = r.RunListener(listener)
if err != nil && err == net.ErrClosed {
logger.Fatal().Err(err).Msg("server stopped with error") logger.Fatal().Err(err).Msg("server stopped with error")
} }
} }

View File

@ -1,4 +1,13 @@
all: release all: release
release: release:
go build -o ./.build/release/backend main.go GOEXPERIMENT=boringcrypto go build -ldflags "-s -w" -o ./.build/release/backend main.go
run: release
./.build/release/backend -c ./misc/config.yaml -o ./.run/log.txt -p ./.run/cpu.pprof
venv:
python3 -m pip install --user virtualenv
locust:
pip3 install locust

View File

@ -1,4 +1,3 @@
port: 8080 port: 8080
postgres_url: "postgres://postgres:postgres@localhost:5432/postgres" postgres_url: "postgres://postgres:postgres@localhost:5432/postgres"
jwt_signing_key: "./config_example/jwt_signing_key" jwt_signing_key: "./misc/jwt_signing_key"
log_file: "./log.txt"

View File

@ -5,6 +5,7 @@ import (
) )
type Args interface { type Args interface {
GetProfilePath() string
GetConfigPath() string GetConfigPath() string
GetLogPath() string GetLogPath() string
} }
@ -14,6 +15,7 @@ func Parse(osArgs []string) (Args, error) {
s := parser.String("c", "config", &argparse.Options{Required: true, Help: "Path to a config file"}) s := parser.String("c", "config", &argparse.Options{Required: true, Help: "Path to a config file"})
l := parser.String("o", "log", &argparse.Options{Required: false, Default: "", Help: "Path to a log file"}) l := parser.String("o", "log", &argparse.Options{Required: false, Default: "", Help: "Path to a log file"})
p := parser.String("p", "profile", &argparse.Options{Required: false, Default: "", Help: "Path to a cpu profile file"})
err := parser.Parse(osArgs) err := parser.Parse(osArgs)
if err != nil { if err != nil {
@ -21,14 +23,16 @@ func Parse(osArgs []string) (Args, error) {
} }
return &args{ return &args{
ConfigPath: *s, ConfigPath: *s,
LogPath: *l, LogPath: *l,
ProfilePath: *p,
}, nil }, nil
} }
type args struct { type args struct {
ConfigPath string ProfilePath string
LogPath string ConfigPath string
LogPath string
} }
func (a *args) GetConfigPath() string { func (a *args) GetConfigPath() string {
@ -38,3 +42,7 @@ func (a *args) GetConfigPath() string {
func (a *args) GetLogPath() string { func (a *args) GetLogPath() string {
return a.LogPath return a.LogPath
} }
func (a *args) GetProfilePath() string {
return a.ProfilePath
}

View File

@ -8,9 +8,9 @@ import (
) )
type createUserInput struct { type createUserInput struct {
Email string Email string `json:"email"`
Password string Password string `json:"password"`
Name string Name string `json:"name"`
} }
type createUserOutput struct { type createUserOutput struct {

View File

@ -8,8 +8,8 @@ import (
) )
type loginUserInput struct { type loginUserInput struct {
Login string Login string `json:"email"`
Password string Password string `json:"password"`
} }
type loginUserOutput struct { type loginUserOutput struct {