Skip to content

Commit

Permalink
chore: more idiomatic unlock, concurrent test, benchmark, changelog e…
Browse files Browse the repository at this point in the history
…ntry
  • Loading branch information
vlastahajek committed Sep 17, 2021
1 parent 18cfd3d commit 2f7aba8
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 17 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## 2.5.1[in progress]
### Bug fixes
- [#276](https://github.com/influxdata/influxdb-client-go/pull/276) Synchronized logging methods of _log.Logger_

### Features
## 2.5.0 [2021-08-20]
### Features
- [#264](https://github.com/influxdata/influxdb-client-go/pull/264) Synced generated server API with the latest [oss.yml](https://github.com/influxdata/openapi/blob/master/contracts/oss.yml).
Expand Down
31 changes: 24 additions & 7 deletions client_e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,16 @@ package influxdb2_test
import (
"context"
"fmt"
"os"
"strconv"
"sync"
"testing"
"time"

influxdb2 "github.com/influxdata/influxdb-client-go/v2"
"github.com/influxdata/influxdb-client-go/v2/domain"
"github.com/influxdata/influxdb-client-go/v2/log"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"os"
"strconv"
"sync"
"testing"
"time"
)

var authToken string
Expand Down Expand Up @@ -272,7 +271,7 @@ func TestV2APIAgainstV1Server(t *testing.T) {
}

func TestHTTPService(t *testing.T) {
client := influxdb2.NewClient("http://localhost:8086", "my-token")
client := influxdb2.NewClient(serverURL, authToken)
apiClient := domain.NewClientWithResponses(client.HTTPService())
org, err := client.OrganizationsAPI().FindOrganizationByName(context.Background(), "my-org")
if err != nil {
Expand Down Expand Up @@ -311,3 +310,21 @@ from(bucket:"my-bucket") |> range(start: -1m) |> last()`
}
}
}

func TestLogsConcurrent(t *testing.T) {
var wg sync.WaitGroup
w := func(loc string, temp float32) {
client1 := influxdb2.NewClientWithOptions(serverURL, authToken, influxdb2.DefaultOptions().SetLogLevel(log.ErrorLevel))
for i := 0; i < 10000; i++ {
client1.WriteAPI("my-org", "my-bucket").WriteRecord(fmt.Sprintf("room,location=%s temp=%f", loc, temp))
}
client1.Close()
wg.Done()
}
for i := 0; i < 2; i++ {
wg.Add(1)
go w(fmt.Sprintf("T%d", i), 23.3+float32(i))
<-time.After(time.Nanosecond)
}
wg.Wait()
}
22 changes: 12 additions & 10 deletions log/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,73 +59,75 @@ type logger struct {

func (l *logger) SetLogLevel(logLevel uint) {
l.lock.Lock()
defer l.lock.Unlock()
l.logLevel = logLevel
l.lock.Unlock()
}

func (l *logger) LogLevel() uint {
l.lock.Lock()
defer l.lock.Unlock()
return l.logLevel
}

func (l *logger) SetPrefix(prefix string) {
l.lock.Lock()
defer l.lock.Unlock()
l.prefix = prefix
l.lock.Unlock()
}

func (l *logger) Debugf(format string, v ...interface{}) {
l.lock.Lock()
defer l.lock.Unlock()
if l.logLevel >= DebugLevel {
log.Print(l.prefix, " D! ", fmt.Sprintf(format, v...))
}
l.lock.Unlock()
}
func (l *logger) Debug(msg string) {
l.lock.Lock()
defer l.lock.Unlock()
if l.logLevel >= DebugLevel {
log.Print(l.prefix, " D! ", msg)
}
l.lock.Unlock()
}

func (l *logger) Infof(format string, v ...interface{}) {
l.lock.Lock()
defer l.lock.Unlock()
if l.logLevel >= InfoLevel {
log.Print(l.prefix, " I! ", fmt.Sprintf(format, v...))
}
l.lock.Unlock()
}
func (l *logger) Info(msg string) {
l.lock.Lock()
defer l.lock.Unlock()
if l.logLevel >= DebugLevel {
log.Print(l.prefix, " I! ", msg)
}
l.lock.Unlock()
}

func (l *logger) Warnf(format string, v ...interface{}) {
l.lock.Lock()
defer l.lock.Unlock()
if l.logLevel >= WarningLevel {
log.Print(l.prefix, " W! ", fmt.Sprintf(format, v...))
}
l.lock.Unlock()
}
func (l *logger) Warn(msg string) {
l.lock.Lock()
defer l.lock.Unlock()
if l.logLevel >= WarningLevel {
log.Print(l.prefix, " W! ", msg)
}
l.lock.Unlock()
}

func (l *logger) Errorf(format string, v ...interface{}) {
l.lock.Lock()
defer l.lock.Unlock()
log.Print(l.prefix, " E! ", fmt.Sprintf(format, v...))
l.lock.Unlock()
}

func (l *logger) Error(msg string) {
l.lock.Lock()
defer l.lock.Unlock()
log.Print(l.prefix, " [E]! ", msg)
l.lock.Unlock()
}
18 changes: 18 additions & 0 deletions log/logger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"strings"
"testing"

logi "github.com/influxdata/influxdb-client-go/v2/internal/log"
dlog "github.com/influxdata/influxdb-client-go/v2/log"
"github.com/stretchr/testify/assert"
)
Expand Down Expand Up @@ -134,3 +135,20 @@ func (l *testLogger) Errorf(format string, v ...interface{}) {
func (l *testLogger) Error(msg string) {
log.Print("testlogger", " [E]! ", msg)
}

type nullWriter struct{}

func (m *nullWriter) Write(p []byte) (n int, err error) {
return len(p), nil
}

func BenchmarkLogger(b *testing.B) {
// run the Fib function b.N times
log.SetOutput(&nullWriter{})
dlog.Log.SetLogLevel(dlog.DebugLevel)
for n := 0; n < b.N; n++ {
for i := 0; i < 5; i++ {
go logi.Debug("Log nothing")
}
}
}

0 comments on commit 2f7aba8

Please sign in to comment.