仰望星辰工作室

miaopanel

miaopanel/ internal/dbmanager/tuning_test.go 2.3 KB · 83 行 原始文件
1package dbmanager
2
3import (
4 "strings"
5 "testing"
6)
7
8func TestParseTSV(t *testing.T) {
9 got := parseTSV("Uptime\t120\nQuestions\t240\nQcache_hits\t12\n")
10 if got["Uptime"] != "120" || got["Questions"] != "240" || got["Qcache_hits"] != "12" {
11 t.Fatalf("unexpected map: %v", got)
12 }
13}
14
15func TestEstimateMaxMemory(t *testing.T) {
16 p := Performance{
17 InnoDBPoolMB: 64, KeyBufferMB: 32, QueryCacheMB: 32, TmpTableMB: 32,
18 InnoDBLogBufferMB: 16, MaxConnections: 100,
19 SortBufferKB: 256, ReadBufferKB: 256, ReadRndBufferKB: 256,
20 JoinBufferKB: 512, ThreadStackKB: 256, BinlogCacheKB: 64,
21 }
22 est, detail := estimateMaxMemory(p)
23 // 176 MB fixed + 100 * 1600KB (156.25MB) = 332.25 MB, the same estimate the
24 // reference panel shows for its 1-2GB profile.
25 if est < 331 || est > 334 {
26 t.Fatalf("estimate = %.2f, want ~332.25\n%s", est, detail)
27 }
28}
29
30func TestRenderMysqldQueryCache(t *testing.T) {
31 p := Performance{
32 QueryCacheMB: 32, TableOpenCache: 128, ThreadCacheSize: 64,
33 KeyBufferMB: 32, TmpTableMB: 32, InnoDBPoolMB: 64,
34 InnoDBLogBufferMB: 16, MaxConnections: 100, SortBufferKB: 256,
35 ReadBufferKB: 256, ReadRndBufferKB: 256, JoinBufferKB: 512,
36 ThreadStackKB: 256, BinlogCacheKB: 64,
37 }
38 with := renderMysqld(p, true)
39 if !strings.Contains(with, "query_cache_size = 32M") {
40 t.Fatalf("query cache missing:\n%s", with)
41 }
42 without := renderMysqld(p, false)
43 if strings.Contains(without, "query_cache") {
44 t.Fatalf("query cache must be omitted on MySQL 8:\n%s", without)
45 }
46}
47
48func TestUpsertMysqldPreservesOtherSections(t *testing.T) {
49 raw := `[client]
50port = 3306
51
52[mysqld]
53port = 3306
54max_connections = 50
55# keep this comment
56
57[mysqldump]
58quick
59`
60 body := "\n[mysqld]\nmax_connections = 200\nsort_buffer_size = 512K\n"
61 out := upsertMysqld(raw, body)
62 for _, want := range []string{
63 "[client]\nport = 3306",
64 "max_connections = 200",
65 "sort_buffer_size = 512K",
66 "[mysqldump]\nquick",
67 "# keep this comment",
68 } {
69 if !strings.Contains(out, want) {
70 t.Errorf("missing %q\n%s", want, out)
71 }
72 }
73 if strings.Contains(out, "max_connections = 50") {
74 t.Errorf("old value not replaced:\n%s", out)
75 }
76}
77
78func TestUpsertMysqldAppendsSection(t *testing.T) {
79 out := upsertMysqld("[client]\nport=3306\n", "\n[mysqld]\nmax_connections = 100\n")
80 if !strings.Contains(out, "[client]") || !strings.Contains(out, "[mysqld]") {
81 t.Fatalf("section not appended:\n%s", out)
82 }
83}