improve upsert perfomance

This commit is contained in:
2024-08-13 23:39:44 +03:00
parent 30e5968e03
commit 25762cbae8
8 changed files with 127 additions and 83 deletions

View File

@ -2,9 +2,11 @@ package main
import (
"context"
"errors"
"log"
"os"
"os/signal"
"runtime"
"strconv"
"sync/atomic"
"time"
@ -38,18 +40,26 @@ func main() {
deviceCountStr := os.Getenv("DEVSIM_DEVICE_COUNT")
delayStr := os.Getenv("DEVSIM_REQUEST_DELAY")
deviceCount, err := strconv.Atoi(deviceCountStr)
if err != nil {
log.Fatalf("parsing device count: %v", err)
deviceCount := runtime.GOMAXPROCS(0)
if deviceCountStr != "" {
var err error
deviceCount, err = strconv.Atoi(deviceCountStr)
if err != nil {
log.Fatalf("parsing device count: %v", err)
}
}
if dstAddr == "" {
log.Fatal("no destination address provided")
}
delay, err := time.ParseDuration(delayStr)
if err != nil {
log.Fatalf("parsing delay duration: %v", err)
delay := time.Millisecond * 20
if delayStr != "" {
var err error
delay, err = time.ParseDuration(delayStr)
if err != nil {
log.Fatalf("parsing delay duration: %v", err)
}
}
log.Printf("running application with settings: destination=%s device_count=%d delay=%s", dstAddr, deviceCount, delay)
@ -113,6 +123,10 @@ func (h *deviceHandler) loop(ctx context.Context) {
err := h.client.Upsert(ctx, h.stats)
if err != nil {
if errors.Is(err, context.Canceled) {
return
}
log.Printf("%q: unable to upsert metrics: %v", h.stats.ID, err)
failedCount++
if failedCount > 10 {