-
Notifications
You must be signed in to change notification settings - Fork 47
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve performance of remote exec in runnerv2 (#724)
This PR refactors the `runnerv2service` package to speed up the performance of executing programs producing large output. ### Test `cat` a large file and pipe the output to `/dev/null`. As the execution goes through the server, the execution involves marshaling and unmarshaling to Protobuf. #### This PR ``` time ./runme beta run --remote generate | cat - >/dev/null ./runme beta run --remote generate 0.70s user 0.64s system 33% cpu 3.973 total cat - > /dev/null 0.01s user 0.06s system 1% cpu 3.972 total ``` #### Main ``` time ./runme beta run --remote generate | cat - >/dev/null ./runme beta run --remote generate 5.58s user 6.89s system 146% cpu 8.537 total cat - > /dev/null 0.21s user 1.04s system 14% cpu 8.536 total ``` We have a speed increase of over 2x.
- Loading branch information
Showing
15 changed files
with
711 additions
and
707 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
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
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,84 @@ | ||
package runnerv2service | ||
|
||
import ( | ||
"bytes" | ||
"io" | ||
"sync" | ||
"sync/atomic" | ||
|
||
"github.com/pkg/errors" | ||
) | ||
|
||
const ( | ||
// msgBufferSize limits the size of data chunks | ||
// sent by the handler to clients. It's smaller | ||
// intentionally as typically the messages are | ||
// small. | ||
// In the future, it might be worth to implement | ||
// variable-sized buffers. | ||
msgBufferSize = 16 * 1024 * 1024 // 16 MiB | ||
) | ||
|
||
// buffer is a thread-safe buffer that returns EOF | ||
// only when it's closed. | ||
type buffer struct { | ||
mu *sync.Mutex | ||
// +checklocks:mu | ||
b *bytes.Buffer | ||
closed *atomic.Bool | ||
close chan struct{} | ||
more chan struct{} | ||
} | ||
|
||
var _ io.WriteCloser = (*buffer)(nil) | ||
|
||
func newBuffer(size int) *buffer { | ||
return &buffer{ | ||
mu: &sync.Mutex{}, | ||
b: bytes.NewBuffer(make([]byte, 0, size)), | ||
closed: &atomic.Bool{}, | ||
close: make(chan struct{}), | ||
more: make(chan struct{}), | ||
} | ||
} | ||
|
||
func (b *buffer) Write(p []byte) (int, error) { | ||
if b.closed.Load() { | ||
return 0, errors.New("closed") | ||
} | ||
|
||
b.mu.Lock() | ||
n, err := b.b.Write(p) | ||
b.mu.Unlock() | ||
|
||
select { | ||
case b.more <- struct{}{}: | ||
default: | ||
} | ||
|
||
return n, err | ||
} | ||
|
||
func (b *buffer) Close() error { | ||
if b.closed.CompareAndSwap(false, true) { | ||
close(b.close) | ||
} | ||
return nil | ||
} | ||
|
||
func (b *buffer) Read(p []byte) (int, error) { | ||
b.mu.Lock() | ||
n, err := b.b.Read(p) | ||
b.mu.Unlock() | ||
|
||
if err != nil && errors.Is(err, io.EOF) && !b.closed.Load() { | ||
select { | ||
case <-b.more: | ||
case <-b.close: | ||
return n, io.EOF | ||
} | ||
return n, nil | ||
} | ||
|
||
return n, err | ||
} |
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,30 @@ | ||
package runnerv2service | ||
|
||
import ( | ||
"github.com/stateful/runme/v3/internal/session" | ||
runnerv2 "github.com/stateful/runme/v3/pkg/api/gen/proto/go/runme/runner/v2" | ||
"github.com/stateful/runme/v3/pkg/project" | ||
) | ||
|
||
func convertSessionToProtoSession(sess *session.Session) *runnerv2.Session { | ||
return &runnerv2.Session{ | ||
Id: sess.ID, | ||
Env: sess.GetAllEnv(), | ||
// Metadata: sess.Metadata, | ||
} | ||
} | ||
|
||
// TODO(adamb): this function should not return nil project and nil error at the same time. | ||
func convertProtoProjectToProject(runnerProj *runnerv2.Project) (*project.Project, error) { | ||
if runnerProj == nil { | ||
return nil, nil | ||
} | ||
|
||
opts := project.DefaultProjectOptions[:] | ||
|
||
if runnerProj.EnvLoadOrder != nil { | ||
opts = append(opts, project.WithEnvFilesReadOrder(runnerProj.EnvLoadOrder)) | ||
} | ||
|
||
return project.NewDirProject(runnerProj.Root, opts...) | ||
} |
Oops, something went wrong.