golang--gopher北京大会(2)(rework)

时间:2022-06-01 16:53:21

三、七牛老许

qlang: github qiniu/qlang

microservice architecture: http://martinfowler.com/articles/microservices.html

Service Governance

  • Authorization
  • Logging
  • Change management
  • Central configuration
  • Scale in and scale out
  • Overload Protection
  • Service degradation
  • Monitor performance and health
  • Manage how and by whom services are used
  • Topology discovery and failure recovery

Overload Protection

  • N = Alert threshold
  • Important things
  • keep limit to N*2,not N
  • kill slow requests(SLA)

四、Dave Cheney 澳洲golang社区负责人,运行悉尼go用户组

http://dave.cheney.net/

Further reading: runtime/pprof

Further reading: Google Perf Tools

  • pprof

-memprofilerate=N adjusts the profile rate to 1/N

#visualise mem profile
go build -gcflags='-memprofile=/tmp/m.p'
go tool pprof --alloc_objects -svg $(go tool -n compile) /tmp/m.p > alloc_objects.svg
go tool pprof --inuse_objects -svg $(go tool -n compile) /tmp/m.p > alloc_objects.svg
  • benchmarking
#fib.go:
func Fib(n int) int {
if n<2 {
return n
}
return Fib(n-1)+Fib(n-2)
}
#fib_test.go:
import "testing"
func BenchmarkFib(b *testing.B) {
for n:=0;n<b.N;n++ {
Fib(20)
}
}
#go test -bench=. ./fib
    • do benchmark multiple times or use "-count=" flag
    • tools like rsc.io/benchstat are useful for comparing results
go test -bench=. -count=5 ./fib> old.txt
go test -bench=. -count=5 ./fib |tee new.txt
benchstat old.txt new.txt
benchstat {old,new}.txt
    • how fast will this bench? I think it's super fast!
package popcnt

import "testing"

const m1 = 0x5555555555
const m2 = 0x33333
const m4 = 0x0f0f0f
const h01 = 0x0101010101 func popcnt(x uint64) uint64 {
x -= (x >> 1) & m1
x = (x & m2) + ((x >> 2) & m2)
x = (x + (x >> 4)) & m4
return (x * h01) >> 56
}
func benchmarkPopcnt(b *testing.B) {
for i := 0; i < b.N; i++ {
popcnt(uint64(i)) //optimied away
}
}
#go test -bench=. ./popcnt
    • 但是被优化没有效果,要改为如下才有bench的效果
#change it to this:
var result uint64
func benchmarkPopcnt(b *testing.B) {
var r uint64
for i:=0;i<b.N;i++ {
r = popcnt(uint64(i))
}
result=r
}
    • 自己测试还是没效果,不知道为什么
  • CPU profile
go test -run=XXX -bench=IndexByte -cpuprofile=/tmp/c.p bytes
go tool pprof bytes.test /tmp/c.p
#note: -run=XXX to disable test,you only need benchmark
  • godoc
cd godoc
golang.org/x/tools/cmd/godoc
vim main.go
add one line in main--defer profile.Start().Stop()
then exec in vim:GoImports
go install -v .
godoc -http=9000
open browser and open localhost:9000
go tool pprof $(which godoc) /var/...cpu.pprof
  • enable output of GCloggin
env GODEBUG=gctrace=1 godoc -http=:8080
  • import net/http/pprof package will register a handler at /debug/pprof and default http.serveMux

it will visible if you use http.listenSndServe(adress,nil)

godoc -http:=8080,show /debug/pprof
  • some slice and string translate...

五、晚场辩论

rust-lang.org

//GO
err := f(x)
if err != nil {
retrun err;
} //Rust
try!(f(x));
f(x)?

一个关于冗长与magic的讨论

六、原射手CEO

gomobile和动态库的使用

七、米嘉

github.com/mijia/gobuildweb

github.com/mijia/web-starter-kit

@fswatch $(GO_FILES) $(TEMPLATES) | xargs -n1 -l{} make restart ||make kill
  • chubby--at the heart of google
  • go Proverbs: simple,Poetic,Pithy,no magic,composing,interface,adaptor
  • request --> onion --> response
  • basiclly we put a lot of things inside handler: like  parse request,query db,user auth, CRUD model objects, biz logic, then render the result to response xml,json etc. or panic/error.

golang--gopher北京大会(2)(rework)

  • go web packages:

dozens of frameworks

julienschmidt/httprouter

codegangstar/negroni

www.gorillatoolkit.org

  • go generate according to database scheme

github.com/mijia/modelg

  • go benchmark

load_test.go//测试1000000LRUcache

package demo1

import (
"fmt"
"testing"
"time"
) //TestPutPerf load tests
func TestPutPerf(t *testing.T) {
fmt.Printf("load testing on 1M size cache\n")
size := 1000000
cache := newLRUCache(size)
start := time.Now()
for i,td := range testData {
if i%10000 == 0 {
end := time.Now()
fmt.Printf("Num: %d, Used time: %v\n", i, end.Sub(start))
start = end
}
cache.Put(td.url,td.body)
}
}
go test -c
./demo1.test -test.run=Perf
//lru_cache_test.go
package demo1
import (
"fmt"
"testing"
) type testDataType struct {
url string
body string
} var testData []testDataType const testDataSize = 10000100 func init () {
testData = make([]testDataType, testDataSize)
for i:= 0; i< testDataSize; i++ {
url := fmt.Sprintf("www.fake.com/%d",i)
testData[i] = testDataType {
url: url,
body: fmt.Sprintf("This is page <b>%s</b>!",url),
}
}
} func TestBasics(t *testing.T) {
cache := newLRUCache(2)
cache.Put("1","1")
cache.Put("2","2")
res, ok := cache.Get("1")
if !ok := || res != "1" {
t.Errorf("unexpected!")
}
cache.Put("4","4")
if _, ok := cache.Get("2");ok {
t.Errorf("unexpected")
}
} //go tool pprof
func BenchmarkCacheFull1KSize(b *test.B) {
benchmarkCacheFull(b,1000)
}
func BenchmarkCacheFull1MSize(b *testing.B) {
benchmarkCacheFull(b,1000000)
}
func benchmarkCacheFull(b *testing.B, size int) {
cache := newLRUCache(size)
for i :=0;i<size;i++ {
cache.Put(testData[i].url,testData[i].body)
}
//两个Put之间有noise必须先做profiling,
//go tool pprof demo1.test prof.out -->#web
f, _ := os.Create("prof.out")
pprof.StartCPUProfile(f)
defer pprof.StopCPUProfile()
//先填满,之后测试每push 一个item需要多长时间
b.ResetTimer()
for i :=0; i< b.N; i++ {
c:=(i+size) % testDataSize
cache.Put(testData[c].url, testData[c].body)
}
} //demo2
//go test -v . -run XXX -bench Mill
//./demo2.test -test.run XXX -test.bench Milli -test.cpuprofile=prof.out
//go tool pprof demo2.test prof.out --> web
./demo1.test -test.run=xxx -test.bench Full
#./demo1.test -test.run=xxx -test.bench Full -test.cpuprofile=prof.out 这是不准确的,因为有很多noise,用上面方法

八、Marcel van Lohuizen -- Google Go team

golang.org/x/text 实现

  • I18n and L10n 国际化与本地化

搜索排序,大小写,双向文本,注入翻译,数字货币,日期,单位转换

  • Go uses UTF-8
const beijing="北京市"
for index,runeValue :=range beijing {
fmt.Printf("%#U 从第%d字节开始\n",runeValue,index)
}
//北从0,京从3,市从6
  • 文本的序列本质
const flags="