|
| 1 | +package album |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "github.com/qiangxue/go-rest-api/pkg/log" |
| 8 | +) |
| 9 | + |
| 10 | +func BenchmarkService_Create(b *testing.B) { |
| 11 | + logger, _ := log.NewForTest() |
| 12 | + s := NewService(&mockRepository{}, logger) |
| 13 | + ctx := context.Background() |
| 14 | + req := CreateAlbumRequest{Name: "benchmark album"} |
| 15 | + |
| 16 | + b.ResetTimer() |
| 17 | + for i := 0; i < b.N; i++ { |
| 18 | + _, _ = s.Create(ctx, req) |
| 19 | + } |
| 20 | +} |
| 21 | + |
| 22 | +func BenchmarkService_Get(b *testing.B) { |
| 23 | + logger, _ := log.NewForTest() |
| 24 | + repo := &mockRepository{} |
| 25 | + s := NewService(repo, logger) |
| 26 | + ctx := context.Background() |
| 27 | + |
| 28 | + // Setup: create an album first |
| 29 | + album, _ := s.Create(ctx, CreateAlbumRequest{Name: "test album"}) |
| 30 | + |
| 31 | + b.ResetTimer() |
| 32 | + for i := 0; i < b.N; i++ { |
| 33 | + _, _ = s.Get(ctx, album.ID) |
| 34 | + } |
| 35 | +} |
| 36 | + |
| 37 | +func BenchmarkService_Update(b *testing.B) { |
| 38 | + logger, _ := log.NewForTest() |
| 39 | + repo := &mockRepository{} |
| 40 | + s := NewService(repo, logger) |
| 41 | + ctx := context.Background() |
| 42 | + |
| 43 | + // Setup: create an album first |
| 44 | + album, _ := s.Create(ctx, CreateAlbumRequest{Name: "test album"}) |
| 45 | + req := UpdateAlbumRequest{Name: "updated album"} |
| 46 | + |
| 47 | + b.ResetTimer() |
| 48 | + for i := 0; i < b.N; i++ { |
| 49 | + _, _ = s.Update(ctx, album.ID, req) |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +func BenchmarkService_Delete(b *testing.B) { |
| 54 | + logger, _ := log.NewForTest() |
| 55 | + ctx := context.Background() |
| 56 | + |
| 57 | + b.ResetTimer() |
| 58 | + for i := 0; i < b.N; i++ { |
| 59 | + b.StopTimer() |
| 60 | + repo := &mockRepository{} |
| 61 | + s := NewService(repo, logger) |
| 62 | + album, _ := s.Create(ctx, CreateAlbumRequest{Name: "test album"}) |
| 63 | + b.StartTimer() |
| 64 | + |
| 65 | + _, _ = s.Delete(ctx, album.ID) |
| 66 | + } |
| 67 | +} |
| 68 | + |
| 69 | +func BenchmarkService_Query(b *testing.B) { |
| 70 | + logger, _ := log.NewForTest() |
| 71 | + repo := &mockRepository{} |
| 72 | + s := NewService(repo, logger) |
| 73 | + ctx := context.Background() |
| 74 | + |
| 75 | + // Setup: create multiple albums |
| 76 | + for i := 0; i < 10; i++ { |
| 77 | + _, _ = s.Create(ctx, CreateAlbumRequest{Name: "test album"}) |
| 78 | + } |
| 79 | + |
| 80 | + b.ResetTimer() |
| 81 | + for i := 0; i < b.N; i++ { |
| 82 | + _, _ = s.Query(ctx, 0, 10) |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +func BenchmarkService_Count(b *testing.B) { |
| 87 | + logger, _ := log.NewForTest() |
| 88 | + repo := &mockRepository{} |
| 89 | + s := NewService(repo, logger) |
| 90 | + ctx := context.Background() |
| 91 | + |
| 92 | + // Setup: create multiple albums |
| 93 | + for i := 0; i < 10; i++ { |
| 94 | + _, _ = s.Create(ctx, CreateAlbumRequest{Name: "test album"}) |
| 95 | + } |
| 96 | + |
| 97 | + b.ResetTimer() |
| 98 | + for i := 0; i < b.N; i++ { |
| 99 | + _, _ = s.Count(ctx) |
| 100 | + } |
| 101 | +} |
| 102 | + |
| 103 | +func BenchmarkCreateAlbumRequest_Validate(b *testing.B) { |
| 104 | + req := CreateAlbumRequest{Name: "test album"} |
| 105 | + |
| 106 | + b.ResetTimer() |
| 107 | + for i := 0; i < b.N; i++ { |
| 108 | + _ = req.Validate() |
| 109 | + } |
| 110 | +} |
| 111 | + |
| 112 | +func BenchmarkUpdateAlbumRequest_Validate(b *testing.B) { |
| 113 | + req := UpdateAlbumRequest{Name: "updated album"} |
| 114 | + |
| 115 | + b.ResetTimer() |
| 116 | + for i := 0; i < b.N; i++ { |
| 117 | + _ = req.Validate() |
| 118 | + } |
| 119 | +} |
0 commit comments