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

Speed up scanning memcache responses, increase throughput. #74

Closed
Closed
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
48 changes: 48 additions & 0 deletions memcache/memcache.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,8 @@ var (
resultTouched = []byte("TOUCHED\r\n")

resultClientErrorPrefix = []byte("CLIENT_ERROR ")

valuePrefix = []byte("VALUE ")
)

// New returns a memcache client using the provided server(s)
Expand Down Expand Up @@ -495,6 +497,51 @@ func parseGetResponse(r *bufio.Reader, cb func(*Item)) error {

// scanGetResponseLine populates it and returns the declared size of the item.
// It does not read the bytes of the item.
// This should be equivalent to the commented out code, which was more time consuming due to reflection in Sscanf.
func scanGetResponseLine(line []byte, it *Item) (size int, err error) {
if len(line) < 13 {
// "VALUE x 0 1\r\n" is the shortest possible message, and that is 13 bytes long.
return -1, fmt.Errorf("Line is too short: %q", line)
}
if !bytes.Equal(line[:6], valuePrefix) {
return -1, fmt.Errorf("Expected line to begin with \"VALUE \": %q", line)
}
if !bytes.Equal(line[len(line)-2:], crlf) {
return -1, fmt.Errorf("Expected line to end with \\r\\n: %q", line)
}
line = line[6 : len(line)-2]
parts := bytes.Split(line, space)
// pattern := "VALUE %s %d %d %d\r\n"
// dest := []interface{}{&it.Key, &it.Flags, &size, &it.casid}
partsCount := len(parts)
if partsCount < 3 || partsCount > 4 {
return -1, fmt.Errorf("Expected line to match %%s %%s %%d [%%d]: got %q", line)
}
// "%s %d %d\n"
it.Key = string(parts[0])
if it.Key == "" {
return -1, fmt.Errorf("memcache: unexpected empty key in %q: %v", line, err)
}
flagsRaw, err := strconv.ParseUint(string(parts[1]), 10, 32)
it.Flags = uint32(flagsRaw)
if err != nil {
return -1, fmt.Errorf("memcache: unexpected flags in %q: %v", line, err)
}
sizeRaw, err := strconv.ParseInt(string(parts[2]), 10, 0)
if err != nil {
return -1, fmt.Errorf("memcache: unexpected size in %q: %v", line, err)
}
if partsCount >= 4 {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can it be larger than 4?

// "%s %d %d %d\n"
it.casid, err = strconv.ParseUint(string(parts[3]), 10, 64)
if err != nil {
return -1, fmt.Errorf("memcache: unexpected casid in %q: %v", line, err)
}
}
return int(sizeRaw), nil
}

/*
func scanGetResponseLine(line []byte, it *Item) (size int, err error) {
pattern := "VALUE %s %d %d %d\r\n"
dest := []interface{}{&it.Key, &it.Flags, &size, &it.casid}
Expand All @@ -508,6 +555,7 @@ func scanGetResponseLine(line []byte, it *Item) (size int, err error) {
}
return size, nil
}
*/

// Set writes the given item, unconditionally.
func (c *Client) Set(item *Item) error {
Expand Down
55 changes: 54 additions & 1 deletion memcache/memcache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,18 @@ import (
"os"
"os/exec"
"strings"
"sync"
"testing"
"time"
)

const testServer = "localhost:11211"

func setup(t *testing.T) bool {
type skippable interface {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(you can use testing.TB interface)

Skipf(format string, args ...interface{})
}

func setup(t skippable) bool {
c, err := net.Dial("tcp", testServer)
if err != nil {
t.Skipf("skipping test; no server running at %s", testServer)
Expand Down Expand Up @@ -287,3 +292,51 @@ func BenchmarkOnItem(b *testing.B) {
c.onItem(&item, dummyFn)
}
}

func BenchmarkSetGet(b *testing.B) {
if !setup(b) {
return
}
// Don't call flush_all
c := New(testServer)

checkErr := func(err error, format string, args ...interface{}) {
if err != nil {
b.Fatalf(format, args...)
}
}

benchmarkWorkerCount := 4
N := b.N
wg := sync.WaitGroup{}
wg.Add(benchmarkWorkerCount)
for j := 0; j < benchmarkWorkerCount; j++ {
j := j
go func() {
defer wg.Done()
expectedKey := fmt.Sprintf("foo%d", j+100)
for i := 0; i < N; i++ {
expectedValue := fmt.Sprintf("foo%d", i+10000000)

// Set
foo := &Item{Key: expectedKey, Value: []byte(expectedValue), Flags: 123}
err := c.Set(foo)
checkErr(err, "first set(%s): %v", expectedKey, err)

// Get
it, err := c.Get(expectedKey)
checkErr(err, "get(%s): %v", expectedKey, err)
if it.Key != expectedKey {
b.Errorf("get(%s) Key = %q, want %s", expectedKey, it.Key, expectedKey)
}
if string(it.Value) != expectedValue {
b.Errorf("get(%s) Value = %q, want %s", expectedKey, string(it.Value), expectedValue)
}
if it.Flags != 123 {
b.Errorf("get(foo) Flags = %v, want 123", it.Flags)
}
}
}()
}
wg.Wait()
}