-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
66 changed files
with
606 additions
and
167 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,165 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"github.com/google/uuid" | ||
"github.com/icinga/icingadb/internal/command" | ||
"github.com/icinga/icinga-go-library/com" | ||
"github.com/icinga/icingadb/pkg/contracts" | ||
"github.com/icinga/icingadb/pkg/icingadb" | ||
v1 "github.com/icinga/icingadb/pkg/icingadb/v1" | ||
"github.com/icinga/icinga-go-library/logging" | ||
"github.com/icinga/icinga-go-library/periodic" | ||
"github.com/icinga/icinga-go-library/types" | ||
"github.com/icinga/icinga-go-library/utils" | ||
"github.com/pkg/errors" | ||
"golang.org/x/sync/errgroup" | ||
"math/rand" | ||
"os" | ||
"strings" | ||
"time" | ||
"unsafe" | ||
) | ||
|
||
func main() { | ||
os.Exit(run()) | ||
} | ||
|
||
const count = 10000000 | ||
|
||
const schema = `DROP TABLE if EXISTS testing; | ||
CREATE TABLE testing ( | ||
id binary(20) NOT NULL, | ||
|
||
name varchar(255) NOT NULL, | ||
display_name varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL, | ||
|
||
PRIMARY KEY (id), | ||
|
||
index idx_display_name (display_name), | ||
index idx_name (NAME) | ||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin ROW_FORMAT=DYNAMIC;` | ||
|
||
type Testing struct { | ||
v1.EntityWithoutChecksum | ||
Name string `json:"name"` | ||
DisplayName string `json:"display_name"` | ||
} | ||
|
||
func OnSuccessIncrement[T any](counter *com.Counter) icingadb.OnSuccess[T] { | ||
return func(_ context.Context, rows []T) error { | ||
counter.Add(uint64(len(rows))) | ||
return nil | ||
} | ||
} | ||
|
||
func run() int { | ||
cmd := command.New() | ||
logs, err := logging.NewLogging( | ||
utils.AppName(), | ||
cmd.Config.Logging.Level, | ||
cmd.Config.Logging.Output, | ||
cmd.Config.Logging.Options, | ||
cmd.Config.Logging.Interval, | ||
) | ||
if err != nil { | ||
utils.Fatal(errors.Wrap(err, "can't configure logging")) | ||
} | ||
|
||
logger := logs.GetLogger() | ||
defer logger.Sync() | ||
|
||
db, err := cmd.Database(logs.GetChildLogger("database")) | ||
if err != nil { | ||
logger.Fatalf("%+v", errors.Wrap(err, "can't create database connection pool from config")) | ||
} | ||
defer db.Close() | ||
{ | ||
logger.Info("Connecting to database") | ||
err := db.Ping() | ||
if err != nil { | ||
logger.Fatalf("%+v", errors.Wrap(err, "can't connect to database")) | ||
} | ||
} | ||
|
||
ctx, cancelCtx := context.WithCancel(context.Background()) | ||
defer cancelCtx() | ||
|
||
logger.Info("Creating schema") | ||
|
||
for _, q := range strings.Split(schema, ";") { | ||
q = strings.TrimSpace(q) | ||
if q == "" { | ||
continue | ||
} | ||
logger.Debugf("Performing %q", q) | ||
if _, err := db.ExecContext(ctx, q); err != nil { | ||
logger.Fatal(err) | ||
} | ||
} | ||
|
||
logger.Info("Inserting entities") | ||
|
||
inserts := make(chan contracts.Entity) | ||
g, ctx := errgroup.WithContext(ctx) | ||
g.Go(func() error { | ||
defer close(inserts) | ||
for i := 0; i < count; i++ { | ||
_uuid := uuid.New() | ||
id := types.Binary(_uuid[:]) | ||
name := RandStringBytesMaskImprSrcUnsafe(255) | ||
inserts <- &Testing{ | ||
EntityWithoutChecksum: v1.EntityWithoutChecksum{IdMeta: v1.IdMeta{Id: id}}, | ||
Name: name, | ||
DisplayName: RandStringBytesMaskImprSrcUnsafe(255), | ||
} | ||
} | ||
|
||
return nil | ||
}) | ||
g.Go(func() error { | ||
var counter com.Counter | ||
|
||
periodic.Start(ctx, time.Second, func(tick periodic.Tick) { | ||
if count := counter.Reset(); count > 0 { | ||
logger.Debugf("Finished %d rows", count) | ||
} | ||
}, periodic.OnStop(func(tick periodic.Tick) { | ||
logger.Debugf("Finished %d rows in %s", counter.Total(), tick.Elapsed) | ||
})) | ||
|
||
return db.UpsertStreamed(ctx, inserts, OnSuccessIncrement[contracts.Entity](&counter)) | ||
}) | ||
if err := g.Wait(); err != nil { | ||
logger.Fatal(err) | ||
} | ||
|
||
return 0 | ||
} | ||
|
||
const letterBytes = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" | ||
const ( | ||
letterIdxBits = 6 // 6 bits to represent a letter index | ||
letterIdxMask = 1<<letterIdxBits - 1 // All 1-bits, as many as letterIdxBits | ||
letterIdxMax = 63 / letterIdxBits // # of letter indices fitting in 63 bits | ||
) | ||
|
||
var src = rand.NewSource(time.Now().UnixNano()) | ||
|
||
func RandStringBytesMaskImprSrcUnsafe(n int) string { | ||
b := make([]byte, n) | ||
// A src.Int63() generates 63 random bits, enough for letterIdxMax characters! | ||
for i, cache, remain := n-1, src.Int63(), letterIdxMax; i >= 0; { | ||
if remain == 0 { | ||
cache, remain = src.Int63(), letterIdxMax | ||
} | ||
if idx := int(cache & letterIdxMask); idx < len(letterBytes) { | ||
b[i] = letterBytes[idx] | ||
i-- | ||
} | ||
cache >>= letterIdxBits | ||
remain-- | ||
} | ||
|
||
return *(*string)(unsafe.Pointer(&b)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package main | ||
|
||
import ( | ||
"github.com/icinga/icinga-go-library/utils" | ||
"github.com/pkg/errors" | ||
"go.uber.org/zap" | ||
"go.uber.org/zap/zapcore" | ||
"sync" | ||
"time" | ||
) | ||
|
||
func main() { | ||
base, err := zap.NewDevelopment() | ||
if err != nil { | ||
utils.Fatal(err) | ||
} | ||
logger := base.Sugar() | ||
logger.Info("Starting Icinga DB Logging") | ||
logger.Fatalf("%+v", errors.New("panic right away")) | ||
wg := sync.WaitGroup{} | ||
wg.Add(1) | ||
go func() { | ||
defer wg.Done() | ||
<-time.After(time.Second*3) | ||
logger.Fatalf("%+v", errors.New("panic for testing")) | ||
}() | ||
wg.Wait() | ||
zapcore.WriteThenFatal | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.