Files
team/server/public/pluginapi/cluster/job_once_mem_test.go
+14 ba79c48dca [MM-53968] Includes mattermost-plugin-api into the mono repo (#24235)
Include https://github.com/mattermost/mattermost-plugin-api into the mono repo

Co-authored-by: Jesse Hallam <[email protected]>
Co-authored-by: Michael Kochell <[email protected]>
Co-authored-by: Alejandro García Montoro <[email protected]>
Co-authored-by: Ben Schumacher <[email protected]>
Co-authored-by: Alex Dovenmuehle <[email protected]>
Co-authored-by: Michael Kochell <[email protected]>
Co-authored-by: Christopher Poile <[email protected]>
Co-authored-by: İlker Göktuğ Öztürk <[email protected]>
Co-authored-by: Shota Gvinepadze <[email protected]>
Co-authored-by: Ali Farooq <[email protected]>
Co-authored-by: Maria A Nunez <[email protected]>
Co-authored-by: Daniel Espino García <[email protected]>
Co-authored-by: Christopher Speller <[email protected]>
Co-authored-by: Alex Dovenmuehle <[email protected]>
Co-authored-by: Szymon Gibała <[email protected]>
Co-authored-by: Lev <[email protected]>
Co-authored-by: Jason Frerich <[email protected]>
Co-authored-by: Agniva De Sarker <[email protected]>
Co-authored-by: Artur M. Wolff <[email protected]>
Co-authored-by: Madhav Hugar <[email protected]>
Co-authored-by: Joe <[email protected]>
Co-authored-by: Ibrahim Serdar Acikgoz <[email protected]>
Co-authored-by: José Peso <[email protected]>
2023-08-21 09:50:30 +02:00

84 lines
1.8 KiB
Go

// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
package cluster
import (
"fmt"
"runtime"
"sync/atomic"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/mattermost/mattermost/server/public/model"
)
func TestMemFootprint(t *testing.T) {
var memConsumed = func() uint64 {
runtime.GC()
var s runtime.MemStats
runtime.ReadMemStats(&s)
return s.Sys
}
t.Run("average k per jobOnce", func(t *testing.T) {
t.SkipNow()
makeKey := model.NewId
numJobs := 100000
jobs := make(map[string]*int32, numJobs)
for i := 0; i < numJobs; i++ {
jobs[makeKey()] = new(int32)
}
callback := func(key string, _ any) {
count, ok := jobs[key]
if ok {
atomic.AddInt32(count, 1)
}
}
mockPluginAPI := newMockPluginAPI(t)
s := GetJobOnceScheduler(mockPluginAPI)
err := s.SetCallback(callback)
require.NoError(t, err)
err = s.Start()
require.NoError(t, err)
getVal := func(key string) []byte {
data, _ := s.pluginAPI.KVGet(key)
return data
}
before := memConsumed()
for k := range jobs {
assert.Empty(t, getVal(oncePrefix+k))
_, err = s.ScheduleOnce(k, time.Now().Add(5*time.Minute), nil)
require.NoError(t, err)
assert.NotEmpty(t, getVal(oncePrefix+k))
}
time.Sleep(10 * time.Second)
// Everything scheduled now:
s.activeJobs.mu.RLock()
assert.Equal(t, numJobs, len(s.activeJobs.jobs))
s.activeJobs.mu.RUnlock()
list, err := s.ListScheduledJobs()
require.NoError(t, err)
assert.Equal(t, numJobs, len(list))
after := memConsumed()
fmt.Printf("\nthe %d jobs, scheduler, and goroutines require: %.2fmB memory, or %.3fkB each job\n",
numJobs,
float64(after-before)/(1024*1024),
(float64(after-before)/float64(numJobs))/1024)
})
}