Skip to content

Instantly share code, notes, and snippets.

@rabellamy
Last active October 12, 2019 09:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rabellamy/23b03995c0e1173421348368d8028f03 to your computer and use it in GitHub Desktop.
Save rabellamy/23b03995c0e1173421348368d8028f03 to your computer and use it in GitHub Desktop.

topics/go/profiling/memcpu

go test -bench . -benchtime 3s
go test -bench . -benchtime 3s -benchmem
go test -bench . -benchtime 3s -benchmem -memprofile p.out
go test -bench . -benchtime 3s -benchmem -memprofile p.out -gcflags "-newescape=false -m=2"
diff --git a/topics/go/profiling/memcpu/stream.go b/topics/go/profiling/memcpu/stream.go
index d070441f..b9a2a473 100644
--- a/topics/go/profiling/memcpu/stream.go
+++ b/topics/go/profiling/memcpu/stream.go
@@ -12,7 +12,6 @@ package main
 import (
 	"bytes"
 	"fmt"
-	"io"
 )
 
 // data represents a table of input and expected output.
@@ -86,19 +85,23 @@ func algOne(data []byte, find []byte, repl []byte, output *bytes.Buffer) {
 	size := len(find)
 
 	// Declare the buffers we need to process the stream.
-	buf := make([]byte, size)
+	buf := make([]byte, 5)
 	end := size - 1
 
 	// Read in an initial number of bytes we need to get started.
-	if n, err := io.ReadFull(input, buf[:end]); err != nil {
+	if n, err := input.ReadByte(); err != nil {
 		output.Write(buf[:n])
 		return
 	}
 
 	for {
 
+		var err error
+
+		buf[end:][0], err = input.ReadByte()
+
 		// Read in one byte from the input stream.
-		if _, err := io.ReadFull(input, buf[end:]); err != nil {
+		if err != nil {
 
 			// Flush the reset of the bytes we have.
 			output.Write(buf[:end])
@@ -110,7 +113,7 @@ func algOne(data []byte, find []byte, repl []byte, output *bytes.Buffer) {
 			output.Write(repl)
 
 			// Read a new initial number of bytes.
-			if n, err := io.ReadFull(input, buf[:end]); err != nil {
+			if n, err := input.ReadByte(); err != nil {
 				output.Write(buf[:n])
 				return
 			}

topics/go/profiling/project

GODEBUG=gctrace=1 ./project 2> >(grep -v 'scvg') > /dev/null

topics/go/profiling/trace

topics/go/testing/benchmarks/validate

go test -bench . -benchtime 3s -benchmem
go test -bench NumCPU -benchtime 3s -benchmem

topics/go/testing/benchmarks/cpu-bound

GOGC=off go test -cpu 1 -run none -bench . -benchtime 3s -benchmem
GOGC=off go test -run none -bench . -benchtime 3s -benchmem

topics/go/testing/benchmarks/io-bound

GOGC=off go test -cpu 1 -run none -bench . -benchtime 3s -benchmem
GOGC=off go test -run none -bench . -benchtime 3s -benchmem
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment