mirror of
https://github.com/golang/go
synced 2024-11-05 18:36:08 +00:00
c14b2689f0
Linux/amd64, 2 x Intel Xeon E5620, 8 HT cores, 2.40GHz benchmark old ns/op new ns/op delta BenchmarkFinalizer 420.00 261.00 -37.86% BenchmarkFinalizer-2 985.00 201.00 -79.59% BenchmarkFinalizer-4 1077.00 244.00 -77.34% BenchmarkFinalizer-8 1155.00 180.00 -84.42% BenchmarkFinalizer-16 1182.00 184.00 -84.43% BenchmarkFinalizerRun 2128.00 1378.00 -35.24% BenchmarkFinalizerRun-2 1655.00 1418.00 -14.32% BenchmarkFinalizerRun-4 1634.00 1522.00 -6.85% BenchmarkFinalizerRun-8 2213.00 1581.00 -28.56% BenchmarkFinalizerRun-16 2424.00 1599.00 -34.03% Darwin/amd64, Intel L9600, 2 cores, 2.13GHz benchmark old ns/op new ns/op delta BenchmarkChanCreation 1451.00 926.00 -36.18% BenchmarkChanCreation-2 3124.00 1412.00 -54.80% BenchmarkChanCreation-4 6121.00 2628.00 -57.07% BenchmarkFinalizer 684.00 420.00 -38.60% BenchmarkFinalizer-2 11195.00 398.00 -96.44% BenchmarkFinalizer-4 15862.00 654.00 -95.88% BenchmarkFinalizerRun 2025.00 1397.00 -31.01% BenchmarkFinalizerRun-2 3920.00 1447.00 -63.09% BenchmarkFinalizerRun-4 9471.00 1545.00 -83.69% R=golang-dev, cw, rsc CC=golang-dev https://golang.org/cl/4963057
77 lines
1.2 KiB
Go
77 lines
1.2 KiB
Go
// $G $D/$F.go && $L $F.$A && ./$A.out
|
|
|
|
// Copyright 2009 The Go Authors. All rights reserved.
|
|
// Use of this source code is governed by a BSD-style
|
|
// license that can be found in the LICENSE file.
|
|
|
|
// trivial finalizer test
|
|
|
|
package main
|
|
|
|
import (
|
|
"runtime"
|
|
"time"
|
|
)
|
|
|
|
const N = 250
|
|
|
|
type A struct {
|
|
b *B
|
|
n int
|
|
}
|
|
|
|
type B struct {
|
|
n int
|
|
}
|
|
|
|
var i int
|
|
var nfinal int
|
|
var final [N]int
|
|
|
|
// the unused return is to test finalizers with return values
|
|
func finalA(a *A) (unused [N]int) {
|
|
if final[a.n] != 0 {
|
|
println("finalA", a.n, final[a.n])
|
|
panic("fail")
|
|
}
|
|
final[a.n] = 1
|
|
return
|
|
}
|
|
|
|
func finalB(b *B) {
|
|
if final[b.n] != 1 {
|
|
println("finalB", b.n, final[b.n])
|
|
panic("fail")
|
|
}
|
|
final[b.n] = 2
|
|
nfinal++
|
|
}
|
|
|
|
func nofinalB(b *B) {
|
|
panic("nofinalB run")
|
|
}
|
|
|
|
func main() {
|
|
runtime.GOMAXPROCS(4)
|
|
for i = 0; i < N; i++ {
|
|
b := &B{i}
|
|
a := &A{b, i}
|
|
c := new(B)
|
|
runtime.SetFinalizer(c, nofinalB)
|
|
runtime.SetFinalizer(b, finalB)
|
|
runtime.SetFinalizer(a, finalA)
|
|
runtime.SetFinalizer(c, nil)
|
|
}
|
|
for i := 0; i < N; i++ {
|
|
runtime.GC()
|
|
runtime.Gosched()
|
|
time.Sleep(1e6)
|
|
if nfinal >= N*8/10 {
|
|
break
|
|
}
|
|
}
|
|
if nfinal < N*8/10 {
|
|
println("not enough finalizing:", nfinal, "/", N)
|
|
panic("fail")
|
|
}
|
|
}
|