Nav apraksta

profile_test.go 24KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018
  1. // Copyright 2014 Google Inc. All Rights Reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package profile
  15. import (
  16. "bytes"
  17. "fmt"
  18. "io/ioutil"
  19. "path/filepath"
  20. "reflect"
  21. "regexp"
  22. "strings"
  23. "sync"
  24. "testing"
  25. "github.com/google/pprof/internal/proftest"
  26. )
  27. func TestParse(t *testing.T) {
  28. const path = "testdata/"
  29. for _, source := range []string{
  30. "go.crc32.cpu",
  31. "go.godoc.thread",
  32. "gobench.cpu",
  33. "gobench.heap",
  34. "cppbench.cpu",
  35. "cppbench.heap",
  36. "cppbench.contention",
  37. "cppbench.growth",
  38. "cppbench.thread",
  39. "cppbench.thread.all",
  40. "cppbench.thread.none",
  41. "java.cpu",
  42. "java.heap",
  43. "java.contention",
  44. } {
  45. inbytes, err := ioutil.ReadFile(filepath.Join(path, source))
  46. if err != nil {
  47. t.Fatal(err)
  48. }
  49. p, err := Parse(bytes.NewBuffer(inbytes))
  50. if err != nil {
  51. t.Fatalf("%s: %s", source, err)
  52. }
  53. js := p.String()
  54. goldFilename := path + source + ".string"
  55. gold, err := ioutil.ReadFile(goldFilename)
  56. if err != nil {
  57. t.Fatalf("%s: %v", source, err)
  58. }
  59. if js != string(gold) {
  60. t.Errorf("diff %s %s", source, goldFilename)
  61. d, err := proftest.Diff(gold, []byte(js))
  62. if err != nil {
  63. t.Fatalf("%s: %v", source, err)
  64. }
  65. t.Error(source + "\n" + string(d) + "\n" + "new profile at:\n" + leaveTempfile([]byte(js)))
  66. }
  67. // Reencode and decode.
  68. bw := bytes.NewBuffer(nil)
  69. if err := p.Write(bw); err != nil {
  70. t.Fatalf("%s: %v", source, err)
  71. }
  72. if p, err = Parse(bw); err != nil {
  73. t.Fatalf("%s: %v", source, err)
  74. }
  75. js2 := p.String()
  76. if js2 != string(gold) {
  77. d, err := proftest.Diff(gold, []byte(js2))
  78. if err != nil {
  79. t.Fatalf("%s: %v", source, err)
  80. }
  81. t.Error(source + "\n" + string(d) + "\n" + "gold:\n" + goldFilename +
  82. "\nnew profile at:\n" + leaveTempfile([]byte(js)))
  83. }
  84. }
  85. }
  86. func TestParseError(t *testing.T) {
  87. testcases := []string{
  88. "",
  89. "garbage text",
  90. "\x1f\x8b", // truncated gzip header
  91. "\x1f\x8b\x08\x08\xbe\xe9\x20\x58\x00\x03\x65\x6d\x70\x74\x79\x00\x03\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00", // empty gzipped file
  92. }
  93. for i, input := range testcases {
  94. _, err := Parse(strings.NewReader(input))
  95. if err == nil {
  96. t.Errorf("got nil, want error for input #%d", i)
  97. }
  98. }
  99. }
  100. func TestCheckValid(t *testing.T) {
  101. const path = "testdata/java.cpu"
  102. inbytes, err := ioutil.ReadFile(path)
  103. if err != nil {
  104. t.Fatalf("failed to read profile file %q: %v", path, err)
  105. }
  106. p, err := Parse(bytes.NewBuffer(inbytes))
  107. if err != nil {
  108. t.Fatalf("failed to parse profile %q: %s", path, err)
  109. }
  110. for _, tc := range []struct {
  111. mutateFn func(*Profile)
  112. wantErr string
  113. }{
  114. {
  115. mutateFn: func(p *Profile) { p.SampleType = nil },
  116. wantErr: "missing sample type information",
  117. },
  118. {
  119. mutateFn: func(p *Profile) { p.Sample[0] = nil },
  120. wantErr: "profile has nil sample",
  121. },
  122. {
  123. mutateFn: func(p *Profile) { p.Sample[0].Value = append(p.Sample[0].Value, 0) },
  124. wantErr: "sample has 3 values vs. 2 types",
  125. },
  126. {
  127. mutateFn: func(p *Profile) { p.Sample[0].Location[0] = nil },
  128. wantErr: "sample has nil location",
  129. },
  130. {
  131. mutateFn: func(p *Profile) { p.Location[0] = nil },
  132. wantErr: "profile has nil location",
  133. },
  134. {
  135. mutateFn: func(p *Profile) { p.Mapping = append(p.Mapping, nil) },
  136. wantErr: "profile has nil mapping",
  137. },
  138. {
  139. mutateFn: func(p *Profile) { p.Function[0] = nil },
  140. wantErr: "profile has nil function",
  141. },
  142. } {
  143. t.Run(tc.wantErr, func(t *testing.T) {
  144. p := p.Copy()
  145. tc.mutateFn(p)
  146. if err := p.CheckValid(); err == nil {
  147. t.Errorf("CheckValid(): got no error, want error %q", tc.wantErr)
  148. } else if !strings.Contains(err.Error(), tc.wantErr) {
  149. t.Errorf("CheckValid(): got error %v, want error %q", err, tc.wantErr)
  150. }
  151. })
  152. }
  153. }
  154. // leaveTempfile leaves |b| in a temporary file on disk and returns the
  155. // temp filename. This is useful to recover a profile when the test
  156. // fails.
  157. func leaveTempfile(b []byte) string {
  158. f1, err := ioutil.TempFile("", "profile_test")
  159. if err != nil {
  160. panic(err)
  161. }
  162. if _, err := f1.Write(b); err != nil {
  163. panic(err)
  164. }
  165. return f1.Name()
  166. }
  167. const mainBinary = "/bin/main"
  168. var cpuM = []*Mapping{
  169. {
  170. ID: 1,
  171. Start: 0x10000,
  172. Limit: 0x40000,
  173. File: mainBinary,
  174. HasFunctions: true,
  175. HasFilenames: true,
  176. HasLineNumbers: true,
  177. HasInlineFrames: true,
  178. },
  179. {
  180. ID: 2,
  181. Start: 0x1000,
  182. Limit: 0x4000,
  183. File: "/lib/lib.so",
  184. HasFunctions: true,
  185. HasFilenames: true,
  186. HasLineNumbers: true,
  187. HasInlineFrames: true,
  188. },
  189. {
  190. ID: 3,
  191. Start: 0x4000,
  192. Limit: 0x5000,
  193. File: "/lib/lib2_c.so.6",
  194. HasFunctions: true,
  195. HasFilenames: true,
  196. HasLineNumbers: true,
  197. HasInlineFrames: true,
  198. },
  199. {
  200. ID: 4,
  201. Start: 0x5000,
  202. Limit: 0x9000,
  203. File: "/lib/lib.so_6 (deleted)",
  204. HasFunctions: true,
  205. HasFilenames: true,
  206. HasLineNumbers: true,
  207. HasInlineFrames: true,
  208. },
  209. }
  210. var cpuF = []*Function{
  211. {ID: 1, Name: "main", SystemName: "main", Filename: "main.c"},
  212. {ID: 2, Name: "foo", SystemName: "foo", Filename: "foo.c"},
  213. {ID: 3, Name: "foo_caller", SystemName: "foo_caller", Filename: "foo.c"},
  214. }
  215. var cpuL = []*Location{
  216. {
  217. ID: 1000,
  218. Mapping: cpuM[1],
  219. Address: 0x1000,
  220. Line: []Line{
  221. {Function: cpuF[0], Line: 1},
  222. },
  223. },
  224. {
  225. ID: 2000,
  226. Mapping: cpuM[0],
  227. Address: 0x2000,
  228. Line: []Line{
  229. {Function: cpuF[1], Line: 2},
  230. {Function: cpuF[2], Line: 1},
  231. },
  232. },
  233. {
  234. ID: 3000,
  235. Mapping: cpuM[0],
  236. Address: 0x3000,
  237. Line: []Line{
  238. {Function: cpuF[1], Line: 2},
  239. {Function: cpuF[2], Line: 1},
  240. },
  241. },
  242. {
  243. ID: 3001,
  244. Mapping: cpuM[0],
  245. Address: 0x3001,
  246. Line: []Line{
  247. {Function: cpuF[2], Line: 2},
  248. },
  249. },
  250. {
  251. ID: 3002,
  252. Mapping: cpuM[0],
  253. Address: 0x3002,
  254. Line: []Line{
  255. {Function: cpuF[2], Line: 3},
  256. },
  257. },
  258. }
  259. var testProfile1 = &Profile{
  260. PeriodType: &ValueType{Type: "cpu", Unit: "milliseconds"},
  261. Period: 1,
  262. DurationNanos: 10e9,
  263. SampleType: []*ValueType{
  264. {Type: "samples", Unit: "count"},
  265. {Type: "cpu", Unit: "milliseconds"},
  266. },
  267. Sample: []*Sample{
  268. {
  269. Location: []*Location{cpuL[0]},
  270. Value: []int64{1000, 1000},
  271. Label: map[string][]string{
  272. "key1": {"tag1"},
  273. "key2": {"tag1"},
  274. },
  275. },
  276. {
  277. Location: []*Location{cpuL[1], cpuL[0]},
  278. Value: []int64{100, 100},
  279. Label: map[string][]string{
  280. "key1": {"tag2"},
  281. "key3": {"tag2"},
  282. },
  283. },
  284. {
  285. Location: []*Location{cpuL[2], cpuL[0]},
  286. Value: []int64{10, 10},
  287. Label: map[string][]string{
  288. "key1": {"tag3"},
  289. "key2": {"tag2"},
  290. },
  291. },
  292. {
  293. Location: []*Location{cpuL[3], cpuL[0]},
  294. Value: []int64{10000, 10000},
  295. Label: map[string][]string{
  296. "key1": {"tag4"},
  297. "key2": {"tag1"},
  298. },
  299. },
  300. {
  301. Location: []*Location{cpuL[4], cpuL[0]},
  302. Value: []int64{1, 1},
  303. Label: map[string][]string{
  304. "key1": {"tag4"},
  305. "key2": {"tag1"},
  306. },
  307. },
  308. },
  309. Location: cpuL,
  310. Function: cpuF,
  311. Mapping: cpuM,
  312. }
  313. var testProfile2 = &Profile{
  314. PeriodType: &ValueType{Type: "cpu", Unit: "milliseconds"},
  315. Period: 1,
  316. DurationNanos: 10e9,
  317. SampleType: []*ValueType{
  318. {Type: "samples", Unit: "count"},
  319. {Type: "cpu", Unit: "milliseconds"},
  320. },
  321. Sample: []*Sample{
  322. {
  323. Location: []*Location{cpuL[0]},
  324. Value: []int64{70, 1000},
  325. Label: map[string][]string{
  326. "key1": {"tag1"},
  327. "key2": {"tag1"},
  328. },
  329. },
  330. {
  331. Location: []*Location{cpuL[1], cpuL[0]},
  332. Value: []int64{60, 100},
  333. Label: map[string][]string{
  334. "key1": {"tag2"},
  335. "key3": {"tag2"},
  336. },
  337. },
  338. {
  339. Location: []*Location{cpuL[2], cpuL[0]},
  340. Value: []int64{50, 10},
  341. Label: map[string][]string{
  342. "key1": {"tag3"},
  343. "key2": {"tag2"},
  344. },
  345. },
  346. {
  347. Location: []*Location{cpuL[3], cpuL[0]},
  348. Value: []int64{40, 10000},
  349. Label: map[string][]string{
  350. "key1": {"tag4"},
  351. "key2": {"tag1"},
  352. },
  353. },
  354. {
  355. Location: []*Location{cpuL[4], cpuL[0]},
  356. Value: []int64{1, 1},
  357. Label: map[string][]string{
  358. "key1": {"tag4"},
  359. "key2": {"tag1"},
  360. },
  361. },
  362. },
  363. Location: cpuL,
  364. Function: cpuF,
  365. Mapping: cpuM,
  366. }
  367. var testProfile3 = &Profile{
  368. PeriodType: &ValueType{Type: "cpu", Unit: "milliseconds"},
  369. Period: 1,
  370. DurationNanos: 10e9,
  371. SampleType: []*ValueType{
  372. {Type: "samples", Unit: "count"},
  373. },
  374. Sample: []*Sample{
  375. {
  376. Location: []*Location{cpuL[0]},
  377. Value: []int64{1000},
  378. Label: map[string][]string{
  379. "key1": {"tag1"},
  380. "key2": {"tag1"},
  381. },
  382. },
  383. },
  384. Location: cpuL,
  385. Function: cpuF,
  386. Mapping: cpuM,
  387. }
  388. var testProfile4 = &Profile{
  389. PeriodType: &ValueType{Type: "cpu", Unit: "milliseconds"},
  390. Period: 1,
  391. DurationNanos: 10e9,
  392. SampleType: []*ValueType{
  393. {Type: "samples", Unit: "count"},
  394. },
  395. Sample: []*Sample{
  396. {
  397. Location: []*Location{cpuL[0]},
  398. Value: []int64{1000},
  399. NumLabel: map[string][]int64{
  400. "key1": {10},
  401. "key2": {30},
  402. },
  403. NumUnit: map[string][]string{
  404. "key1": {"bytes"},
  405. "key2": {"bytes"},
  406. },
  407. },
  408. },
  409. Location: cpuL,
  410. Function: cpuF,
  411. Mapping: cpuM,
  412. }
  413. var testProfile5 = &Profile{
  414. PeriodType: &ValueType{Type: "cpu", Unit: "milliseconds"},
  415. Period: 1,
  416. DurationNanos: 10e9,
  417. SampleType: []*ValueType{
  418. {Type: "samples", Unit: "count"},
  419. },
  420. Sample: []*Sample{
  421. {
  422. Location: []*Location{cpuL[0]},
  423. Value: []int64{1000},
  424. NumLabel: map[string][]int64{
  425. "key1": {10},
  426. "key2": {30},
  427. },
  428. NumUnit: map[string][]string{
  429. "key1": {"bytes"},
  430. "key2": {"bytes"},
  431. },
  432. },
  433. {
  434. Location: []*Location{cpuL[0]},
  435. Value: []int64{1000},
  436. NumLabel: map[string][]int64{
  437. "key1": {10},
  438. "key2": {30},
  439. },
  440. NumUnit: map[string][]string{
  441. "key1": {"kilobytes"},
  442. "key2": {"kilobytes"},
  443. },
  444. },
  445. },
  446. Location: cpuL,
  447. Function: cpuF,
  448. Mapping: cpuM,
  449. }
  450. var aggTests = map[string]aggTest{
  451. "precise": {true, true, true, true, 5},
  452. "fileline": {false, true, true, true, 4},
  453. "inline_function": {false, true, false, true, 3},
  454. "function": {false, true, false, false, 2},
  455. }
  456. type aggTest struct {
  457. precise, function, fileline, inlineFrame bool
  458. rows int
  459. }
  460. const totalSamples = int64(11111)
  461. func TestAggregation(t *testing.T) {
  462. prof := testProfile1.Copy()
  463. for _, resolution := range []string{"precise", "fileline", "inline_function", "function"} {
  464. a := aggTests[resolution]
  465. if !a.precise {
  466. if err := prof.Aggregate(a.inlineFrame, a.function, a.fileline, a.fileline, false); err != nil {
  467. t.Error("aggregating to " + resolution + ":" + err.Error())
  468. }
  469. }
  470. if err := checkAggregation(prof, &a); err != nil {
  471. t.Error("failed aggregation to " + resolution + ": " + err.Error())
  472. }
  473. }
  474. }
  475. // checkAggregation verifies that the profile remained consistent
  476. // with its aggregation.
  477. func checkAggregation(prof *Profile, a *aggTest) error {
  478. // Check that the total number of samples for the rows was preserved.
  479. total := int64(0)
  480. samples := make(map[string]bool)
  481. for _, sample := range prof.Sample {
  482. tb := locationHash(sample)
  483. samples[tb] = true
  484. total += sample.Value[0]
  485. }
  486. if total != totalSamples {
  487. return fmt.Errorf("sample total %d, want %d", total, totalSamples)
  488. }
  489. // Check the number of unique sample locations
  490. if a.rows != len(samples) {
  491. return fmt.Errorf("number of samples %d, want %d", len(samples), a.rows)
  492. }
  493. // Check that all mappings have the right detail flags.
  494. for _, m := range prof.Mapping {
  495. if m.HasFunctions != a.function {
  496. return fmt.Errorf("unexpected mapping.HasFunctions %v, want %v", m.HasFunctions, a.function)
  497. }
  498. if m.HasFilenames != a.fileline {
  499. return fmt.Errorf("unexpected mapping.HasFilenames %v, want %v", m.HasFilenames, a.fileline)
  500. }
  501. if m.HasLineNumbers != a.fileline {
  502. return fmt.Errorf("unexpected mapping.HasLineNumbers %v, want %v", m.HasLineNumbers, a.fileline)
  503. }
  504. if m.HasInlineFrames != a.inlineFrame {
  505. return fmt.Errorf("unexpected mapping.HasInlineFrames %v, want %v", m.HasInlineFrames, a.inlineFrame)
  506. }
  507. }
  508. // Check that aggregation has removed finer resolution data.
  509. for _, l := range prof.Location {
  510. if !a.inlineFrame && len(l.Line) > 1 {
  511. return fmt.Errorf("found %d lines on location %d, want 1", len(l.Line), l.ID)
  512. }
  513. for _, ln := range l.Line {
  514. if !a.fileline && (ln.Function.Filename != "" || ln.Line != 0) {
  515. return fmt.Errorf("found line %s:%d on location %d, want :0",
  516. ln.Function.Filename, ln.Line, l.ID)
  517. }
  518. if !a.function && (ln.Function.Name != "") {
  519. return fmt.Errorf(`found file %s location %d, want ""`,
  520. ln.Function.Name, l.ID)
  521. }
  522. }
  523. }
  524. return nil
  525. }
  526. // Test merge leaves the main binary in place.
  527. func TestMergeMain(t *testing.T) {
  528. prof := testProfile1.Copy()
  529. p1, err := Merge([]*Profile{prof})
  530. if err != nil {
  531. t.Fatalf("merge error: %v", err)
  532. }
  533. if cpuM[0].File != p1.Mapping[0].File {
  534. t.Errorf("want Mapping[0]=%s got %s", cpuM[0].File, p1.Mapping[0].File)
  535. }
  536. }
  537. func TestMerge(t *testing.T) {
  538. // Aggregate a profile with itself and once again with a factor of
  539. // -2. Should end up with an empty profile (all samples for a
  540. // location should add up to 0).
  541. prof := testProfile1.Copy()
  542. p1, err := Merge([]*Profile{prof, prof})
  543. if err != nil {
  544. t.Errorf("merge error: %v", err)
  545. }
  546. prof.Scale(-2)
  547. prof, err = Merge([]*Profile{p1, prof})
  548. if err != nil {
  549. t.Errorf("merge error: %v", err)
  550. }
  551. // Use aggregation to merge locations at function granularity.
  552. if err := prof.Aggregate(false, true, false, false, false); err != nil {
  553. t.Errorf("aggregating after merge: %v", err)
  554. }
  555. samples := make(map[string]int64)
  556. for _, s := range prof.Sample {
  557. tb := locationHash(s)
  558. samples[tb] = samples[tb] + s.Value[0]
  559. }
  560. for s, v := range samples {
  561. if v != 0 {
  562. t.Errorf("nonzero value for sample %s: %d", s, v)
  563. }
  564. }
  565. }
  566. func TestMergeAll(t *testing.T) {
  567. // Aggregate 10 copies of the profile.
  568. profs := make([]*Profile, 10)
  569. for i := 0; i < 10; i++ {
  570. profs[i] = testProfile1.Copy()
  571. }
  572. prof, err := Merge(profs)
  573. if err != nil {
  574. t.Errorf("merge error: %v", err)
  575. }
  576. samples := make(map[string]int64)
  577. for _, s := range prof.Sample {
  578. tb := locationHash(s)
  579. samples[tb] = samples[tb] + s.Value[0]
  580. }
  581. for _, s := range testProfile1.Sample {
  582. tb := locationHash(s)
  583. if samples[tb] != s.Value[0]*10 {
  584. t.Errorf("merge got wrong value at %s : %d instead of %d", tb, samples[tb], s.Value[0]*10)
  585. }
  586. }
  587. }
  588. func TestNumLabelMerge(t *testing.T) {
  589. for _, tc := range []struct {
  590. name string
  591. profs []*Profile
  592. wantNumLabels []map[string][]int64
  593. wantNumUnits []map[string][]string
  594. }{
  595. {
  596. name: "different tag units not merged",
  597. profs: []*Profile{testProfile4.Copy(), testProfile5.Copy()},
  598. wantNumLabels: []map[string][]int64{
  599. {
  600. "key1": {10},
  601. "key2": {30},
  602. },
  603. {
  604. "key1": {10},
  605. "key2": {30},
  606. },
  607. },
  608. wantNumUnits: []map[string][]string{
  609. {
  610. "key1": {"bytes"},
  611. "key2": {"bytes"},
  612. },
  613. {
  614. "key1": {"kilobytes"},
  615. "key2": {"kilobytes"},
  616. },
  617. },
  618. },
  619. } {
  620. t.Run(tc.name, func(t *testing.T) {
  621. prof, err := Merge(tc.profs)
  622. if err != nil {
  623. t.Errorf("merge error: %v", err)
  624. }
  625. if want, got := len(tc.wantNumLabels), len(prof.Sample); want != got {
  626. t.Fatalf("got %d samples, want %d samples", got, want)
  627. }
  628. for i, wantLabels := range tc.wantNumLabels {
  629. numLabels := prof.Sample[i].NumLabel
  630. if !reflect.DeepEqual(wantLabels, numLabels) {
  631. t.Errorf("got numeric labels %v, want %v", numLabels, wantLabels)
  632. }
  633. wantUnits := tc.wantNumUnits[i]
  634. numUnits := prof.Sample[i].NumUnit
  635. if !reflect.DeepEqual(wantUnits, numUnits) {
  636. t.Errorf("got numeric labels %v, want %v", numUnits, wantUnits)
  637. }
  638. }
  639. })
  640. }
  641. }
  642. func TestNormalizeBySameProfile(t *testing.T) {
  643. pb := testProfile1.Copy()
  644. p := testProfile1.Copy()
  645. if err := p.Normalize(pb); err != nil {
  646. t.Fatal(err)
  647. }
  648. for i, s := range p.Sample {
  649. for j, v := range s.Value {
  650. expectedSampleValue := testProfile1.Sample[i].Value[j]
  651. if v != expectedSampleValue {
  652. t.Errorf("For sample %d, value %d want %d got %d", i, j, expectedSampleValue, v)
  653. }
  654. }
  655. }
  656. }
  657. func TestNormalizeByDifferentProfile(t *testing.T) {
  658. p := testProfile1.Copy()
  659. pb := testProfile2.Copy()
  660. if err := p.Normalize(pb); err != nil {
  661. t.Fatal(err)
  662. }
  663. expectedSampleValues := [][]int64{
  664. {19, 1000},
  665. {1, 100},
  666. {0, 10},
  667. {198, 10000},
  668. {0, 1},
  669. }
  670. for i, s := range p.Sample {
  671. for j, v := range s.Value {
  672. if v != expectedSampleValues[i][j] {
  673. t.Errorf("For sample %d, value %d want %d got %d", i, j, expectedSampleValues[i][j], v)
  674. }
  675. }
  676. }
  677. }
  678. func TestNormalizeByMultipleOfSameProfile(t *testing.T) {
  679. pb := testProfile1.Copy()
  680. for i, s := range pb.Sample {
  681. for j, v := range s.Value {
  682. pb.Sample[i].Value[j] = 10 * v
  683. }
  684. }
  685. p := testProfile1.Copy()
  686. err := p.Normalize(pb)
  687. if err != nil {
  688. t.Fatal(err)
  689. }
  690. for i, s := range p.Sample {
  691. for j, v := range s.Value {
  692. expectedSampleValue := 10 * testProfile1.Sample[i].Value[j]
  693. if v != expectedSampleValue {
  694. t.Errorf("For sample %d, value %d, want %d got %d", i, j, expectedSampleValue, v)
  695. }
  696. }
  697. }
  698. }
  699. func TestNormalizeIncompatibleProfiles(t *testing.T) {
  700. p := testProfile1.Copy()
  701. pb := testProfile3.Copy()
  702. if err := p.Normalize(pb); err == nil {
  703. t.Errorf("Expected an error")
  704. }
  705. }
  706. func TestFilter(t *testing.T) {
  707. // Perform several forms of filtering on the test profile.
  708. type filterTestcase struct {
  709. focus, ignore, hide, show *regexp.Regexp
  710. fm, im, hm, hnm bool
  711. }
  712. for tx, tc := range []filterTestcase{
  713. {
  714. fm: true, // nil focus matches every sample
  715. },
  716. {
  717. focus: regexp.MustCompile("notfound"),
  718. },
  719. {
  720. ignore: regexp.MustCompile("foo.c"),
  721. fm: true,
  722. im: true,
  723. },
  724. {
  725. hide: regexp.MustCompile("lib.so"),
  726. fm: true,
  727. hm: true,
  728. },
  729. {
  730. show: regexp.MustCompile("foo.c"),
  731. fm: true,
  732. hnm: true,
  733. },
  734. {
  735. show: regexp.MustCompile("notfound"),
  736. fm: true,
  737. },
  738. } {
  739. prof := *testProfile1.Copy()
  740. gf, gi, gh, gnh := prof.FilterSamplesByName(tc.focus, tc.ignore, tc.hide, tc.show)
  741. if gf != tc.fm {
  742. t.Errorf("Filter #%d, got fm=%v, want %v", tx, gf, tc.fm)
  743. }
  744. if gi != tc.im {
  745. t.Errorf("Filter #%d, got im=%v, want %v", tx, gi, tc.im)
  746. }
  747. if gh != tc.hm {
  748. t.Errorf("Filter #%d, got hm=%v, want %v", tx, gh, tc.hm)
  749. }
  750. if gnh != tc.hnm {
  751. t.Errorf("Filter #%d, got hnm=%v, want %v", tx, gnh, tc.hnm)
  752. }
  753. }
  754. }
  755. func TestTagFilter(t *testing.T) {
  756. // Perform several forms of tag filtering on the test profile.
  757. type filterTestcase struct {
  758. include, exclude *regexp.Regexp
  759. im, em bool
  760. count int
  761. }
  762. countTags := func(p *Profile) map[string]bool {
  763. tags := make(map[string]bool)
  764. for _, s := range p.Sample {
  765. for l := range s.Label {
  766. tags[l] = true
  767. }
  768. for l := range s.NumLabel {
  769. tags[l] = true
  770. }
  771. }
  772. return tags
  773. }
  774. for tx, tc := range []filterTestcase{
  775. {nil, nil, true, false, 3},
  776. {regexp.MustCompile("notfound"), nil, false, false, 0},
  777. {regexp.MustCompile("key1"), nil, true, false, 1},
  778. {nil, regexp.MustCompile("key[12]"), true, true, 1},
  779. } {
  780. prof := testProfile1.Copy()
  781. gim, gem := prof.FilterTagsByName(tc.include, tc.exclude)
  782. if gim != tc.im {
  783. t.Errorf("Filter #%d, got include match=%v, want %v", tx, gim, tc.im)
  784. }
  785. if gem != tc.em {
  786. t.Errorf("Filter #%d, got exclude match=%v, want %v", tx, gem, tc.em)
  787. }
  788. if tags := countTags(prof); len(tags) != tc.count {
  789. t.Errorf("Filter #%d, got %d tags[%v], want %d", tx, len(tags), tags, tc.count)
  790. }
  791. }
  792. }
  793. // locationHash constructs a string to use as a hashkey for a sample, based on its locations
  794. func locationHash(s *Sample) string {
  795. var tb string
  796. for _, l := range s.Location {
  797. for _, ln := range l.Line {
  798. tb = tb + fmt.Sprintf("%s:%d@%d ", ln.Function.Name, ln.Line, l.Address)
  799. }
  800. }
  801. return tb
  802. }
  803. func TestNumLabelUnits(t *testing.T) {
  804. var tagFilterTests = []struct {
  805. desc string
  806. tagVals []map[string][]int64
  807. tagUnits []map[string][]string
  808. wantUnits map[string]string
  809. wantIgnoredUnits map[string][]string
  810. }{
  811. {
  812. "One sample, multiple keys, different specified units",
  813. []map[string][]int64{{"key1": {131072}, "key2": {128}}},
  814. []map[string][]string{{"key1": {"bytes"}, "key2": {"kilobytes"}}},
  815. map[string]string{"key1": "bytes", "key2": "kilobytes"},
  816. map[string][]string{},
  817. },
  818. {
  819. "One sample, one key with one value, unit specified",
  820. []map[string][]int64{{"key1": {8}}},
  821. []map[string][]string{{"key1": {"bytes"}}},
  822. map[string]string{"key1": "bytes"},
  823. map[string][]string{},
  824. },
  825. {
  826. "One sample, one key with one value, empty unit specified",
  827. []map[string][]int64{{"key1": {8}}},
  828. []map[string][]string{{"key1": {""}}},
  829. map[string]string{"key1": "key1"},
  830. map[string][]string{},
  831. },
  832. {
  833. "Key bytes, unit not specified",
  834. []map[string][]int64{{"bytes": {8}}},
  835. []map[string][]string{nil},
  836. map[string]string{"bytes": "bytes"},
  837. map[string][]string{},
  838. },
  839. {
  840. "One sample, one key with one value, unit not specified",
  841. []map[string][]int64{{"kilobytes": {8}}},
  842. []map[string][]string{nil},
  843. map[string]string{"kilobytes": "kilobytes"},
  844. map[string][]string{},
  845. },
  846. {
  847. "Key request, unit not specified",
  848. []map[string][]int64{{"request": {8}}},
  849. []map[string][]string{nil},
  850. map[string]string{"request": "bytes"},
  851. map[string][]string{},
  852. },
  853. {
  854. "Key alignment, unit not specified",
  855. []map[string][]int64{{"alignment": {8}}},
  856. []map[string][]string{nil},
  857. map[string]string{"alignment": "bytes"},
  858. map[string][]string{},
  859. },
  860. {
  861. "One sample, one key with multiple values and two different units",
  862. []map[string][]int64{{"key1": {8, 8}}},
  863. []map[string][]string{{"key1": {"bytes", "kilobytes"}}},
  864. map[string]string{"key1": "bytes"},
  865. map[string][]string{"key1": {"kilobytes"}},
  866. },
  867. {
  868. "One sample, one key with multiple values and three different units",
  869. []map[string][]int64{{"key1": {8, 8}}},
  870. []map[string][]string{{"key1": {"bytes", "megabytes", "kilobytes"}}},
  871. map[string]string{"key1": "bytes"},
  872. map[string][]string{"key1": {"kilobytes", "megabytes"}},
  873. },
  874. {
  875. "Two samples, one key, different units specified",
  876. []map[string][]int64{{"key1": {8}}, {"key1": {8}}},
  877. []map[string][]string{{"key1": {"bytes"}}, {"key1": {"kilobytes"}}},
  878. map[string]string{"key1": "bytes"},
  879. map[string][]string{"key1": {"kilobytes"}},
  880. },
  881. {
  882. "Keys alignment, request, and bytes have units specified",
  883. []map[string][]int64{{
  884. "alignment": {8},
  885. "request": {8},
  886. "bytes": {8},
  887. }},
  888. []map[string][]string{{
  889. "alignment": {"seconds"},
  890. "request": {"minutes"},
  891. "bytes": {"hours"},
  892. }},
  893. map[string]string{
  894. "alignment": "seconds",
  895. "request": "minutes",
  896. "bytes": "hours",
  897. },
  898. map[string][]string{},
  899. },
  900. }
  901. for _, test := range tagFilterTests {
  902. p := &Profile{Sample: make([]*Sample, len(test.tagVals))}
  903. for i, numLabel := range test.tagVals {
  904. s := Sample{
  905. NumLabel: numLabel,
  906. NumUnit: test.tagUnits[i],
  907. }
  908. p.Sample[i] = &s
  909. }
  910. units, ignoredUnits := p.NumLabelUnits()
  911. if !reflect.DeepEqual(test.wantUnits, units) {
  912. t.Errorf("%s: got %v units, want %v", test.desc, units, test.wantUnits)
  913. }
  914. if !reflect.DeepEqual(test.wantIgnoredUnits, ignoredUnits) {
  915. t.Errorf("%s: got %v ignored units, want %v", test.desc, ignoredUnits, test.wantIgnoredUnits)
  916. }
  917. }
  918. }
  919. func TestSetMain(t *testing.T) {
  920. testProfile1.massageMappings()
  921. if testProfile1.Mapping[0].File != mainBinary {
  922. t.Errorf("got %s for main", testProfile1.Mapping[0].File)
  923. }
  924. }
  925. // parallel runs n copies of fn in parallel.
  926. func parallel(n int, fn func()) {
  927. var wg sync.WaitGroup
  928. wg.Add(n)
  929. for i := 0; i < n; i++ {
  930. go func() {
  931. fn()
  932. wg.Done()
  933. }()
  934. }
  935. wg.Wait()
  936. }
  937. func TestThreadSafety(t *testing.T) {
  938. src := testProfile1.Copy()
  939. parallel(4, func() { src.Copy() })
  940. parallel(4, func() {
  941. var b bytes.Buffer
  942. src.WriteUncompressed(&b)
  943. })
  944. parallel(4, func() {
  945. var b bytes.Buffer
  946. src.Write(&b)
  947. })
  948. }