github.com/ActiveState/cli@v0.0.0-20240508170324-6801f60cd051/test/integration/performance_svc_int_test.go (about) 1 package integration 2 3 import ( 4 "fmt" 5 "testing" 6 "time" 7 8 "github.com/ActiveState/cli/internal/constants" 9 "github.com/ActiveState/cli/internal/errs" 10 "github.com/ActiveState/cli/internal/installation" 11 "github.com/ActiveState/cli/internal/installation/storage" 12 "github.com/ActiveState/cli/internal/logging" 13 "github.com/ActiveState/cli/internal/svcctl" 14 "github.com/ActiveState/cli/internal/testhelpers/e2e" 15 "github.com/ActiveState/cli/internal/testhelpers/suite" 16 "github.com/ActiveState/cli/internal/testhelpers/tagsuite" 17 "github.com/ActiveState/cli/pkg/platform/model" 18 "golang.org/x/net/context" 19 ) 20 21 var ( 22 SvcEnsureStartMaxTime = 1000 * time.Millisecond // https://activestatef.atlassian.net/browse/DX-935 23 SvcRequestMaxTime = 200 * time.Millisecond 24 SvcStopMaxTime = 333 * time.Millisecond 25 ) 26 27 type PerformanceSvcIntegrationTestSuite struct { 28 tagsuite.Suite 29 } 30 31 func (suite *PerformanceIntegrationTestSuite) TearDownSuite() { 32 ipcClient := svcctl.NewDefaultIPCClient() 33 err := svcctl.StopServer(ipcClient) 34 suite.Require().NoError(err) 35 } 36 37 func (suite *PerformanceIntegrationTestSuite) TestSvcPerformance() { 38 suite.OnlyRunForTags(tagsuite.Performance) 39 ts := e2e.New(suite.T(), false) 40 defer ts.Close() 41 42 // This integration test is a bit special because it bypasses the spawning logic 43 // so in order to get the right log files when debugging we manually provide the config dir 44 var err error 45 ts.Dirs.Config, err = storage.AppDataPath() 46 suite.Require().NoError(err) 47 48 ipcClient := svcctl.NewDefaultIPCClient() 49 var svcPort string 50 51 suite.Run("StartServer", func() { 52 svcExec, err := installation.ServiceExecFromDir(ts.Dirs.Bin) 53 suite.Require().NoError(err, errs.JoinMessage(err)) 54 55 t := time.Now() 56 svcPort, err = svcctl.EnsureExecStartedAndLocateHTTP(ipcClient, svcExec, "from test", nil) 57 suite.Require().NoError(err, ts.DebugMessage(fmt.Sprintf("Error: %s\nLog Tail:\n%s", errs.JoinMessage(err), logging.ReadTail()))) 58 duration := time.Since(t) 59 60 if duration.Nanoseconds() > SvcEnsureStartMaxTime.Nanoseconds() { 61 suite.Fail(fmt.Sprintf("Service start took too long: %s (should be under %s)", duration.String(), SvcEnsureStartMaxTime.String())) 62 } 63 }) 64 65 svcmodel := model.NewSvcModel(svcPort) 66 svcmodel.EnableDebugLog() 67 68 suite.Run("Query StateVersion", func() { 69 t := time.Now() 70 _, err := svcmodel.StateVersion(context.Background()) 71 suite.Require().NoError(err, ts.DebugMessage(fmt.Sprintf("Error: %s\nLog Tail:\n%s", errs.JoinMessage(err), logging.ReadTail()))) 72 duration := time.Since(t) 73 74 if duration.Nanoseconds() > SvcRequestMaxTime.Nanoseconds() { 75 suite.Fail(fmt.Sprintf("Service request took too long: %s (should be under %s)", duration.String(), SvcRequestMaxTime.String())) 76 } 77 }) 78 79 suite.Run("Query Analytics", func() { 80 t := time.Now() 81 err := svcmodel.AnalyticsEvent(context.Background(), "performance-test", "performance-test", "performance-test", "performance-test", "{}") 82 suite.Require().NoError(err, ts.DebugMessage(fmt.Sprintf("Error: %s\nLog Tail:\n%s", errs.JoinMessage(err), logging.ReadTail()))) 83 duration := time.Since(t) 84 85 if duration.Nanoseconds() > SvcRequestMaxTime.Nanoseconds() { 86 suite.Fail(fmt.Sprintf("Service analytics request took too long: %s (should be under %s)", duration.String(), SvcRequestMaxTime.String())) 87 } 88 }) 89 90 suite.Run("Query Update", func() { 91 t := time.Now() 92 _, err := svcmodel.CheckUpdate(context.Background(), constants.ChannelName, "") 93 suite.Require().NoError(err, ts.DebugMessage(fmt.Sprintf("Error: %s\nLog Tail:\n%s", errs.JoinMessage(err), logging.ReadTail()))) 94 duration := time.Since(t) 95 96 if duration.Nanoseconds() > SvcRequestMaxTime.Nanoseconds() { 97 suite.Fail(fmt.Sprintf("Service update request took too long: %s (should be under %s)", duration.String(), SvcRequestMaxTime.String())) 98 } 99 }) 100 101 suite.Run("StopServer", func() { 102 t := time.Now() 103 err := svcctl.StopServer(ipcClient) 104 suite.Require().NoError(err, ts.DebugMessage(fmt.Sprintf("Error: %s\nLog Tail:\n%s", errs.JoinMessage(err), logging.ReadTail()))) 105 duration := time.Since(t) 106 107 if duration.Nanoseconds() > SvcStopMaxTime.Nanoseconds() { 108 suite.Fail(fmt.Sprintf("Service request took too long: %s (should be under %s)", duration.String(), SvcStopMaxTime.String())) 109 } 110 }) 111 } 112 113 func TestPerformanceSvcIntegrationTestSuite(t *testing.T) { 114 suite.Run(t, new(PerformanceSvcIntegrationTestSuite)) 115 }