-
Notifications
You must be signed in to change notification settings - Fork 17
BCDA-9702: Add auth token benchmark test #1326
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,5 +7,5 @@ repos: | |
| rev: v2.10.1 | ||
| hooks: | ||
| - id: golangci-lint | ||
| language_version: 1.25.7 | ||
| language_version: 1.26.0 | ||
| args: ['-v'] | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -281,6 +281,20 @@ make fhir_testing | |
|
|
||
| See FHIR Testing [here](fhir_testing/README.md) for more info on the inferno tests and the FHIR Scan workflow. | ||
|
|
||
| #### Benchmark Testing | ||
|
|
||
| Benchmark tests live in *_test.go files. They do not run as part of the normal testing suite. In order to run them you can do the following: | ||
| ``` | ||
| docker compose -f docker-compose.test.yml run tests go test -v ./... -bench=. -benchtime=10x -run=^$ -benchmem | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is it worth adding a Makefile command for this with some sensible defaults?
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since the test expects a test ACO to already be set up in the database, it might make sense to include a
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I started with a Makefile command but almost every time I ran it I wanted different values for the various options so abandoned it and put it in the README instead. I will add that note, although generally speaking Im not sure if you can run the test suite without loading fixtures at some point prior? |
||
| ``` | ||
| - `-v ./...` says to look for tests everywhere (not just current dir). | ||
| - `-bench=.` sets to run all benchmark tests. Alternatively you can give a specific name eg `-bench=BenchmarkAuthToken` | ||
| - `-benchtime=10x` means run all bunchmarks ten times each. This would normally be 1000's+. | ||
| - `-run=^$` means run all tests starting with `$`. Basically an easy way to say dont run any tests, just run the benchmarks. | ||
| - `-benchmem` means to add memory allocations per operation. | ||
| The end result will include average time spent (nanoseconds) per operation, average bytes used per op, and number of memory allocations per op. | ||
|
|
||
|
|
||
| ### Environment variables | ||
|
|
||
| The various BCDA services (api, worker, ssas) require multiple environment variables and config files. | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,7 @@ | ||
| package auth_test | ||
|
|
||
| import ( | ||
| "context" | ||
| "database/sql" | ||
| "encoding/json" | ||
| "errors" | ||
|
|
@@ -12,6 +13,7 @@ import ( | |
| "strings" | ||
| "testing" | ||
|
|
||
| appMiddleware "github.com/CMSgov/bcda-app/middleware" | ||
| "github.com/go-chi/chi/v5" | ||
| "github.com/golang-jwt/jwt/v5" | ||
| "github.com/pborman/uuid" | ||
|
|
@@ -21,6 +23,7 @@ import ( | |
| customErrors "github.com/CMSgov/bcda-app/bcda/errors" | ||
| "github.com/CMSgov/bcda-app/bcda/models/postgres" | ||
| "github.com/CMSgov/bcda-app/bcda/testUtils" | ||
| "github.com/CMSgov/bcda-app/bcda/web" | ||
| bcdaLog "github.com/CMSgov/bcda-app/log" | ||
|
|
||
| "github.com/stretchr/testify/assert" | ||
|
|
@@ -212,3 +215,51 @@ func (s *AuthAPITestSuite) TestWelcome() { | |
| func TestAuthAPITestSuite(t *testing.T) { | ||
| suite.Run(t, new(AuthAPITestSuite)) | ||
| } | ||
|
|
||
| // benchmark tests below | ||
|
|
||
| func BenchmarkAuthToken(b *testing.B) { | ||
| // set up generic background needs | ||
| db := database.Connect() | ||
| repository := postgres.NewRepository(db) | ||
| provider := auth.NewProvider(db) | ||
| baseApi := auth.NewBaseApi(provider) | ||
| router := web.NewAuthRouter(provider) | ||
| server := httptest.NewServer(router) | ||
| ctx := context.Background() | ||
|
|
||
| // set up db records | ||
| aco, err := repository.GetACOByCMSID(ctx, "A9994") | ||
| if err != nil { | ||
| b.Log("failed to get aco") | ||
| b.FailNow() | ||
| } | ||
| err = repository.UpdateACO(ctx, aco.UUID, map[string]interface{}{"system_id": "2"}) | ||
| if err != nil { | ||
| b.Logf("failed to update aco: %+v", err) | ||
| b.FailNow() | ||
| } | ||
| creds, err := provider.ResetSecret(aco.ClientID) | ||
| if err != nil { | ||
| b.Logf("failed to reset secrets: %+v", err) | ||
| b.FailNow() | ||
|
||
| } | ||
|
|
||
| // set up request | ||
| rr := httptest.NewRecorder() | ||
| req, err := http.NewRequestWithContext(ctx, "POST", fmt.Sprintf("%s/auth/token", server.URL), nil) | ||
| if err != nil { | ||
| b.Log("failed to create request") | ||
| b.FailNow() | ||
| } | ||
| req.Header.Add("Accept", constants.JsonContentType) | ||
| req = req.WithContext(context.WithValue(req.Context(), appMiddleware.CtxTransactionKey, uuid.New())) | ||
| req.SetBasicAuth(creds.ClientID, creds.ClientSecret) | ||
| handler := http.HandlerFunc(baseApi.GetAuthToken) | ||
|
|
||
| b.ResetTimer() | ||
|
|
||
| for b.Loop() { | ||
| handler.ServeHTTP(rr, req) | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice catch!