From 0d56885726891dfa402cbcf656f09655d61ce6ae Mon Sep 17 00:00:00 2001 From: John Date: Wed, 17 Jan 2018 10:28:42 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E5=96=84grpool=E6=B5=8B=E8=AF=95?= =?UTF-8?q?=E8=84=9A=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- g/os/grpool/grpool_test.go | 41 +++++++++---------------------- g/os/grpool/grpool_testb1_test.go | 30 ++++++++++++++++++++++ g/os/grpool/grpool_testb2_test.go | 34 +++++++++++++++++++++++++ 3 files changed, 76 insertions(+), 29 deletions(-) create mode 100644 g/os/grpool/grpool_testb1_test.go create mode 100644 g/os/grpool/grpool_testb2_test.go diff --git a/g/os/grpool/grpool_test.go b/g/os/grpool/grpool_test.go index d00577708..7c1c25550 100644 --- a/g/os/grpool/grpool_test.go +++ b/g/os/grpool/grpool_test.go @@ -4,30 +4,27 @@ // If a copy of the MIT was not distributed with this file, // You can obtain one at https://gitee.com/johng/gf. -// go test *.go -bench=".*" - package grpool_test import ( - "testing" - "runtime" "fmt" + "runtime" + "testing" + "gitee.com/johng/gf/g/os/grpool" ) -var n = 5000000 - func increment() { for i := 0; i < 1000000; i++ {} } -//func Test_GrpoolMemUsage(t *testing.T) { -// for i := 0; i < n; i++ { -// grpool.Add(increment) -// } -// mem := runtime.MemStats{} -// runtime.ReadMemStats(&mem) -// fmt.Println("mem usage:", mem.TotalAlloc/1024) -//} +func Test_GrpoolMemUsage(t *testing.T) { + for i := 0; i < n; i++ { + grpool.Add(increment) + } + mem := runtime.MemStats{} + runtime.ReadMemStats(&mem) + fmt.Println("mem usage:", mem.TotalAlloc/1024) +} func Test_GroroutineMemUsage(t *testing.T) { for i := 0; i < n; i++ { @@ -36,18 +33,4 @@ func Test_GroroutineMemUsage(t *testing.T) { mem := runtime.MemStats{} runtime.ReadMemStats(&mem) fmt.Println("mem usage:", mem.TotalAlloc/1024) -} - -//func BenchmarkGrpool(b *testing.B) { -// b.N = n -// for i := 0; i < b.N; i++ { -// grpool.Add(increment) -// } -//} - -//func BenchmarkGoroutine(b *testing.B) { -// b.N = n -// for i := 0; i < b.N; i++ { -// go increment() -// } -//} \ No newline at end of file +} \ No newline at end of file diff --git a/g/os/grpool/grpool_testb1_test.go b/g/os/grpool/grpool_testb1_test.go new file mode 100644 index 000000000..16826af47 --- /dev/null +++ b/g/os/grpool/grpool_testb1_test.go @@ -0,0 +1,30 @@ +// Copyright 2017 gf Author(https://gitee.com/johng/gf). All Rights Reserved. +// +// This Source Code Form is subject to the terms of the MIT License. +// If a copy of the MIT was not distributed with this file, +// You can obtain one at https://gitee.com/johng/gf. + +// go test *.go -bench=".*" + +package grpool_test + +import ( + "testing" + "gitee.com/johng/gf/g/os/grpool" +) + +func increment1() { + for i := 0; i < 1000000; i++ {} +} + +func BenchmarkGrpool_1(b *testing.B) { + for i := 0; i < b.N; i++ { + grpool.Add(increment1) + } +} + +func BenchmarkGoroutine_1(b *testing.B) { + for i := 0; i < b.N; i++ { + go increment1() + } +} \ No newline at end of file diff --git a/g/os/grpool/grpool_testb2_test.go b/g/os/grpool/grpool_testb2_test.go new file mode 100644 index 000000000..0c47175bd --- /dev/null +++ b/g/os/grpool/grpool_testb2_test.go @@ -0,0 +1,34 @@ +// Copyright 2017 gf Author(https://gitee.com/johng/gf). All Rights Reserved. +// +// This Source Code Form is subject to the terms of the MIT License. +// If a copy of the MIT was not distributed with this file, +// You can obtain one at https://gitee.com/johng/gf. + +// go test *.go -bench=".*" -count=1 + +package grpool_test + +import ( + "testing" + "gitee.com/johng/gf/g/os/grpool" +) + +var n = 500000 + +func increment2() { + for i := 0; i < 1000000; i++ {} +} + +func BenchmarkGrpool2(b *testing.B) { + b.N = n + for i := 0; i < b.N; i++ { + grpool.Add(increment) + } +} + +func BenchmarkGoroutine2(b *testing.B) { + b.N = n + for i := 0; i < b.N; i++ { + go increment() + } +} \ No newline at end of file