Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

In memory #6

Merged
merged 2 commits into from
Aug 6, 2023
Merged

In memory #6

merged 2 commits into from
Aug 6, 2023

Conversation

YaroslavPodorvanov
Copy link
Contributor

bench-go-sequence:
	docker exec -e MODE=sequence research-online-redis-go-app go test ./... -v -run=$$^ -bench='Go' -benchmem -benchtime=1000000x -count=10 | tee ./output/bench-go-10x-1000000x-sequence.txt
	benchstat ./output/bench-go-10x-1000000x-sequence.txt
cpu: Intel(R) Core(TM) i7-8750H CPU @ 2.20GHz
BenchmarkGoOnlineStorage
BenchmarkGoOnlineStorage-12    	 1000000	       511.6 ns/op	      87 B/op	       0 allocs/op
BenchmarkGoOnlineStorage-12    	 1000000	       497.7 ns/op	      87 B/op	       0 allocs/op
BenchmarkGoOnlineStorage-12    	 1000000	       495.0 ns/op	      87 B/op	       0 allocs/op
BenchmarkGoOnlineStorage-12    	 1000000	       512.1 ns/op	      87 B/op	       0 allocs/op
BenchmarkGoOnlineStorage-12    	 1000000	       501.4 ns/op	      87 B/op	       0 allocs/op
BenchmarkGoOnlineStorage-12    	 1000000	       535.0 ns/op	      87 B/op	       0 allocs/op
BenchmarkGoOnlineStorage-12    	 1000000	       495.9 ns/op	      87 B/op	       0 allocs/op
BenchmarkGoOnlineStorage-12    	 1000000	       495.6 ns/op	      87 B/op	       0 allocs/op
BenchmarkGoOnlineStorage-12    	 1000000	       497.4 ns/op	      87 B/op	       0 allocs/op
BenchmarkGoOnlineStorage-12    	 1000000	       541.0 ns/op	      87 B/op	       0 allocs/op
PASS
ok  	github.com/doutivity/research-online-redis-go	5.118s
name                time/op
GoOnlineStorage-12  508ns ± 6%

name                alloc/op
GoOnlineStorage-12  87.0B ± 0%

name                allocs/op
GoOnlineStorage-12   0.00     
bench-go-parallel:
	docker exec -e MODE=parallel research-online-redis-go-app go test ./... -v -run=$$^ -bench='Go' -benchmem -benchtime=1000000x -count=10 | tee ./output/bench-go-10x-1000000x-parallel.txt
	benchstat ./output/bench-go-10x-1000000x-parallel.txt
cpu: Intel(R) Core(TM) i7-8750H CPU @ 2.20GHz
BenchmarkGoOnlineStorage
BenchmarkGoOnlineStorage-12    	 1000000	       913.3 ns/op	      87 B/op	       0 allocs/op
BenchmarkGoOnlineStorage-12    	 1000000	       828.1 ns/op	      87 B/op	       0 allocs/op
BenchmarkGoOnlineStorage-12    	 1000000	       877.1 ns/op	      87 B/op	       0 allocs/op
BenchmarkGoOnlineStorage-12    	 1000000	       948.2 ns/op	      87 B/op	       0 allocs/op
BenchmarkGoOnlineStorage-12    	 1000000	       932.0 ns/op	      87 B/op	       0 allocs/op
BenchmarkGoOnlineStorage-12    	 1000000	       781.2 ns/op	      87 B/op	       0 allocs/op
BenchmarkGoOnlineStorage-12    	 1000000	       875.9 ns/op	      87 B/op	       0 allocs/op
BenchmarkGoOnlineStorage-12    	 1000000	      1067 ns/op	      87 B/op	       0 allocs/op
BenchmarkGoOnlineStorage-12    	 1000000	       765.8 ns/op	      87 B/op	       0 allocs/op
BenchmarkGoOnlineStorage-12    	 1000000	       752.3 ns/op	      87 B/op	       0 allocs/op
PASS
ok  	github.com/doutivity/research-online-redis-go	8.777s
name                time/op
GoOnlineStorage-12  874ns ±22%

name                alloc/op
GoOnlineStorage-12  87.0B ± 0%

name                allocs/op
GoOnlineStorage-12   0.00   

@YaroslavPodorvanov YaroslavPodorvanov self-assigned this Apr 5, 2023
@YaroslavPodorvanov
Copy link
Contributor Author

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) 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
}

@YaroslavPodorvanov YaroslavPodorvanov merged commit 4a784e6 into main Aug 6, 2023
@YaroslavPodorvanov YaroslavPodorvanov deleted the go-in-memory branch August 6, 2023 13:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant