فهرست منبع

Direct comparison.

Richard Crowley 12 سال پیش
والد
کامیت
c4c2de361f
2فایلهای تغییر یافته به همراه43 افزوده شده و 24 حذف شده
  1. 22 12
      debug_test.go
  2. 21 12
      runtime_test.go

+ 22 - 12
debug_test.go

@@ -1,8 +1,10 @@
 package metrics
 
 import (
+	// "runtime"
 	"runtime/debug"
 	"testing"
+	"time"
 )
 
 func BenchmarkDebugGCStats(b *testing.B) {
@@ -16,18 +18,26 @@ func BenchmarkDebugGCStats(b *testing.B) {
 
 func TestDebugGCStatsBlocking(t *testing.T) {
 	ch := make(chan int)
-	go func() {
-		i := 0
-		for {
-			select {
-			case ch <- i:
-				return
-			default:
-				i++
-			}
-		}
-	}()
+	go testDebugGCStatsBlocking(ch)
+	//runtime.Gosched()
 	var gcStats debug.GCStats
+	t0 := time.Now()
 	debug.ReadGCStats(&gcStats)
-	t.Log(<-ch)
+	t1 := time.Now()
+	t.Log("i++ during debug.ReadGCStats:", <-ch)
+	go testDebugGCStatsBlocking(ch)
+	time.Sleep(t1.Sub(t0))
+	t.Log("i++ during time.Sleep:", <-ch)
+}
+
+func testDebugGCStatsBlocking(ch chan int) {
+	i := 0
+	for {
+		select {
+		case ch <- i:
+			return
+		default:
+			i++
+		}
+	}
 }

+ 21 - 12
runtime_test.go

@@ -3,6 +3,7 @@ package metrics
 import (
 	"runtime"
 	"testing"
+	"time"
 )
 
 func BenchmarkRuntimeMemStats(b *testing.B) {
@@ -16,18 +17,26 @@ func BenchmarkRuntimeMemStats(b *testing.B) {
 
 func TestRuntimeMemStatsBlocking(t *testing.T) {
 	ch := make(chan int)
-	go func() {
-		i := 0
-		for {
-			select {
-			case ch <- i:
-				return
-			default:
-				i++
-			}
-		}
-	}()
+	go testRuntimeMemStatsBlocking(ch)
+	//runtime.Gosched()
 	var memStats runtime.MemStats
+	t0 := time.Now()
 	runtime.ReadMemStats(&memStats)
-	t.Log(<-ch)
+	t1 := time.Now()
+	t.Log("i++ during runtime.ReadMemStats:", <-ch)
+	go testRuntimeMemStatsBlocking(ch)
+	time.Sleep(t1.Sub(t0))
+	t.Log("i++ during time.Sleep:", <-ch)
+}
+
+func testRuntimeMemStatsBlocking(ch chan int) {
+	i := 0
+	for {
+		select {
+		case ch <- i:
+			return
+		default:
+			i++
+		}
+	}
 }