Skip to content

Commit

Permalink
request: use bufio.Reader
Browse files Browse the repository at this point in the history
Before:
`goos: linux
goarch: amd64
pkg: github.com/thiagokokada/hyprland-go
cpu: AMD Ryzen 7 PRO 4750U with Radeon Graphics
BenchmarkSplash
BenchmarkSplash-16    	  35851	    34729 ns/op
PASS
ok  	github.com/thiagokokada/hyprland-go	1.595s``
```

After:
```
goos: linux
goarch: amd64
pkg: github.com/thiagokokada/hyprland-go
cpu: AMD Ryzen 7 PRO 4750U with Radeon Graphics
BenchmarkSplash
BenchmarkSplash-16    	  30976	    38695 ns/op
PASS
ok  	github.com/thiagokokada/hyprland-go	1.604s
```
  • Loading branch information
thiagokokada committed Jul 25, 2024
1 parent 634aa89 commit 1afff9c
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
5 changes: 4 additions & 1 deletion request.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package hyprland

import (
"bufio"
"bytes"
"encoding/json"
"errors"
Expand Down Expand Up @@ -240,6 +241,7 @@ func (c *RequestClient) RawRequest(request RawRequest) (response RawResponse, er
request,
)
}

_, err = conn.Write(request)
if err != nil {
return nil, fmt.Errorf("error while writing to socket: %w", err)
Expand All @@ -248,8 +250,9 @@ func (c *RequestClient) RawRequest(request RawRequest) (response RawResponse, er
// Get the response back
rbuf := bytes.NewBuffer(nil)
sbuf := make([]byte, BUF_SIZE)
reader := bufio.NewReader(conn)
for {
n, err := conn.Read(sbuf)
n, err := reader.Read(sbuf)
if err != nil {
if err == io.EOF {
break
Expand Down
9 changes: 9 additions & 0 deletions request_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,15 @@ func TestSplash(t *testing.T) {
testCommand(t, c.Splash, "")
}

func BenchmarkSplash(b *testing.B) {
if c == nil {
b.Skip("HYPRLAND_INSTANCE_SIGNATURE not set, skipping test")
}
for i := 0; i < b.N; i++ {
c.Splash()
}
}

func TestWorkspaces(t *testing.T) {
testCommand(t, c.Workspaces, []Workspace{})
}
Expand Down

0 comments on commit 1afff9c

Please sign in to comment.