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

GO-3981 zip tracing on android is slow #1517

Merged
merged 5 commits into from
Aug 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions core/application/account_select.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"errors"
"os"
"path/filepath"
"runtime"
trace2 "runtime/trace"
"strings"

Expand Down Expand Up @@ -40,9 +41,10 @@ func (s *Service) AccountSelect(ctx context.Context, req *pb.RpcAccountSelectReq
return nil, ErrEmptyAccountID
}

s.traceRecorder.start()
defer s.traceRecorder.stop()

if runtime.GOOS != "android" && runtime.GOOS != "ios" {
s.traceRecorder.start()
defer s.traceRecorder.stop()
}
s.cancelStartIfInProcess()
s.lock.Lock()
defer s.lock.Unlock()
Expand Down
13 changes: 9 additions & 4 deletions core/application/profiler.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"sync"
"time"

"github.com/klauspost/compress/flate"
exptrace "golang.org/x/exp/trace"

"github.com/anyproto/anytype-heart/util/debug"
Expand Down Expand Up @@ -91,6 +92,9 @@ type zipFile struct {

func createZipArchive(w io.Writer, files []zipFile) error {
zipw := zip.NewWriter(w)
zipw.RegisterCompressor(zip.Deflate, func(w io.Writer) (io.WriteCloser, error) {
return flate.NewWriter(w, flate.BestSpeed)
})
err := func() error {
for _, file := range files {
f, err := zipw.Create(file.name)
Expand All @@ -107,8 +111,8 @@ func createZipArchive(w io.Writer, files []zipFile) error {
return errors.Join(err, zipw.Close())
}

func (s *Service) SaveLoginTrace() (string, error) {
return s.traceRecorder.save()
func (s *Service) SaveLoginTrace(dir string) (string, error) {
return s.traceRecorder.save(dir)
}

// traceRecorder is a helper to start and stop flight trace recorder
Expand All @@ -118,7 +122,8 @@ type traceRecorder struct {
lastRecordedBuf *bytes.Buffer // contains zip archive of trace
}

func (r *traceRecorder) save() (string, error) {
// empty dir means use system temp dir
func (r *traceRecorder) save(dir string) (string, error) {
r.lock.Lock()
defer r.lock.Unlock()

Expand All @@ -138,7 +143,7 @@ func (r *traceRecorder) save() (string, error) {
traceReader = buf
}

f, err := os.CreateTemp("", "account-select-trace-*.zip")
f, err := os.CreateTemp(dir, "account-select-trace-*.zip")
if err != nil {
return "", fmt.Errorf("create temp file: %w", err)
}
Expand Down
2 changes: 1 addition & 1 deletion core/debug.go
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ func (mw *Middleware) DebugRunProfiler(cctx context.Context, req *pb.RpcDebugRun
}

func (mw *Middleware) DebugAccountSelectTrace(cctx context.Context, req *pb.RpcDebugAccountSelectTraceRequest) *pb.RpcDebugAccountSelectTraceResponse {
path, err := mw.applicationService.SaveLoginTrace()
path, err := mw.applicationService.SaveLoginTrace(req.Dir)
if err != nil {
return &pb.RpcDebugAccountSelectTraceResponse{
Error: &pb.RpcDebugAccountSelectTraceResponseError{
Expand Down
13 changes: 13 additions & 0 deletions core/files/fileobject/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
"github.com/anyproto/anytype-heart/core/filestorage"
"github.com/anyproto/anytype-heart/core/filestorage/filesync"
"github.com/anyproto/anytype-heart/core/filestorage/rpcstore"
"github.com/anyproto/anytype-heart/core/session"
wallet2 "github.com/anyproto/anytype-heart/core/wallet"
"github.com/anyproto/anytype-heart/core/wallet/mock_wallet"
"github.com/anyproto/anytype-heart/pb"
Expand Down Expand Up @@ -68,6 +69,16 @@ func (c *dummyConfig) Name() string {
return "dummyConfig"
}

type dummyObjectArchiver struct{}

func (a *dummyObjectArchiver) SetPagesIsArchived(ctx session.Context, req pb.RpcObjectListSetIsArchivedRequest) error {
return nil
}

func (a *dummyObjectArchiver) Name() string { return "dummyObjectArchiver" }

func (a *dummyObjectArchiver) Init(_ *app.App) error { return nil }

const testResolveRetryDelay = 5 * time.Millisecond

func newFixture(t *testing.T) *fixture {
Expand All @@ -85,6 +96,7 @@ func newFixture(t *testing.T) *fixture {
eventSender.EXPECT().Broadcast(mock.Anything).Return().Maybe()
fileService := files.New()
spaceService := mock_space.NewMockService(t)
spaceService.EXPECT().GetPersonalSpace(mock.Anything).Return(nil, fmt.Errorf("not needed")).Maybe()
spaceIdResolver := mock_idresolver.NewMockResolver(t)

svc := New(testResolveRetryDelay, testResolveRetryDelay)
Expand Down Expand Up @@ -114,6 +126,7 @@ func newFixture(t *testing.T) *fixture {
a.Register(testutil.PrepareMock(ctx, a, mock_accountservice.NewMockService(ctrl)))
a.Register(testutil.PrepareMock(ctx, a, wallet))
a.Register(&config.Config{DisableFileConfig: true, NetworkMode: pb.RpcAccount_DefaultConfig, PeferYamuxTransport: true})
a.Register(&dummyObjectArchiver{})

err = a.Start(ctx)
require.NoError(t, err)
Expand Down
22 changes: 11 additions & 11 deletions core/peerstatus/status_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,10 +203,6 @@ func TestP2pStatus_SendPeerUpdate(t *testing.T) {
f.setNotPossibleStatus()
checkStatus(t, "spaceId", f.p2pStatus, NotPossible)

f.store.UpdateLocalPeer("peerId", []string{"spaceId"})
ctrl := gomock.NewController(t)
peer := mock_peer.NewMockPeer(ctrl)
peer.EXPECT().Id().Return("peerId")
f.sender.EXPECT().Broadcast(&pb.Event{
Messages: []*pb.EventMessage{
{
Expand All @@ -220,6 +216,10 @@ func TestP2pStatus_SendPeerUpdate(t *testing.T) {
},
},
})
f.store.UpdateLocalPeer("peerId", []string{"spaceId"})
ctrl := gomock.NewController(t)
peer := mock_peer.NewMockPeer(ctrl)
peer.EXPECT().Id().Return("peerId")
err := f.pool.AddPeer(context.Background(), peer)
assert.Nil(t, err)

Expand All @@ -234,13 +234,6 @@ func TestP2pStatus_SendPeerUpdate(t *testing.T) {
// when
checkStatus(t, "spaceId", f.p2pStatus, NotConnected)

f.store.UpdateLocalPeer("peerId", []string{"spaceId"})
ctrl := gomock.NewController(t)
peer := mock_peer.NewMockPeer(ctrl)
peer.EXPECT().Id().Return("peerId")
err := f.pool.AddPeer(context.Background(), peer)
assert.Nil(t, err)

f.sender.EXPECT().Broadcast(&pb.Event{
Messages: []*pb.EventMessage{
{
Expand All @@ -254,6 +247,13 @@ func TestP2pStatus_SendPeerUpdate(t *testing.T) {
},
},
})
f.store.UpdateLocalPeer("peerId", []string{"spaceId"})
ctrl := gomock.NewController(t)
peer := mock_peer.NewMockPeer(ctrl)
peer.EXPECT().Id().Return("peerId")
err := f.pool.AddPeer(context.Background(), peer)
assert.Nil(t, err)

checkStatus(t, "spaceId", f.p2pStatus, Connected)

// then
Expand Down
5 changes: 5 additions & 0 deletions docs/proto.md
Original file line number Diff line number Diff line change
Expand Up @@ -9732,6 +9732,11 @@ Get marks list in the selected range in text block.



| Field | Type | Label | Description |
| ----- | ---- | ----- | ----------- |
| dir | [string](#string) | | empty means using OS-provided temp dir |





Expand Down
Loading
Loading