Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
xujihui1985 committed Oct 29, 2018
1 parent 41acb9e commit d3ae2f4
Show file tree
Hide file tree
Showing 9 changed files with 267 additions and 0 deletions.
14 changes: 14 additions & 0 deletions profiling/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -162,5 +162,19 @@ go build
go tool trace t.out // analize the tracing file
```

#### toolexec

```bash
go build -toolexec="/usr/bin/time" cmd/compile/internal/gc
```
for linux user, we can use pref to profile go application

```
go build -toolexec="perf stat" cmd/compile/internal/gc
```

#### Flame graph

go-torch

Binary file added profiling/stream/c.out
Binary file not shown.
Binary file added profiling/stream/m.out
Binary file not shown.
144 changes: 144 additions & 0 deletions profiling/stream/stream.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
package stream

import (
"bytes"
"io"
)

var data = []struct {
input []byte
output []byte
}{
{[]byte("abc"), []byte("abc")},
{[]byte("elvis"), []byte("Elvis")},
{[]byte("aElvis"), []byte("aElvis")},
}

func assembleInputStream() []byte {
var in []byte
for _, d := range data {
in = append(in, d.input...)
}
return in
}

func assembleOutputStream() []byte {
var out []byte
for _, d := range data {
out = append(out, d.output...)
}
return out
}

func algOne(data []byte, find []byte, repl []byte, output *bytes.Buffer) {

input := bytes.NewBuffer(data)

size := len(find)

buf := make([]byte, size)
end := size - 1

if n, err := io.ReadFull(input, buf[:end]); err != nil {
output.Write(buf[:n])
return
}

for {
if _, err := io.ReadFull(input, buf[end:]); err != nil {
output.Write(buf[:end])
return
}

if bytes.Compare(buf, find) == 0 {
output.Write(repl)

if n, err := io.ReadFull(input, buf[:end]); err != nil {
output.Write(buf[:n])
return
}
continue
}
output.WriteByte(buf[0])
copy(buf, buf[1:])
}
}

func algTwo(data []byte, find []byte, repl []byte, output *bytes.Buffer) {

input := bytes.NewReader(data)

size := len(find)

idx := 0

for {

b, err := input.ReadByte()
if err != nil {
break
}

if b == find[idx] {

idx++

if idx == size {
output.Write(repl)
idx = 0
}
continue
}

if idx != 0 {
output.Write(find[:idx])

input.UnreadByte()

idx = 0
continue
}

output.WriteByte(b)
idx = 0
}
}

func algThree(data []byte, find []byte, repl []byte, output *bytes.Buffer) {

input := bytes.NewBuffer(data)

size := len(find)

buf := make([]byte, size)
end := size - 1

if n, err := input.Read(buf[:end]); err != nil {
output.Write(buf[:n])
return
}

for {

var err error
buf[end:][0], err = input.ReadByte()
if err != nil {
output.Write(buf[:end])
return
}
// if _, err := io.ReadFull(input, buf[end:]); err != nil {
// }

if bytes.Compare(buf, find) == 0 {
output.Write(repl)

if n, err := input.Read(buf[:end]); err != nil {
output.Write(buf[:n])
return
}
continue
}
output.WriteByte(buf[0])
copy(buf, buf[1:])
}
}
Binary file added profiling/stream/stream.test
Binary file not shown.
53 changes: 53 additions & 0 deletions profiling/stream/stream_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package stream

import (
"bytes"
"testing"
)

func BenchmarkAlgOne(b *testing.B) {
var output bytes.Buffer
in := assembleInputStream()

find := []byte("elvis")
repl := []byte("Elvis")

b.ResetTimer()

for i := 0; i < b.N; i++ {
output.Reset()
algOne(in, find, repl, &output)
}
}

func BenchmarkAlgTwo(b *testing.B) {

var output bytes.Buffer
in := assembleInputStream()

find := []byte("elvis")
repl := []byte("Elvis")

b.ResetTimer()

for i := 0; i < b.N; i++ {
output.Reset()
algTwo(in, find, repl, &output)
}
}

func BenchmarkAlgThree(b *testing.B) {

var output bytes.Buffer
in := assembleInputStream()

find := []byte("elvis")
repl := []byte("Elvis")

b.ResetTimer()

for i := 0; i < b.N; i++ {
output.Reset()
algThree(in, find, repl, &output)
}
}
22 changes: 22 additions & 0 deletions sync/pool/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package main

import (
"fmt"
"sync"
)

// const MaxPacketSize = 4096

var bufPool = sync.Pool{
New: func() interface{} {
return make([]byte, 2)
},
}

func main() {
a := [...]int{0, 1, 2, 3, 4, 5, 6}
b := bufPool.Get().([]byte)
fmt.Println(len(b), cap(b))
b[0] = 1
b[1] = 2
}
34 changes: 34 additions & 0 deletions syscall/mmap/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package main

import (
"fmt"
"os"
"syscall"
)

func main() {
f, err := os.OpenFile("mmap.bin", os.O_RDWR|os.O_CREATE, 0644)
if err != nil {
panic(err)
}

n, err := f.WriteAt([]byte{byte(0)}, 1<<8)
if err != nil {
panic(err)
}
fmt.Println("n = ", n)
data, err := syscall.Mmap(int(f.Fd()), 0, 1<<8, syscall.PROT_WRITE, syscall.MAP_SHARED)
if err != nil {
panic(err)
}

f.Close()

for i, v := range []byte("hello syscall") {
data[i] = v
}

fmt.Println(data)

syscall.Munmap(data)
}
Binary file added syscall/mmap/mmap.bin
Binary file not shown.

0 comments on commit d3ae2f4

Please sign in to comment.