Skip to content
This repository has been archived by the owner on Mar 9, 2022. It is now read-only.

Commit

Permalink
influx: Faster time conversions
Browse files Browse the repository at this point in the history
SafeCalcTime now returns as int64 instead of a time.Time. The caller
needs an int64 anyway so creating the intermediate time.Time just
slows things down.
  • Loading branch information
mjs committed May 22, 2018
1 parent d7a711e commit 5c2062f
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 14 deletions.
8 changes: 4 additions & 4 deletions influx/timestamps.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,15 +126,15 @@ func ExtractNanos(line []byte) (int64, int) {

// SafeCalcTime safely calculates the time given. Will return error if
// the time is outside the supported range.
func SafeCalcTime(timestamp int64, precision string) (time.Time, error) {
func SafeCalcTime(timestamp int64, precision string) (int64, error) {
mult := getPrecisionMultiplier(precision)
if t, ok := safeSignedMult(timestamp, mult); ok {
if t < minNanoTime || t > maxNanoTime {
return time.Time{}, ErrTimeOutOfRange
return 0, ErrTimeOutOfRange
}
return time.Unix(0, t).UTC(), nil
return t, nil
}
return time.Time{}, ErrTimeOutOfRange
return 0, ErrTimeOutOfRange
}

// getPrecisionMultiplier will return a multiplier for the precision specified.
Expand Down
18 changes: 9 additions & 9 deletions influx/timestamps_small_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,56 +103,56 @@ func TestSafeCalcTime(t *testing.T) {
name string
ts int64
precision string
exp time.Time
exp int64
}{
{
name: "nanosecond by default",
ts: 946730096789012345,
precision: "",
exp: time.Unix(0, 946730096789012345),
exp: 946730096789012345,
},
{
name: "nanosecond",
ts: 946730096789012345,
precision: "n",
exp: time.Unix(0, 946730096789012345),
exp: 946730096789012345,
},
{
name: "microsecond",
ts: 946730096789012,
precision: "u",
exp: time.Unix(0, 946730096789012000),
exp: 946730096789012000,
},
{
name: "millisecond",
ts: 946730096789,
precision: "ms",
exp: time.Unix(0, 946730096789000000),
exp: 946730096789000000,
},
{
name: "second",
ts: 946730096,
precision: "s",
exp: time.Unix(0, 946730096000000000),
exp: 946730096000000000,
},
{
name: "minute",
ts: 15778834,
precision: "m",
exp: time.Unix(0, 946730040000000000),
exp: 946730040000000000,
},
{
name: "hour",
ts: 262980,
precision: "h",
exp: time.Unix(0, 946728000000000000),
exp: 946728000000000000,
},
}

for _, test := range tests {
ts, err := influx.SafeCalcTime(test.ts, test.precision)
assert.NoError(t, err, `%s: SafeCalcTime() failed. got %s`, test.name, err)
assert.True(t, ts.Equal(test.exp), "%s: expected %s, got %s", test.name, test.exp, ts)
assert.Equal(t, test.exp, ts, "%s: expected %s, got %s", test.name, test.exp, ts)
}
}

Expand Down
2 changes: 1 addition & 1 deletion listener/listener.go
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,7 @@ func applyTimestampPrecision(line []byte, precision string) []byte {

newLine := make([]byte, offset, offset+influx.MaxTsLen+1)
copy(newLine, line[:offset])
newLine = strconv.AppendInt(newLine, newTs.UnixNano(), 10)
newLine = strconv.AppendInt(newLine, newTs, 10)
return append(newLine, '\n')
}

Expand Down

0 comments on commit 5c2062f

Please sign in to comment.