-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* In memory * prepare for new article https://dou.ua/forums/topic/44655/
- Loading branch information
1 parent
6a3094d
commit 4a784e6
Showing
10 changed files
with
275 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
name: Code quality | ||
|
||
on: | ||
push: | ||
branches: | ||
- main | ||
|
||
jobs: | ||
code-quality: | ||
name: Code quality | ||
runs-on: ubuntu-latest | ||
steps: | ||
- name: Checkout | ||
uses: actions/checkout@master | ||
|
||
- name: Set up Go | ||
uses: actions/setup-go@v4 | ||
with: | ||
go-version: '1.20' | ||
|
||
- name: Run go vet | ||
run: go vet ./... | ||
|
||
- name: Unit tests | ||
run: go test ./... -v -short |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package research_online_redis_go | ||
|
||
import ( | ||
"context" | ||
"sync" | ||
) | ||
|
||
type GoOnlineStorage struct { | ||
mu sync.Mutex | ||
data map[int64]int64 | ||
} | ||
|
||
func NewGoOnlineStorage() *GoOnlineStorage { | ||
return &GoOnlineStorage{ | ||
data: map[int64]int64{}, | ||
} | ||
} | ||
|
||
func (s *GoOnlineStorage) Store(ctx context.Context, pair UserOnlinePair) error { | ||
s.mu.Lock() | ||
defer s.mu.Unlock() | ||
|
||
s.data[pair.UserID] = pair.Timestamp | ||
|
||
return nil | ||
} | ||
|
||
func (s *GoOnlineStorage) BatchStore(ctx context.Context, pairs []UserOnlinePair) error { | ||
s.mu.Lock() | ||
defer s.mu.Unlock() | ||
|
||
for _, pair := range pairs { | ||
s.data[pair.UserID] = pair.Timestamp | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (s *GoOnlineStorage) Count(ctx context.Context) (int64, error) { | ||
s.mu.Lock() | ||
defer s.mu.Unlock() | ||
|
||
return int64(len(s.data)), nil | ||
} | ||
|
||
func (s *GoOnlineStorage) GetAndClear(ctx context.Context) ([]UserOnlinePair, error) { | ||
s.mu.Lock() | ||
defer s.mu.Unlock() | ||
|
||
result := make([]UserOnlinePair, 0, len(s.data)) | ||
for userID, timestamp := range s.data { | ||
result = append(result, UserOnlinePair{ | ||
UserID: userID, | ||
Timestamp: timestamp, | ||
}) | ||
} | ||
|
||
s.data = map[int64]int64{} | ||
|
||
return result, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
package research_online_redis_go | ||
|
||
import ( | ||
"context" | ||
"os" | ||
"sync/atomic" | ||
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestGoOnlineStorage(t *testing.T) { | ||
testMapOnlineStorage(t, NewGoOnlineStorage()) | ||
} | ||
|
||
func BenchmarkGoOnlineStorage(b *testing.B) { | ||
benchmarkMapOnlineStorage(b, NewGoOnlineStorage()) | ||
} | ||
|
||
func testMapOnlineStorage( | ||
t *testing.T, | ||
storage OnlineStorage, | ||
) { | ||
t.Helper() | ||
|
||
ctx := context.Background() | ||
|
||
expected := []UserOnlinePair{ | ||
{ | ||
UserID: 10000001, | ||
Timestamp: 1679800725, | ||
}, | ||
{ | ||
UserID: 10000002, | ||
Timestamp: 1679800730, | ||
}, | ||
{ | ||
UserID: 10000003, | ||
Timestamp: 1679800735, | ||
}, | ||
} | ||
|
||
for _, pair := range expected { | ||
err := storage.Store(ctx, pair) | ||
|
||
require.NoError(t, err) | ||
} | ||
|
||
actualCount, err := storage.Count(ctx) | ||
require.NoError(t, err) | ||
require.Equal(t, int64(len(expected)), int64(actualCount)) | ||
|
||
actual, err := storage.GetAndClear(ctx) | ||
require.NoError(t, err) | ||
|
||
requireUserOnlinePairsEqual(t, expected, actual) | ||
} | ||
|
||
func benchmarkMapOnlineStorage( | ||
b *testing.B, | ||
storage OnlineStorage, | ||
) { | ||
b.Helper() | ||
|
||
ctx := context.Background() | ||
|
||
var ( | ||
startTimestamp = time.Now().Unix() | ||
startUserID = int64(1e7) | ||
) | ||
|
||
b.ResetTimer() | ||
if os.Getenv("MODE") == "parallel" { | ||
var ( | ||
counter = int64(0) | ||
) | ||
|
||
b.RunParallel(func(pb *testing.PB) { | ||
for pb.Next() { | ||
index := atomic.AddInt64(&counter, 1) | ||
|
||
err := storage.Store(ctx, UserOnlinePair{ | ||
UserID: startUserID + index, | ||
Timestamp: startTimestamp + index, | ||
}) | ||
|
||
require.NoError(b, err) | ||
} | ||
}) | ||
} else { | ||
for index := int64(0); index < int64(b.N); index++ { | ||
err := storage.Store(ctx, UserOnlinePair{ | ||
UserID: startUserID + index, | ||
Timestamp: startTimestamp + index, | ||
}) | ||
|
||
require.NoError(b, err) | ||
} | ||
} | ||
|
||
actualCount, err := storage.Count(ctx) | ||
require.NoError(b, err) | ||
require.Equal(b, int64(b.N), actualCount) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.