github.com/go-spring/spring-base@v1.1.3/code/code_test.go (about) 1 /* 2 * Copyright 2012-2019 the original author or authors. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * https://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package code_test 18 19 import ( 20 "fmt" 21 "runtime/debug" 22 "testing" 23 "time" 24 25 "github.com/go-spring/spring-base/assert" 26 "github.com/go-spring/spring-base/code" 27 ) 28 29 func f1(t *testing.T) ([]string, time.Duration) { 30 ret, cost := f2(t) 31 assert.String(t, code.File()).HasSuffix("code/code_test.go") 32 assert.Equal(t, code.Line(), 32) 33 start := time.Now() 34 fileLine := code.FileLine() 35 cost += time.Since(start) 36 return append(ret, fileLine), cost 37 } 38 39 func f2(t *testing.T) ([]string, time.Duration) { 40 ret, cost := f3(t) 41 assert.String(t, code.File()).HasSuffix("code/code_test.go") 42 assert.Equal(t, code.Line(), 42) 43 start := time.Now() 44 fileLine := code.FileLine() 45 cost += time.Since(start) 46 return append(ret, fileLine), cost 47 } 48 49 func f3(t *testing.T) ([]string, time.Duration) { 50 ret, cost := f4(t) 51 assert.String(t, code.File()).HasSuffix("code/code_test.go") 52 assert.Equal(t, code.Line(), 52) 53 start := time.Now() 54 fileLine := code.FileLine() 55 cost += time.Since(start) 56 return append(ret, fileLine), cost 57 } 58 59 func f4(t *testing.T) ([]string, time.Duration) { 60 ret, cost := f5(t) 61 assert.String(t, code.File()).HasSuffix("code/code_test.go") 62 assert.Equal(t, code.Line(), 62) 63 start := time.Now() 64 fileLine := code.FileLine() 65 cost += time.Since(start) 66 return append(ret, fileLine), cost 67 } 68 69 func f5(t *testing.T) ([]string, time.Duration) { 70 ret, cost := f6(t) 71 assert.String(t, code.File()).HasSuffix("code/code_test.go") 72 assert.Equal(t, code.Line(), 72) 73 start := time.Now() 74 fileLine := code.FileLine() 75 cost += time.Since(start) 76 return append(ret, fileLine), cost 77 } 78 79 func f6(t *testing.T) ([]string, time.Duration) { 80 ret, cost := f7(t) 81 assert.String(t, code.File()).HasSuffix("code/code_test.go") 82 assert.Equal(t, code.Line(), 82) 83 start := time.Now() 84 fileLine := code.FileLine() 85 cost += time.Since(start) 86 return append(ret, fileLine), cost 87 } 88 89 func f7(t *testing.T) ([]string, time.Duration) { 90 assert.String(t, code.File()).HasSuffix("code/code_test.go") 91 assert.Equal(t, code.Line(), 91) 92 { 93 start := time.Now() 94 _ = debug.Stack() 95 fmt.Println("\t", "debug.Stack cost", time.Since(start)) 96 } 97 start := time.Now() 98 fileLine := code.FileLine() 99 cost := time.Since(start) 100 return []string{fileLine}, cost 101 } 102 103 func TestFileLine(t *testing.T) { 104 for i := 0; i < 5; i++ { 105 fmt.Printf("loop %d\n", i) 106 ret, cost := f1(t) 107 fmt.Println("\t", ret) 108 fmt.Println("\t", "all code.FileLine cost", cost) 109 } 110 //loop 0 111 // debug.Stack cost 37.794µs 112 // all code.FileLine cost 14.638µs 113 //loop 1 114 // debug.Stack cost 11.699µs 115 // all code.FileLine cost 6.398µs 116 //loop 2 117 // debug.Stack cost 20.62µs 118 // all code.FileLine cost 4.185µs 119 //loop 3 120 // debug.Stack cost 11.736µs 121 // all code.FileLine cost 4.274µs 122 //loop 4 123 // debug.Stack cost 19.821µs 124 // all code.FileLine cost 4.061µs 125 }