Skip to content

Commit

Permalink
Merge pull request #31 from FishGoddess/develop
Browse files Browse the repository at this point in the history
v1.8.0
  • Loading branch information
FishGoddess authored Aug 7, 2024
2 parents 187f6c5 + 2ed3cc8 commit 703a851
Show file tree
Hide file tree
Showing 56 changed files with 203 additions and 74 deletions.
16 changes: 0 additions & 16 deletions .gitattributes

This file was deleted.

4 changes: 2 additions & 2 deletions FUTURE.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

### v1.8.x

* [ ] 融合 Go 官方设计的正式版本
* [x] 提高单元测试覆盖率到 80%
* [x] 增加快速时钟,可以非常快速地查询时间
* [ ] 提高单元测试覆盖率到 90%

### v1.5.x
Expand All @@ -21,7 +22,6 @@
* [x] 增加一个高可读性且高性能的 handler 实现
* [x] TapeHandler 转义处理
* [x] 提高单元测试覆盖率到 70%
* [ ] 进一步提高单元测试覆盖率到 80%

### v1.2.x

Expand Down
7 changes: 7 additions & 0 deletions HISTORY.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
## ✒ 历史版本的特性介绍 (Features in old versions)

### v1.8.0

> 此版本发布于 2024-08-07
* 提高单元测试覆盖率到 80%
* 增加快速时钟,可以非常快速地查询时间

### v1.5.10

> 此版本发布于 2024-01-19
Expand Down
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@
same "printed page" as the copyright notice for easier
identification within third-party archives.

Copyright [2020] [FishGoddess]
Copyright [2024] [FishGoddess]

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
.PHONY: test bench benchfile fmt
.PHONY: test bench fmt

all: test bench

Expand Down
2 changes: 1 addition & 1 deletion _examples/basic.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2023 FishGoddess. All Rights Reserved.
// Copyright 2024 FishGoddess. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down
2 changes: 1 addition & 1 deletion _examples/config.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2023 FishGoddess. All Rights Reserved.
// Copyright 2024 FishGoddess. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down
2 changes: 1 addition & 1 deletion _examples/context.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2023 FishGoddess. All Rights Reserved.
// Copyright 2024 FishGoddess. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down
2 changes: 1 addition & 1 deletion _examples/default.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2023 FishGoddess. All Rights Reserved.
// Copyright 2024 FishGoddess. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down
2 changes: 1 addition & 1 deletion _examples/file.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2023 FishGoddess. All Rights Reserved.
// Copyright 2024 FishGoddess. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down
2 changes: 1 addition & 1 deletion _examples/handler.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2023 FishGoddess. All Rights Reserved.
// Copyright 2024 FishGoddess. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down
2 changes: 1 addition & 1 deletion _examples/logger.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2023 FishGoddess. All Rights Reserved.
// Copyright 2024 FishGoddess. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down
2 changes: 1 addition & 1 deletion _examples/option.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2023 FishGoddess. All Rights Reserved.
// Copyright 2024 FishGoddess. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down
2 changes: 1 addition & 1 deletion _examples/writer.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2023 FishGoddess. All Rights Reserved.
// Copyright 2024 FishGoddess. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down
4 changes: 2 additions & 2 deletions _icons/coverage.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file removed _icons/logit.png
Binary file not shown.
2 changes: 1 addition & 1 deletion config.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2023 FishGoddess. All Rights Reserved.
// Copyright 2024 FishGoddess. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down
2 changes: 1 addition & 1 deletion config_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2023 FishGoddess. All Rights Reserved.
// Copyright 2024 FishGoddess. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down
2 changes: 1 addition & 1 deletion context.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2023 FishGoddess. All Rights Reserved.
// Copyright 2024 FishGoddess. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down
2 changes: 1 addition & 1 deletion context_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2023 FishGoddess. All Rights Reserved.
// Copyright 2024 FishGoddess. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down
2 changes: 1 addition & 1 deletion default.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2023 FishGoddess. All Rights Reserved.
// Copyright 2024 FishGoddess. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down
2 changes: 1 addition & 1 deletion default_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2023 FishGoddess. All Rights Reserved.
// Copyright 2024 FishGoddess. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down
2 changes: 1 addition & 1 deletion defaults/defaults.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2023 FishGoddess. All Rights Reserved.
// Copyright 2024 FishGoddess. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down
2 changes: 1 addition & 1 deletion doc.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2023 FishGoddess. All Rights Reserved.
// Copyright 2024 FishGoddess. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down
2 changes: 1 addition & 1 deletion extension/config/config.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2023 FishGoddess. All Rights Reserved.
// Copyright 2024 FishGoddess. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down
2 changes: 1 addition & 1 deletion extension/config/config_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2023 FishGoddess. All Rights Reserved.
// Copyright 2024 FishGoddess. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down
2 changes: 1 addition & 1 deletion extension/config/parse.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2023 FishGoddess. All Rights Reserved.
// Copyright 2024 FishGoddess. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down
4 changes: 3 additions & 1 deletion extension/config/parse_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2023 FishGoddess. All Rights Reserved.
// Copyright 2024 FishGoddess. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -80,7 +80,9 @@ func TestParseTimeDuration(t *testing.T) {
{name: "12s", s: "12s", want: 12 * time.Second, wantErr: false},
{name: "3m", s: "3m", want: 3 * time.Minute, wantErr: false},
{name: "24h", s: "24h", want: 24 * time.Hour, wantErr: false},
{name: "24h50m12s", s: "24h50m12s", want: 24*time.Hour + 50*time.Minute + 12*time.Second, wantErr: false},
{name: "7d", s: "7d", want: 7 * 24 * time.Hour, wantErr: false},
{name: "90D", s: "90D", want: 90 * 24 * time.Hour, wantErr: false},
{name: "''", s: "", want: 0, wantErr: true},
{name: "14", s: "14", want: 0, wantErr: true},
}
Expand Down
77 changes: 77 additions & 0 deletions extension/fastclock/fast_clock.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
// Copyright 2024 FishGoddess. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package fastclock

import (
"sync"
"sync/atomic"
"time"
)

// fastClock is a clock for getting current time faster.
// It caches current time in nanos and updates it in fixed duration, so it's not a precise way to get current time.
// In fact, we don't recommend you to use it unless you do need a fast way to get current time even the time is "incorrect".
// According to our benchmarks, it does run faster than time.Now:
//
// In my linux server with 2 cores:
// BenchmarkTimeNow-2 19150246 62.26 ns/op 0 B/op 0 allocs/op
// BenchmarkFastClockNow-2 357209233 3.46 ns/op 0 B/op 0 allocs/op
//
// However, the performance of time.Now is faster enough for 99.9% situations, so we hope you never use it :)
type fastClock struct {
nowNanos int64
}

func newClock() *fastClock {
clock := &fastClock{
nowNanos: time.Now().UnixNano(),
}

go clock.start()
return clock
}

func (fc *fastClock) start() {
const duration = 100 * time.Millisecond

for {
for i := 0; i < 9; i++ {
time.Sleep(duration)
atomic.AddInt64(&fc.nowNanos, int64(duration))
}

time.Sleep(duration)
atomic.StoreInt64(&fc.nowNanos, time.Now().UnixNano())
}
}

func (fc *fastClock) now() time.Time {
nanos := atomic.LoadInt64(&fc.nowNanos)
return time.Unix(0, nanos)
}

var (
clock *fastClock
clockOnce sync.Once
)

// Now returns the current time from fast clock.
func Now() time.Time {
clockOnce.Do(func() {
clock = newClock()
})

return clock.now()
}
59 changes: 59 additions & 0 deletions extension/fastclock/fast_clock_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
// Copyright 2024 FishGoddess. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package fastclock

import (
"math"
"math/rand"
"testing"
"time"
)

// go test -v -run=^$ -bench=^BenchmarkTimeNow$ -benchtime=1s
func BenchmarkTimeNow(b *testing.B) {
b.ReportAllocs()
b.ResetTimer()

for i := 0; i < b.N; i++ {
time.Now()
}
}

// go test -v -run=^$ -bench=^BenchmarkFastClockNow$ -benchtime=1s
func BenchmarkFastClockNow(b *testing.B) {
b.ReportAllocs()
b.ResetTimer()

for i := 0; i < b.N; i++ {
Now()
}
}

// go test -v -cover -count=1 -test.cpu=1 -run=^TestNow$
func TestNow(t *testing.T) {
duration := 100 * time.Millisecond

for i := 0; i < 100; i++ {
got := Now()
gap := time.Since(got)
t.Logf("got: %v, gap: %v", got, gap)

if math.Abs(float64(gap.Nanoseconds())) > float64(duration)*1.1 {
t.Errorf("now %v is wrong", got)
}

time.Sleep(time.Duration(rand.Int63n(int64(duration))))
}
}
2 changes: 1 addition & 1 deletion handler/buffer.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2023 FishGoddess. All Rights Reserved.
// Copyright 2024 FishGoddess. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down
4 changes: 2 additions & 2 deletions handler/buffer_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2023 FishGoddess. All Rights Reserved.
// Copyright 2024 FishGoddess. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand All @@ -20,7 +20,7 @@ import (
"github.com/FishGoddess/logit/defaults"
)

// go test -v -cover -count=1 -test.cpu=1 -run=^TestNewBuffer$
// go test -v -cover -count=1 -test.cpu=1 -run=^TestBufferPool$
func TestBufferPool(t *testing.T) {
for i := 0; i < 100; i++ {
bs := make([]byte, 0, 2*defaults.MaxBufferSize)
Expand Down
2 changes: 1 addition & 1 deletion handler/escape.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2023 FishGoddess. All Rights Reserved.
// Copyright 2024 FishGoddess. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down
Loading

0 comments on commit 703a851

Please sign in to comment.