Skip to content

Commit

Permalink
Refactored some functionalities.
Browse files Browse the repository at this point in the history
  • Loading branch information
newacorn committed Oct 19, 2024
1 parent 63cef83 commit 438c070
Show file tree
Hide file tree
Showing 90 changed files with 48,726 additions and 5,286 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ tags
.DS_Store
vendor/
testdata/fuzz
testdata2/cantAccess
15 changes: 8 additions & 7 deletions allocation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,27 +10,27 @@ import (
func TestAllocationServeConn(t *testing.T) {
s := &Server{
Handler: func(ctx *RequestCtx) {

},
Concurrency: DefaultConcurrency,
}

rw := &readWriter{}
// Make space for the request and response here so it
// doesn't allocate within the test.
rw.r.Grow(1024)
rw.w.Grow(1024)

n := testing.AllocsPerRun(100, func() {
n := testing.AllocsPerRun(1000, func() {
rw.r.WriteString("GET / HTTP/1.1\r\nHost: google.com\r\nCookie: foo=bar\r\n\r\n")
if err := s.ServeConn(rw); err != nil {
t.Fatal(err)
}

// Reset the write buffer to make space for the next response.
rw.w.Reset()
})

if n != 0 {
t.Fatalf("expected 0 allocations, got %f", n)
if n != 2 {
t.Fatalf("expected 2 allocations, got %f", n)
}
}

Expand Down Expand Up @@ -63,8 +63,9 @@ func TestAllocationClient(t *testing.T) {
ReleaseResponse(res)
})

if n != 0 {
t.Fatalf("expected 0 allocations, got %f", n)
// host to string convert.
if n != 1 {
t.Fatalf("expected 1 allocations, got %f", n)
}
}

Expand Down
13 changes: 5 additions & 8 deletions args.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ import (
"io"
"sort"
"sync"

"github.com/valyala/bytebufferpool"
)

const (
Expand Down Expand Up @@ -285,7 +283,7 @@ func (a *Args) HasBytes(key []byte) bool {
var ErrNoArgValue = errors.New("no Args value for the given key")

// GetUint returns uint value for the given key.
func (a *Args) GetUint(key string) (int, error) {
func (a *Args) GetUint(key string) (int64, error) {
value := a.Peek(key)
if len(value) == 0 {
return -1, ErrNoArgValue
Expand All @@ -295,10 +293,9 @@ func (a *Args) GetUint(key string) (int, error) {

// SetUint sets uint value for the given key.
func (a *Args) SetUint(key string, value int) {
bb := bytebufferpool.Get()
bb.B = AppendUint(bb.B[:0], value)
a.SetBytesV(key, bb.B)
bytebufferpool.Put(bb)
dst := make([]byte, 20)
dst = AppendUintInto(dst, value)
a.SetBytesV(key, dst)
}

// SetUintBytes sets uint value for the given key.
Expand All @@ -309,7 +306,7 @@ func (a *Args) SetUintBytes(key []byte, value int) {
// GetUintOrZero returns uint value for the given key.
//
// Zero (0) is returned on error.
func (a *Args) GetUintOrZero(key string) int {
func (a *Args) GetUintOrZero(key string) int64 {
n, err := a.GetUint(key)
if err != nil {
n = 0
Expand Down
69 changes: 69 additions & 0 deletions args_bench_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package fasthttp

import (
"testing"
)

func BenchmarkSetUint(b *testing.B) {
arg := AcquireArgs()
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
arg.SetUint("b", 121232323)
}
ReleaseArgs(arg)
}

func BenchmarkSetUintWithoutPool(b *testing.B) {
arg := AcquireArgs()
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
arg.SetUintInto("b", 121232323)
}
ReleaseArgs(arg)
}

// SetUint sets uint value for the given key.
func (a *Args) SetUintInto(key string, value int) {
//bb := bytebufferpool.Get()
dst := make([]byte, 20)
dst = AppendUintInto(dst, value)
a.SetBytesV(key, dst)
}

/*
func TestDecode(t *testing.T) {
dataLen := [10]int{0, 10, 200, 65536}
for _, d := range dataLen {
dataStr := randomstring.HumanFriendlyString(d)
dataBytes := []byte(dataStr)
buf := bytes.Buffer{}
w := brotli.NewWriterV2(&buf, 4)
n, err := w.Write(dataBytes)
assert.Eq(t, n, len(dataBytes))
assert.Eq(t, err, nil)
err = w.Close()
assert.NoErr(t, err)
r := cbrotli.NewReader(&buf)
var dst = []byte("")
p := make([]byte, 100)
for {
n, err = r.Read(p)
if err != nil {
if err == io.EOF {
break
}
t.Fatal(err)
}
dst = append(dst, p[:n]...)
}
err = r.Close()
assert.NoErr(t, err)
assert.Eq(t, dataBytes, dst)
}
}
*/
Loading

0 comments on commit 438c070

Please sign in to comment.