Add tracing support
Replaces existing logging support. Package tracelog provides adapter for old style logging. https://github.com/jackc/pgx/issues/1061
This commit is contained in:
-117
@@ -284,123 +284,6 @@ func BenchmarkPointerPointerWithPresentValues(b *testing.B) {
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkSelectWithoutLogging(b *testing.B) {
|
||||
conn := mustConnect(b, mustParseConfig(b, os.Getenv("PGX_TEST_DATABASE")))
|
||||
defer closeConn(b, conn)
|
||||
|
||||
benchmarkSelectWithLog(b, conn)
|
||||
}
|
||||
|
||||
type discardLogger struct{}
|
||||
|
||||
func (dl discardLogger) Log(ctx context.Context, level pgx.LogLevel, msg string, data map[string]any) {
|
||||
}
|
||||
|
||||
func BenchmarkSelectWithLoggingTraceDiscard(b *testing.B) {
|
||||
var logger discardLogger
|
||||
config := mustParseConfig(b, os.Getenv("PGX_TEST_DATABASE"))
|
||||
config.Logger = logger
|
||||
config.LogLevel = pgx.LogLevelTrace
|
||||
|
||||
conn := mustConnect(b, config)
|
||||
defer closeConn(b, conn)
|
||||
|
||||
benchmarkSelectWithLog(b, conn)
|
||||
}
|
||||
|
||||
func BenchmarkSelectWithLoggingDebugWithDiscard(b *testing.B) {
|
||||
var logger discardLogger
|
||||
config := mustParseConfig(b, os.Getenv("PGX_TEST_DATABASE"))
|
||||
config.Logger = logger
|
||||
config.LogLevel = pgx.LogLevelDebug
|
||||
|
||||
conn := mustConnect(b, config)
|
||||
defer closeConn(b, conn)
|
||||
|
||||
benchmarkSelectWithLog(b, conn)
|
||||
}
|
||||
|
||||
func BenchmarkSelectWithLoggingInfoWithDiscard(b *testing.B) {
|
||||
var logger discardLogger
|
||||
config := mustParseConfig(b, os.Getenv("PGX_TEST_DATABASE"))
|
||||
config.Logger = logger
|
||||
config.LogLevel = pgx.LogLevelInfo
|
||||
|
||||
conn := mustConnect(b, config)
|
||||
defer closeConn(b, conn)
|
||||
|
||||
benchmarkSelectWithLog(b, conn)
|
||||
}
|
||||
|
||||
func BenchmarkSelectWithLoggingErrorWithDiscard(b *testing.B) {
|
||||
var logger discardLogger
|
||||
config := mustParseConfig(b, os.Getenv("PGX_TEST_DATABASE"))
|
||||
config.Logger = logger
|
||||
config.LogLevel = pgx.LogLevelError
|
||||
|
||||
conn := mustConnect(b, config)
|
||||
defer closeConn(b, conn)
|
||||
|
||||
benchmarkSelectWithLog(b, conn)
|
||||
}
|
||||
|
||||
func benchmarkSelectWithLog(b *testing.B, conn *pgx.Conn) {
|
||||
_, err := conn.Prepare(context.Background(), "test", "select 1::int4, 'johnsmith', 'johnsmith@example.com', 'John Smith', 'male', '1970-01-01'::date, '2015-01-01 00:00:00'::timestamptz")
|
||||
if err != nil {
|
||||
b.Fatal(err)
|
||||
}
|
||||
|
||||
b.ResetTimer()
|
||||
for i := 0; i < b.N; i++ {
|
||||
var record struct {
|
||||
id int32
|
||||
userName string
|
||||
email string
|
||||
name string
|
||||
sex string
|
||||
birthDate time.Time
|
||||
lastLoginTime time.Time
|
||||
}
|
||||
|
||||
err = conn.QueryRow(context.Background(), "test").Scan(
|
||||
&record.id,
|
||||
&record.userName,
|
||||
&record.email,
|
||||
&record.name,
|
||||
&record.sex,
|
||||
&record.birthDate,
|
||||
&record.lastLoginTime,
|
||||
)
|
||||
if err != nil {
|
||||
b.Fatal(err)
|
||||
}
|
||||
|
||||
// These checks both ensure that the correct data was returned
|
||||
// and provide a benchmark of accessing the returned values.
|
||||
if record.id != 1 {
|
||||
b.Fatalf("bad value for id: %v", record.id)
|
||||
}
|
||||
if record.userName != "johnsmith" {
|
||||
b.Fatalf("bad value for userName: %v", record.userName)
|
||||
}
|
||||
if record.email != "johnsmith@example.com" {
|
||||
b.Fatalf("bad value for email: %v", record.email)
|
||||
}
|
||||
if record.name != "John Smith" {
|
||||
b.Fatalf("bad value for name: %v", record.name)
|
||||
}
|
||||
if record.sex != "male" {
|
||||
b.Fatalf("bad value for sex: %v", record.sex)
|
||||
}
|
||||
if record.birthDate != time.Date(1970, 1, 1, 0, 0, 0, 0, time.UTC) {
|
||||
b.Fatalf("bad value for birthDate: %v", record.birthDate)
|
||||
}
|
||||
if record.lastLoginTime != time.Date(2015, 1, 1, 0, 0, 0, 0, time.Local) {
|
||||
b.Fatalf("bad value for lastLoginTime: %v", record.lastLoginTime)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const benchmarkWriteTableCreateSQL = `drop table if exists t;
|
||||
|
||||
create table t(
|
||||
|
||||
Reference in New Issue
Block a user