Skip to content

Commit 8077ecd

Browse files
committed
BCDA-9702: Add auth token benchmark test
1 parent 0cd811d commit 8077ecd

File tree

3 files changed

+66
-1
lines changed

3 files changed

+66
-1
lines changed

.pre-commit-config.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,5 @@ repos:
77
rev: v2.10.1
88
hooks:
99
- id: golangci-lint
10-
language_version: 1.25.7
10+
language_version: 1.26.0
1111
args: ['-v']

README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,20 @@ make fhir_testing
281281

282282
See FHIR Testing [here](fhir_testing/README.md) for more info on the inferno tests and the FHIR Scan workflow.
283283

284+
#### Benchmark Testing
285+
286+
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:
287+
```
288+
docker compose -f docker-compose.test.yml run tests go test -v ./... -bench=. -benchtime=10x -run=^$ -benchmem
289+
```
290+
- `-v ./...` says to look for tests everywhere (not just current dir).
291+
- `-bench=.` sets to run all benchmark tests. Alternatively you can give a specific name eg `-bench=BenchmarkAuthToken`
292+
- `-benchtime=10x` means run all bunchmarks ten times each. This would normally be 1000's+.
293+
- `-run=^$` means run all tests starting with `$`. Basically an easy way to say dont run any tests, just run the benchmarks.
294+
- `-benchmem` means to add memory allocations per operation.
295+
The end result will include average time spent (nanoseconds) per operation, average bytes used per op, and number of memory allocations per op.
296+
297+
284298
### Environment variables
285299

286300
The various BCDA services (api, worker, ssas) require multiple environment variables and config files.

bcda/auth/api_test.go

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package auth_test
22

33
import (
4+
"context"
45
"database/sql"
56
"encoding/json"
67
"errors"
@@ -12,6 +13,7 @@ import (
1213
"strings"
1314
"testing"
1415

16+
appMiddleware "github.com/CMSgov/bcda-app/middleware"
1517
"github.com/go-chi/chi/v5"
1618
"github.com/golang-jwt/jwt/v5"
1719
"github.com/pborman/uuid"
@@ -21,6 +23,7 @@ import (
2123
customErrors "github.com/CMSgov/bcda-app/bcda/errors"
2224
"github.com/CMSgov/bcda-app/bcda/models/postgres"
2325
"github.com/CMSgov/bcda-app/bcda/testUtils"
26+
"github.com/CMSgov/bcda-app/bcda/web"
2427
bcdaLog "github.com/CMSgov/bcda-app/log"
2528

2629
"github.com/stretchr/testify/assert"
@@ -212,3 +215,51 @@ func (s *AuthAPITestSuite) TestWelcome() {
212215
func TestAuthAPITestSuite(t *testing.T) {
213216
suite.Run(t, new(AuthAPITestSuite))
214217
}
218+
219+
// benchmark tests below
220+
221+
func BenchmarkAuthToken(b *testing.B) {
222+
// set up generic background needs
223+
db := database.Connect()
224+
repository := postgres.NewRepository(db)
225+
provider := auth.NewProvider(db)
226+
baseApi := auth.NewBaseApi(provider)
227+
router := web.NewAuthRouter(provider)
228+
server := httptest.NewServer(router)
229+
ctx := context.Background()
230+
231+
// set up db records
232+
aco, err := repository.GetACOByCMSID(ctx, "A9994")
233+
if err != nil {
234+
b.Log("failed to get aco")
235+
b.FailNow()
236+
}
237+
err = repository.UpdateACO(ctx, aco.UUID, map[string]interface{}{"system_id": "2"})
238+
if err != nil {
239+
b.Logf("failed to update aco: %+v", err)
240+
b.FailNow()
241+
}
242+
creds, err := provider.ResetSecret(aco.ClientID)
243+
if err != nil {
244+
b.Logf("failed to reset secrets: %+v", err)
245+
b.FailNow()
246+
}
247+
248+
// set up request
249+
rr := httptest.NewRecorder()
250+
req, err := http.NewRequestWithContext(ctx, "POST", fmt.Sprintf("%s/auth/token", server.URL), nil)
251+
if err != nil {
252+
b.Log("failed to create request")
253+
b.FailNow()
254+
}
255+
req.Header.Add("Accept", constants.JsonContentType)
256+
req = req.WithContext(context.WithValue(req.Context(), appMiddleware.CtxTransactionKey, uuid.New()))
257+
req.SetBasicAuth(creds.ClientID, creds.ClientSecret)
258+
handler := http.HandlerFunc(baseApi.GetAuthToken)
259+
260+
b.ResetTimer()
261+
262+
for b.Loop() {
263+
handler.ServeHTTP(rr, req)
264+
}
265+
}

0 commit comments

Comments
 (0)