@@ -3,8 +3,13 @@ package tfparse_test
3
3
import (
4
4
"archive/tar"
5
5
"bytes"
6
+ "context"
7
+ "io"
8
+ "log"
6
9
"testing"
7
10
11
+ "cdr.dev/slog"
12
+ "cdr.dev/slog/sloggers/sloghuman"
8
13
"cdr.dev/slog/sloggers/slogtest"
9
14
"github.com/coder/coder/v2/archive"
10
15
"github.com/coder/coder/v2/provisioner/terraform/tfparse"
@@ -326,7 +331,7 @@ func Test_WorkspaceTagDefaultsFromFile(t *testing.T) {
326
331
}
327
332
}
328
333
329
- func createTar (t * testing.T , files map [string ]string ) []byte {
334
+ func createTar (t testing.TB , files map [string ]string ) []byte {
330
335
var buffer bytes.Buffer
331
336
writer := tar .NewWriter (& buffer )
332
337
for path , content := range files {
@@ -348,10 +353,72 @@ func createTar(t *testing.T, files map[string]string) []byte {
348
353
return buffer .Bytes ()
349
354
}
350
355
351
- func createZip (t * testing.T , files map [string ]string ) []byte {
356
+ func createZip (t testing.TB , files map [string ]string ) []byte {
352
357
ta := createTar (t , files )
353
358
tr := tar .NewReader (bytes .NewReader (ta ))
354
359
za , err := archive .CreateZipFromTar (tr , int64 (len (ta )))
355
360
require .NoError (t , err )
356
361
return za
357
362
}
363
+
364
+ // Current benchmark results before any changes / caching.
365
+ // goos: linux
366
+ // goarch: amd64
367
+ // pkg: github.com/coder/coder/v2/provisioner/terraform/tfparse
368
+ // cpu: AMD EPYC 7502P 32-Core Processor
369
+ // BenchmarkWorkspaceTagDefaultsFromFile/Tar-16 766 1493850 ns/op 339935 B/op 2238 allocs/op
370
+ // BenchmarkWorkspaceTagDefaultsFromFile/Zip-16 706 1633258 ns/op 389421 B/op 2296 allocs/op
371
+ // PASS
372
+ func BenchmarkWorkspaceTagDefaultsFromFile (b * testing.B ) {
373
+ files := map [string ]string {
374
+ "main.tf" : `
375
+ provider "foo" {}
376
+ resource "foo_bar" "baz" {}
377
+ variable "region" {
378
+ type = string
379
+ default = "us"
380
+ }
381
+ data "coder_parameter" "az" {
382
+ name = "az"
383
+ type = "string"
384
+ default = "a"
385
+ }
386
+ data "coder_workspace_tags" "tags" {
387
+ tags = {
388
+ "platform" = "kubernetes",
389
+ "cluster" = "${"devel"}${"opers"}"
390
+ "region" = var.region
391
+ "az" = data.coder_parameter.az.value
392
+ }
393
+ }` ,
394
+ }
395
+ tarFile := createTar (b , files )
396
+ zipFile := createZip (b , files )
397
+ logger := discardLogger (b )
398
+ b .ResetTimer ()
399
+ b .Run ("Tar" , func (b * testing.B ) {
400
+ ctx := context .Background ()
401
+ for i := 0 ; i < b .N ; i ++ {
402
+ _ , err := tfparse .WorkspaceTagDefaultsFromFile (ctx , logger , tarFile , "application/x-tar" )
403
+ if err != nil {
404
+ b .Fatal (err )
405
+ }
406
+ }
407
+ })
408
+
409
+ b .Run ("Zip" , func (b * testing.B ) {
410
+ ctx := context .Background ()
411
+ for i := 0 ; i < b .N ; i ++ {
412
+ _ , err := tfparse .WorkspaceTagDefaultsFromFile (ctx , logger , zipFile , "application/zip" )
413
+ if err != nil {
414
+ b .Fatal (err )
415
+ }
416
+ }
417
+ })
418
+ }
419
+
420
+ func discardLogger (t testing.TB ) slog.Logger {
421
+ l := slog .Make (sloghuman .Sink (io .Discard ))
422
+ log .SetOutput (slog .Stdlib (context .Background (), l , slog .LevelInfo ).Writer ())
423
+ return l
424
+ }
0 commit comments