Skip to content

Commit

Permalink
request: use bufio.Reader and bufio.Writer
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``
```

Bufio.Reader:
```
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
```

Bufio.Reader + Bufio.Writer:
```
goos: linux
goarch: amd64
pkg: github.com/thiagokokada/hyprland-go
cpu: AMD Ryzen 7 PRO 4750U with Radeon Graphics
BenchmarkSplash
BenchmarkSplash-16    	  29901	    40494 ns/op
PASS
ok  	github.com/thiagokokada/hyprland-go	1.630s
```
  • Loading branch information
thiagokokada committed Jul 25, 2024
1 parent 634aa89 commit 7dd51de
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
9 changes: 7 additions & 2 deletions 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,16 +241,20 @@ func (c *RequestClient) RawRequest(request RawRequest) (response RawResponse, er
request,
)
}
_, err = conn.Write(request)

writer := bufio.NewWriter(conn)
_, err = writer.Write(request)
if err != nil {
return nil, fmt.Errorf("error while writing to socket: %w", err)
}
writer.Flush()

// 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 7dd51de

Please sign in to comment.