diff --git a/tests/bench/bench.go b/tests/bench/bench.go new file mode 100644 index 0000000..68063ab --- /dev/null +++ b/tests/bench/bench.go @@ -0,0 +1,131 @@ +package main + +import ( + "context" + "net" + "net/http" + + "github.com/go-redis/redis/v8" + "github.com/integration-system/isp-kit/grpc" + "github.com/integration-system/isp-kit/grpc/client" + endpoint2 "github.com/integration-system/isp-kit/grpc/endpoint" + "github.com/integration-system/isp-kit/grpc/isp" + "github.com/integration-system/isp-kit/log" + "isp-gate-service/assembly" + "isp-gate-service/conf" + "isp-gate-service/domain" + "isp-gate-service/routes" +) + +type request struct { + Id string +} + +type response struct { + Id string +} + +//nolint +func main() { + redisCli := redis.NewClient(&redis.Options{}) + ctx := context.Background() + logger, err := log.New() + if err != nil { + panic(err) + } + defer func() { + _, _ = redisCli.Pipelined(ctx, func(p redis.Pipeliner) error { + p.Select(ctx, 0) + p.FlushDB(ctx) + p.Select(ctx, 1) + p.FlushDB(ctx) + p.Select(ctx, 2) + p.FlushDB(ctx) + return nil + }) + }() + config := conf.Remote{ + Redis: &conf.Redis{Address: "localhost:6379"}, + Secrets: conf.Secrets{AdminTokenSecret: "secret"}, + Http: conf.Http{MaxRequestBodySizeInMb: 1, ProxyTimeoutInSec: 15}, + Logging: conf.Logging{LogLevel: log.DebugLevel, RequestLogEnable: true, BodyLogEnable: true}, + Caching: conf.Caching{AuthorizationDataInSec: 15, AuthenticationDataInSec: 15}, + DailyLimits: []conf.DailyLimit{{ApplicationId: 1, RequestsPerDay: 100000000}}, + Throttling: []conf.Throttling{{ApplicationId: 1, RequestsPerSeconds: 5000}}, + } + systemService, systemCli := NewMock(logger) + systemService.Mock("system/secure/authenticate", func() domain.AuthenticateResponse { + return domain.AuthenticateResponse{ + Authenticated: true, + ErrorReason: "", + AuthData: &domain.AuthData{ + SystemId: 1, + DomainId: 1, + ServiceId: 1, + ApplicationId: 1, + }, + } + }).Mock("system/secure/authorize", func() domain.AuthorizeResponse { + return domain.AuthorizeResponse{Authorized: true} + }) + + targetService, targetCli := NewMock(logger) + targetService.Mock("endpoint", func(req request) response { + return response{Id: req.Id} + }) + targetClients := map[string]*client.Client{"target": targetCli} + logger, _ = log.New(log.WithLevel(log.DebugLevel)) + locator := assembly.NewLocator(logger, targetClients, nil, routes.NewRoutes(), systemCli) + locations := []conf.Location{{ + SkipAuth: false, + PathPrefix: "/api", + Protocol: "grpc", + TargetModule: "target", + }} + handler, _ := locator.Handler(config, locations, redisCli) + + srv := &http.Server{ + Addr: "localhost:8000", + Handler: handler, + } + srv.ListenAndServe() +} + +func TestServer(service isp.BackendServiceServer) (*grpc.Server, *client.Client) { + listener, _ := net.Listen("tcp", "127.0.0.1:") + srv := grpc.NewServer() + cli, _ := client.Default() + srv.Upgrade(service) + go func() { + _ = srv.Serve(listener) + }() + + cli.Upgrade([]string{listener.Addr().String()}) + return srv, cli +} + +type MockServer struct { + srv *grpc.Server + logger log.Logger + mockEndpoints map[string]interface{} +} + +func NewMock(logger log.Logger) (*MockServer, *client.Client) { + srv, cli := TestServer(grpc.NewMux()) + return &MockServer{ + srv: srv, + logger: logger, + mockEndpoints: make(map[string]interface{}), + }, cli +} + +func (m *MockServer) Mock(endpoint string, handler interface{}) *MockServer { + m.mockEndpoints[endpoint] = handler + wrapper := endpoint2.DefaultWrapper(m.logger) + muxer := grpc.NewMux() + for e, handler := range m.mockEndpoints { + muxer.Handle(e, wrapper.Endpoint(handler)) + } + m.srv.Upgrade(muxer) + return m +} diff --git a/tests/bench/body.json b/tests/bench/body.json new file mode 100644 index 0000000..fe45c5c --- /dev/null +++ b/tests/bench/body.json @@ -0,0 +1,3 @@ +{ + "id": "GO4IIZW5uav4cSo2zyR6i41LdalKytNH628yDhC92jXQF07gu4LS75JuzQgc6d3Dly48fwla9iHK6LPTJcvFbOcklXp7Zr9g3b180s0H3dVgaYGcO8BrAFxMVb2KcHyu" +} diff --git a/tests/bench/run.sh b/tests/bench/run.sh new file mode 100644 index 0000000..baec1f7 --- /dev/null +++ b/tests/bench/run.sh @@ -0,0 +1,2 @@ +bombardier -c 128 -t 1s -d 60s --fasthttp -H "x-application-token: token" -m POST -f body.json http://localhost:8000/api/endpoint +