Skip to content

Commit d1e46df

Browse files
committed
Fix calls to NanoTimer to call twice, due to defer.
1 parent 0597998 commit d1e46df

File tree

3 files changed

+34
-3
lines changed

3 files changed

+34
-3
lines changed

blockfile/blockfile.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ func (b *BlockFile) readPacket(pos int64, ci *gopacket.CaptureInfo) ([]byte, err
8585
// 28 bytes actually isn't the entire packet header, but it's all the fields
8686
// that we care about.
8787
packetsRead.Increment()
88-
defer packetReadNanos.NanoTimer()
88+
defer packetReadNanos.NanoTimer()()
8989
var dataBuf [28]byte
9090
_, err := b.f.ReadAt(dataBuf[:], pos)
9191
if err != nil {
@@ -130,7 +130,7 @@ type allPacketsIter struct {
130130
}
131131

132132
func (a *allPacketsIter) Next() bool {
133-
defer packetScanNanos.NanoTimer()
133+
defer packetScanNanos.NanoTimer()()
134134
if a.err != nil || a.done {
135135
return false
136136
}

indexfile/indexfile.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ func (i *IndexFile) positions(ctx context.Context, from, to []byte) (out base.Po
139139
indexCurrentReads.IncrementBy(-1)
140140
indexReads.Increment()
141141
}()
142-
defer indexReadNanos.NanoTimer()
142+
defer indexReadNanos.NanoTimer()()
143143
iter := i.ss.Find(from, nil)
144144
keyLen := len(from)
145145
last := make([]byte, keyLen)

stats/stats_test.go

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// Copyright 2014 Google Inc. All rights reserved.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package stats
16+
17+
import (
18+
"testing"
19+
"time"
20+
)
21+
22+
func TestNanoTimer(t *testing.T) {
23+
s := &Stat{}
24+
func() {
25+
defer s.NanoTimer()()
26+
time.Sleep(time.Millisecond)
27+
}()
28+
if got := s.int64; got < 1000000 {
29+
t.Error("invalid nano time:", got)
30+
}
31+
}

0 commit comments

Comments
 (0)