Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@ repos:
rev: v2.10.1
hooks:
- id: golangci-lint
language_version: 1.25.7
language_version: 1.26.0
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice catch!

args: ['-v']
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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. To note the existing benchmark test assumes that you have existing data in the DB (ie have run `make load-fixtures`). 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
Copy link
Contributor

Choose a reason for hiding this comment

The 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?

Copy link
Contributor

Choose a reason for hiding this comment

The 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 load-fixtures command in there too, and/or reference that requirement in the readme.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The 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.
Expand Down
47 changes: 47 additions & 0 deletions bcda/auth/api_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package auth_test

import (
"context"
"database/sql"
"encoding/json"
"errors"
Expand All @@ -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"
Expand All @@ -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"
Expand Down Expand Up @@ -212,3 +215,47 @@ 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.Fatal("failed to get aco")
}
err = repository.UpdateACO(ctx, aco.UUID, map[string]interface{}{"system_id": "2"})
if err != nil {
b.Fatalf("failed to update aco: %+v", err)
}
creds, err := provider.ResetSecret(aco.ClientID)
if err != nil {
b.Fatalf("failed to reset secrets: %+v", err)
}

// set up request
rr := httptest.NewRecorder()
req, err := http.NewRequestWithContext(ctx, "POST", fmt.Sprintf("%s/auth/token", server.URL), nil)
if err != nil {
b.Fatal("failed to create request")
}
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)
}
}