| 1 | //go:build cgo |
| 2 | |
| 3 | package pmi |
| 4 | |
| 5 | import ( |
| 6 | "encoding/xml" |
| 7 | "os" |
| 8 | "path/filepath" |
| 9 | "testing" |
| 10 | |
| 11 | "github.com/netdata/netdata/go/plugins/plugin/ibm.d/framework" |
| 12 | pmiproto "github.com/netdata/netdata/go/plugins/plugin/ibm.d/protocols/websphere/pmi" |
| 13 | ) |
| 14 | |
| 15 | func TestAggregatorWithActivatedSamples(t *testing.T) { |
| 16 | sampleFiles := []string{ |
| 17 | "traditional-8.5.5.27-pmi-activated-port-10080.xml", |
| 18 | "traditional-9.0.5.x-pmi-activated-port-11080.xml", |
| 19 | } |
| 20 | |
| 21 | for _, rel := range sampleFiles { |
| 22 | path := filepath.Join("..", "..", "..", "samples.d", rel) |
| 23 | file, err := os.Open(path) |
| 24 | if err != nil { |
| 25 | t.Fatalf("open sample %s: %v", path, err) |
| 26 | } |
| 27 | |
| 28 | var snapshot pmiproto.Snapshot |
| 29 | if err := xml.NewDecoder(file).Decode(&snapshot); err != nil { |
| 30 | file.Close() |
| 31 | t.Fatalf("decode sample %s: %v", path, err) |
| 32 | } |
| 33 | file.Close() |
| 34 | |
| 35 | snapshot.Normalize() |
| 36 | |
| 37 | var urlStats int |
| 38 | var servletStats int |
| 39 | var totalStats int |
| 40 | var statsWithSubstats int |
| 41 | var statsWithoutMetrics int |
| 42 | |
| 43 | var walk func(stats []pmiproto.Stat) |
| 44 | walk = func(stats []pmiproto.Stat) { |
| 45 | for i := range stats { |
| 46 | st := &stats[i] |
| 47 | totalStats++ |
| 48 | if len(st.SubStats) > 0 { |
| 49 | statsWithSubstats++ |
| 50 | } |
| 51 | if len(st.CountStatistics) == 0 && len(st.TimeStatistics) == 0 && len(st.RangeStatistics) == 0 && len(st.BoundedRangeStatistics) == 0 { |
| 52 | statsWithoutMetrics++ |
| 53 | } |
| 54 | if st.Name == "URLs" { |
| 55 | urlStats += len(st.SubStats) |
| 56 | if len(st.SubStats) > 0 { |
| 57 | names := make([]string, 0, len(st.SubStats)) |
| 58 | for _, sub := range st.SubStats { |
| 59 | names = append(names, sub.Name) |
| 60 | } |
| 61 | t.Logf("URLs under %s => %v", st.Path, names) |
| 62 | } |
| 63 | } |
| 64 | if st.Name == "Servlets" { |
| 65 | servletStats += len(st.SubStats) |
| 66 | } |
| 67 | if len(st.SubStats) > 0 { |
| 68 | walk(st.SubStats) |
| 69 | } |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | for _, node := range snapshot.Nodes { |
| 74 | for _, server := range node.Servers { |
| 75 | walk(server.Stats) |
| 76 | } |
| 77 | } |
| 78 | walk(snapshot.Stats) |
| 79 | |
| 80 | t.Logf("sample %s stats totals: total=%d withSubstats=%d withoutMetrics=%d urlSubentries=%d servletChildren=%d", path, totalStats, statsWithSubstats, statsWithoutMetrics, urlStats, servletStats) |
| 81 | |
| 82 | agg := newAggregator(Config{}) |
| 83 | agg.processSnapshot(&snapshot, selectorBundle{}) |
| 84 | if len(agg.urls) == 0 { |
| 85 | t.Logf("no URLs captured; sample contains URLs: %d", urlStats) |
| 86 | } |
| 87 | for key, data := range agg.urls { |
| 88 | t.Logf("url key=%s requests=%d service=%d", key, data.requestCount, data.serviceTimeMs) |
| 89 | } |
| 90 | |
| 91 | state := framework.NewCollectorState() |
| 92 | agg.exportMetrics(state) |
| 93 | |
| 94 | metrics := state.GetMetrics() |
| 95 | if len(metrics) == 0 { |
| 96 | t.Fatalf("no metrics exported for sample %s", path) |
| 97 | } |
| 98 | if len(agg.jdbcPools) == 0 { |
| 99 | t.Fatalf("expected jdbc pools in sample %s", path) |
| 100 | } |
| 101 | if len(agg.webApps) == 0 { |
| 102 | t.Fatalf("expected web apps in sample %s", path) |
| 103 | } |
| 104 | if len(agg.sessions) == 0 { |
| 105 | t.Fatalf("expected sessions in sample %s", path) |
| 106 | } |
| 107 | if len(agg.jmsQueues) == 0 { |
| 108 | t.Fatalf("expected jms queues in sample %s", path) |
| 109 | } |
| 110 | |
| 111 | t.Logf("sample %s => metrics=%d threadPools=%d jdbcPools=%d jcaPools=%d jmsQueues=%d jmsTopics=%d webApps=%d sessions=%d dynamicCaches=%d urls=%d securityAuth=%d portletApps=%d portlets=%d", path, len(metrics), len(agg.threadPools), len(agg.jdbcPools), len(agg.jcaPools), len(agg.jmsQueues), len(agg.jmsTopics), len(agg.webApps), len(agg.sessions), len(agg.dynamicCaches), len(agg.urls), len(agg.securityAuth), len(agg.portletApps), len(agg.portlets)) |
| 112 | } |
| 113 | } |