Nenhuma descrição

profile_test.go 23KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961
  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. // leaveTempfile leaves |b| in a temporary file on disk and returns the
  101. // temp filename. This is useful to recover a profile when the test
  102. // fails.
  103. func leaveTempfile(b []byte) string {
  104. f1, err := ioutil.TempFile("", "profile_test")
  105. if err != nil {
  106. panic(err)
  107. }
  108. if _, err := f1.Write(b); err != nil {
  109. panic(err)
  110. }
  111. return f1.Name()
  112. }
  113. const mainBinary = "/bin/main"
  114. var cpuM = []*Mapping{
  115. {
  116. ID: 1,
  117. Start: 0x10000,
  118. Limit: 0x40000,
  119. File: mainBinary,
  120. HasFunctions: true,
  121. HasFilenames: true,
  122. HasLineNumbers: true,
  123. HasInlineFrames: true,
  124. },
  125. {
  126. ID: 2,
  127. Start: 0x1000,
  128. Limit: 0x4000,
  129. File: "/lib/lib.so",
  130. HasFunctions: true,
  131. HasFilenames: true,
  132. HasLineNumbers: true,
  133. HasInlineFrames: true,
  134. },
  135. {
  136. ID: 3,
  137. Start: 0x4000,
  138. Limit: 0x5000,
  139. File: "/lib/lib2_c.so.6",
  140. HasFunctions: true,
  141. HasFilenames: true,
  142. HasLineNumbers: true,
  143. HasInlineFrames: true,
  144. },
  145. {
  146. ID: 4,
  147. Start: 0x5000,
  148. Limit: 0x9000,
  149. File: "/lib/lib.so_6 (deleted)",
  150. HasFunctions: true,
  151. HasFilenames: true,
  152. HasLineNumbers: true,
  153. HasInlineFrames: true,
  154. },
  155. }
  156. var cpuF = []*Function{
  157. {ID: 1, Name: "main", SystemName: "main", Filename: "main.c"},
  158. {ID: 2, Name: "foo", SystemName: "foo", Filename: "foo.c"},
  159. {ID: 3, Name: "foo_caller", SystemName: "foo_caller", Filename: "foo.c"},
  160. }
  161. var cpuL = []*Location{
  162. {
  163. ID: 1000,
  164. Mapping: cpuM[1],
  165. Address: 0x1000,
  166. Line: []Line{
  167. {Function: cpuF[0], Line: 1},
  168. },
  169. },
  170. {
  171. ID: 2000,
  172. Mapping: cpuM[0],
  173. Address: 0x2000,
  174. Line: []Line{
  175. {Function: cpuF[1], Line: 2},
  176. {Function: cpuF[2], Line: 1},
  177. },
  178. },
  179. {
  180. ID: 3000,
  181. Mapping: cpuM[0],
  182. Address: 0x3000,
  183. Line: []Line{
  184. {Function: cpuF[1], Line: 2},
  185. {Function: cpuF[2], Line: 1},
  186. },
  187. },
  188. {
  189. ID: 3001,
  190. Mapping: cpuM[0],
  191. Address: 0x3001,
  192. Line: []Line{
  193. {Function: cpuF[2], Line: 2},
  194. },
  195. },
  196. {
  197. ID: 3002,
  198. Mapping: cpuM[0],
  199. Address: 0x3002,
  200. Line: []Line{
  201. {Function: cpuF[2], Line: 3},
  202. },
  203. },
  204. }
  205. var testProfile1 = &Profile{
  206. PeriodType: &ValueType{Type: "cpu", Unit: "milliseconds"},
  207. Period: 1,
  208. DurationNanos: 10e9,
  209. SampleType: []*ValueType{
  210. {Type: "samples", Unit: "count"},
  211. {Type: "cpu", Unit: "milliseconds"},
  212. },
  213. Sample: []*Sample{
  214. {
  215. Location: []*Location{cpuL[0]},
  216. Value: []int64{1000, 1000},
  217. Label: map[string][]string{
  218. "key1": {"tag1"},
  219. "key2": {"tag1"},
  220. },
  221. },
  222. {
  223. Location: []*Location{cpuL[1], cpuL[0]},
  224. Value: []int64{100, 100},
  225. Label: map[string][]string{
  226. "key1": {"tag2"},
  227. "key3": {"tag2"},
  228. },
  229. },
  230. {
  231. Location: []*Location{cpuL[2], cpuL[0]},
  232. Value: []int64{10, 10},
  233. Label: map[string][]string{
  234. "key1": {"tag3"},
  235. "key2": {"tag2"},
  236. },
  237. },
  238. {
  239. Location: []*Location{cpuL[3], cpuL[0]},
  240. Value: []int64{10000, 10000},
  241. Label: map[string][]string{
  242. "key1": {"tag4"},
  243. "key2": {"tag1"},
  244. },
  245. },
  246. {
  247. Location: []*Location{cpuL[4], cpuL[0]},
  248. Value: []int64{1, 1},
  249. Label: map[string][]string{
  250. "key1": {"tag4"},
  251. "key2": {"tag1"},
  252. },
  253. },
  254. },
  255. Location: cpuL,
  256. Function: cpuF,
  257. Mapping: cpuM,
  258. }
  259. var testProfile2 = &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{70, 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{60, 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{50, 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{40, 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 testProfile3 = &Profile{
  314. PeriodType: &ValueType{Type: "cpu", Unit: "milliseconds"},
  315. Period: 1,
  316. DurationNanos: 10e9,
  317. SampleType: []*ValueType{
  318. {Type: "samples", Unit: "count"},
  319. },
  320. Sample: []*Sample{
  321. {
  322. Location: []*Location{cpuL[0]},
  323. Value: []int64{1000},
  324. Label: map[string][]string{
  325. "key1": {"tag1"},
  326. "key2": {"tag1"},
  327. },
  328. },
  329. },
  330. Location: cpuL,
  331. Function: cpuF,
  332. Mapping: cpuM,
  333. }
  334. var testProfile4 = &Profile{
  335. PeriodType: &ValueType{Type: "cpu", Unit: "milliseconds"},
  336. Period: 1,
  337. DurationNanos: 10e9,
  338. SampleType: []*ValueType{
  339. {Type: "samples", Unit: "count"},
  340. },
  341. Sample: []*Sample{
  342. {
  343. Location: []*Location{cpuL[0]},
  344. Value: []int64{1000},
  345. NumLabel: map[string][]int64{
  346. "key1": {10},
  347. "key2": {30},
  348. },
  349. NumUnit: map[string][]string{
  350. "key1": {"bytes"},
  351. "key2": {"bytes"},
  352. },
  353. },
  354. },
  355. Location: cpuL,
  356. Function: cpuF,
  357. Mapping: cpuM,
  358. }
  359. var testProfile5 = &Profile{
  360. PeriodType: &ValueType{Type: "cpu", Unit: "milliseconds"},
  361. Period: 1,
  362. DurationNanos: 10e9,
  363. SampleType: []*ValueType{
  364. {Type: "samples", Unit: "count"},
  365. },
  366. Sample: []*Sample{
  367. {
  368. Location: []*Location{cpuL[0]},
  369. Value: []int64{1000},
  370. NumLabel: map[string][]int64{
  371. "key1": {10},
  372. "key2": {30},
  373. },
  374. NumUnit: map[string][]string{
  375. "key1": {"bytes"},
  376. "key2": {"bytes"},
  377. },
  378. },
  379. {
  380. Location: []*Location{cpuL[0]},
  381. Value: []int64{1000},
  382. NumLabel: map[string][]int64{
  383. "key1": {10},
  384. "key2": {30},
  385. },
  386. NumUnit: map[string][]string{
  387. "key1": {"kilobytes"},
  388. "key2": {"kilobytes"},
  389. },
  390. },
  391. },
  392. Location: cpuL,
  393. Function: cpuF,
  394. Mapping: cpuM,
  395. }
  396. var aggTests = map[string]aggTest{
  397. "precise": {true, true, true, true, 5},
  398. "fileline": {false, true, true, true, 4},
  399. "inline_function": {false, true, false, true, 3},
  400. "function": {false, true, false, false, 2},
  401. }
  402. type aggTest struct {
  403. precise, function, fileline, inlineFrame bool
  404. rows int
  405. }
  406. const totalSamples = int64(11111)
  407. func TestAggregation(t *testing.T) {
  408. prof := testProfile1.Copy()
  409. for _, resolution := range []string{"precise", "fileline", "inline_function", "function"} {
  410. a := aggTests[resolution]
  411. if !a.precise {
  412. if err := prof.Aggregate(a.inlineFrame, a.function, a.fileline, a.fileline, false); err != nil {
  413. t.Error("aggregating to " + resolution + ":" + err.Error())
  414. }
  415. }
  416. if err := checkAggregation(prof, &a); err != nil {
  417. t.Error("failed aggregation to " + resolution + ": " + err.Error())
  418. }
  419. }
  420. }
  421. // checkAggregation verifies that the profile remained consistent
  422. // with its aggregation.
  423. func checkAggregation(prof *Profile, a *aggTest) error {
  424. // Check that the total number of samples for the rows was preserved.
  425. total := int64(0)
  426. samples := make(map[string]bool)
  427. for _, sample := range prof.Sample {
  428. tb := locationHash(sample)
  429. samples[tb] = true
  430. total += sample.Value[0]
  431. }
  432. if total != totalSamples {
  433. return fmt.Errorf("sample total %d, want %d", total, totalSamples)
  434. }
  435. // Check the number of unique sample locations
  436. if a.rows != len(samples) {
  437. return fmt.Errorf("number of samples %d, want %d", len(samples), a.rows)
  438. }
  439. // Check that all mappings have the right detail flags.
  440. for _, m := range prof.Mapping {
  441. if m.HasFunctions != a.function {
  442. return fmt.Errorf("unexpected mapping.HasFunctions %v, want %v", m.HasFunctions, a.function)
  443. }
  444. if m.HasFilenames != a.fileline {
  445. return fmt.Errorf("unexpected mapping.HasFilenames %v, want %v", m.HasFilenames, a.fileline)
  446. }
  447. if m.HasLineNumbers != a.fileline {
  448. return fmt.Errorf("unexpected mapping.HasLineNumbers %v, want %v", m.HasLineNumbers, a.fileline)
  449. }
  450. if m.HasInlineFrames != a.inlineFrame {
  451. return fmt.Errorf("unexpected mapping.HasInlineFrames %v, want %v", m.HasInlineFrames, a.inlineFrame)
  452. }
  453. }
  454. // Check that aggregation has removed finer resolution data.
  455. for _, l := range prof.Location {
  456. if !a.inlineFrame && len(l.Line) > 1 {
  457. return fmt.Errorf("found %d lines on location %d, want 1", len(l.Line), l.ID)
  458. }
  459. for _, ln := range l.Line {
  460. if !a.fileline && (ln.Function.Filename != "" || ln.Line != 0) {
  461. return fmt.Errorf("found line %s:%d on location %d, want :0",
  462. ln.Function.Filename, ln.Line, l.ID)
  463. }
  464. if !a.function && (ln.Function.Name != "") {
  465. return fmt.Errorf(`found file %s location %d, want ""`,
  466. ln.Function.Name, l.ID)
  467. }
  468. }
  469. }
  470. return nil
  471. }
  472. // Test merge leaves the main binary in place.
  473. func TestMergeMain(t *testing.T) {
  474. prof := testProfile1.Copy()
  475. p1, err := Merge([]*Profile{prof})
  476. if err != nil {
  477. t.Fatalf("merge error: %v", err)
  478. }
  479. if cpuM[0].File != p1.Mapping[0].File {
  480. t.Errorf("want Mapping[0]=%s got %s", cpuM[0].File, p1.Mapping[0].File)
  481. }
  482. }
  483. func TestMerge(t *testing.T) {
  484. // Aggregate a profile with itself and once again with a factor of
  485. // -2. Should end up with an empty profile (all samples for a
  486. // location should add up to 0).
  487. prof := testProfile1.Copy()
  488. p1, err := Merge([]*Profile{prof, prof})
  489. if err != nil {
  490. t.Errorf("merge error: %v", err)
  491. }
  492. prof.Scale(-2)
  493. prof, err = Merge([]*Profile{p1, prof})
  494. if err != nil {
  495. t.Errorf("merge error: %v", err)
  496. }
  497. // Use aggregation to merge locations at function granularity.
  498. if err := prof.Aggregate(false, true, false, false, false); err != nil {
  499. t.Errorf("aggregating after merge: %v", err)
  500. }
  501. samples := make(map[string]int64)
  502. for _, s := range prof.Sample {
  503. tb := locationHash(s)
  504. samples[tb] = samples[tb] + s.Value[0]
  505. }
  506. for s, v := range samples {
  507. if v != 0 {
  508. t.Errorf("nonzero value for sample %s: %d", s, v)
  509. }
  510. }
  511. }
  512. func TestMergeAll(t *testing.T) {
  513. // Aggregate 10 copies of the profile.
  514. profs := make([]*Profile, 10)
  515. for i := 0; i < 10; i++ {
  516. profs[i] = testProfile1.Copy()
  517. }
  518. prof, err := Merge(profs)
  519. if err != nil {
  520. t.Errorf("merge error: %v", err)
  521. }
  522. samples := make(map[string]int64)
  523. for _, s := range prof.Sample {
  524. tb := locationHash(s)
  525. samples[tb] = samples[tb] + s.Value[0]
  526. }
  527. for _, s := range testProfile1.Sample {
  528. tb := locationHash(s)
  529. if samples[tb] != s.Value[0]*10 {
  530. t.Errorf("merge got wrong value at %s : %d instead of %d", tb, samples[tb], s.Value[0]*10)
  531. }
  532. }
  533. }
  534. func TestNumLabelMerge(t *testing.T) {
  535. for _, tc := range []struct {
  536. name string
  537. profs []*Profile
  538. wantNumLabels []map[string][]int64
  539. wantNumUnits []map[string][]string
  540. }{
  541. {
  542. name: "different tag units not merged",
  543. profs: []*Profile{testProfile4.Copy(), testProfile5.Copy()},
  544. wantNumLabels: []map[string][]int64{
  545. {
  546. "key1": {10},
  547. "key2": {30},
  548. },
  549. {
  550. "key1": {10},
  551. "key2": {30},
  552. },
  553. },
  554. wantNumUnits: []map[string][]string{
  555. {
  556. "key1": {"bytes"},
  557. "key2": {"bytes"},
  558. },
  559. {
  560. "key1": {"kilobytes"},
  561. "key2": {"kilobytes"},
  562. },
  563. },
  564. },
  565. } {
  566. t.Run(tc.name, func(t *testing.T) {
  567. prof, err := Merge(tc.profs)
  568. if err != nil {
  569. t.Errorf("merge error: %v", err)
  570. }
  571. if want, got := len(tc.wantNumLabels), len(prof.Sample); want != got {
  572. t.Fatalf("got %d samples, want %d samples", got, want)
  573. }
  574. for i, wantLabels := range tc.wantNumLabels {
  575. numLabels := prof.Sample[i].NumLabel
  576. if !reflect.DeepEqual(wantLabels, numLabels) {
  577. t.Errorf("got numeric labels %v, want %v", numLabels, wantLabels)
  578. }
  579. wantUnits := tc.wantNumUnits[i]
  580. numUnits := prof.Sample[i].NumUnit
  581. if !reflect.DeepEqual(wantUnits, numUnits) {
  582. t.Errorf("got numeric labels %v, want %v", numUnits, wantUnits)
  583. }
  584. }
  585. })
  586. }
  587. }
  588. func TestNormalizeBySameProfile(t *testing.T) {
  589. pb := testProfile1.Copy()
  590. p := testProfile1.Copy()
  591. if err := p.Normalize(pb); err != nil {
  592. t.Fatal(err)
  593. }
  594. for i, s := range p.Sample {
  595. for j, v := range s.Value {
  596. expectedSampleValue := testProfile1.Sample[i].Value[j]
  597. if v != expectedSampleValue {
  598. t.Errorf("For sample %d, value %d want %d got %d", i, j, expectedSampleValue, v)
  599. }
  600. }
  601. }
  602. }
  603. func TestNormalizeByDifferentProfile(t *testing.T) {
  604. p := testProfile1.Copy()
  605. pb := testProfile2.Copy()
  606. if err := p.Normalize(pb); err != nil {
  607. t.Fatal(err)
  608. }
  609. expectedSampleValues := [][]int64{
  610. {19, 1000},
  611. {1, 100},
  612. {0, 10},
  613. {198, 10000},
  614. {0, 1},
  615. }
  616. for i, s := range p.Sample {
  617. for j, v := range s.Value {
  618. if v != expectedSampleValues[i][j] {
  619. t.Errorf("For sample %d, value %d want %d got %d", i, j, expectedSampleValues[i][j], v)
  620. }
  621. }
  622. }
  623. }
  624. func TestNormalizeByMultipleOfSameProfile(t *testing.T) {
  625. pb := testProfile1.Copy()
  626. for i, s := range pb.Sample {
  627. for j, v := range s.Value {
  628. pb.Sample[i].Value[j] = 10 * v
  629. }
  630. }
  631. p := testProfile1.Copy()
  632. err := p.Normalize(pb)
  633. if err != nil {
  634. t.Fatal(err)
  635. }
  636. for i, s := range p.Sample {
  637. for j, v := range s.Value {
  638. expectedSampleValue := 10 * testProfile1.Sample[i].Value[j]
  639. if v != expectedSampleValue {
  640. t.Errorf("For sample %d, value %d, want %d got %d", i, j, expectedSampleValue, v)
  641. }
  642. }
  643. }
  644. }
  645. func TestNormalizeIncompatibleProfiles(t *testing.T) {
  646. p := testProfile1.Copy()
  647. pb := testProfile3.Copy()
  648. if err := p.Normalize(pb); err == nil {
  649. t.Errorf("Expected an error")
  650. }
  651. }
  652. func TestFilter(t *testing.T) {
  653. // Perform several forms of filtering on the test profile.
  654. type filterTestcase struct {
  655. focus, ignore, hide, show *regexp.Regexp
  656. fm, im, hm, hnm bool
  657. }
  658. for tx, tc := range []filterTestcase{
  659. {
  660. fm: true, // nil focus matches every sample
  661. },
  662. {
  663. focus: regexp.MustCompile("notfound"),
  664. },
  665. {
  666. ignore: regexp.MustCompile("foo.c"),
  667. fm: true,
  668. im: true,
  669. },
  670. {
  671. hide: regexp.MustCompile("lib.so"),
  672. fm: true,
  673. hm: true,
  674. },
  675. {
  676. show: regexp.MustCompile("foo.c"),
  677. fm: true,
  678. hnm: true,
  679. },
  680. {
  681. show: regexp.MustCompile("notfound"),
  682. fm: true,
  683. },
  684. } {
  685. prof := *testProfile1.Copy()
  686. gf, gi, gh, gnh := prof.FilterSamplesByName(tc.focus, tc.ignore, tc.hide, tc.show)
  687. if gf != tc.fm {
  688. t.Errorf("Filter #%d, got fm=%v, want %v", tx, gf, tc.fm)
  689. }
  690. if gi != tc.im {
  691. t.Errorf("Filter #%d, got im=%v, want %v", tx, gi, tc.im)
  692. }
  693. if gh != tc.hm {
  694. t.Errorf("Filter #%d, got hm=%v, want %v", tx, gh, tc.hm)
  695. }
  696. if gnh != tc.hnm {
  697. t.Errorf("Filter #%d, got hnm=%v, want %v", tx, gnh, tc.hnm)
  698. }
  699. }
  700. }
  701. func TestTagFilter(t *testing.T) {
  702. // Perform several forms of tag filtering on the test profile.
  703. type filterTestcase struct {
  704. include, exclude *regexp.Regexp
  705. im, em bool
  706. count int
  707. }
  708. countTags := func(p *Profile) map[string]bool {
  709. tags := make(map[string]bool)
  710. for _, s := range p.Sample {
  711. for l := range s.Label {
  712. tags[l] = true
  713. }
  714. for l := range s.NumLabel {
  715. tags[l] = true
  716. }
  717. }
  718. return tags
  719. }
  720. for tx, tc := range []filterTestcase{
  721. {nil, nil, true, false, 3},
  722. {regexp.MustCompile("notfound"), nil, false, false, 0},
  723. {regexp.MustCompile("key1"), nil, true, false, 1},
  724. {nil, regexp.MustCompile("key[12]"), true, true, 1},
  725. } {
  726. prof := testProfile1.Copy()
  727. gim, gem := prof.FilterTagsByName(tc.include, tc.exclude)
  728. if gim != tc.im {
  729. t.Errorf("Filter #%d, got include match=%v, want %v", tx, gim, tc.im)
  730. }
  731. if gem != tc.em {
  732. t.Errorf("Filter #%d, got exclude match=%v, want %v", tx, gem, tc.em)
  733. }
  734. if tags := countTags(prof); len(tags) != tc.count {
  735. t.Errorf("Filter #%d, got %d tags[%v], want %d", tx, len(tags), tags, tc.count)
  736. }
  737. }
  738. }
  739. // locationHash constructs a string to use as a hashkey for a sample, based on its locations
  740. func locationHash(s *Sample) string {
  741. var tb string
  742. for _, l := range s.Location {
  743. for _, ln := range l.Line {
  744. tb = tb + fmt.Sprintf("%s:%d@%d ", ln.Function.Name, ln.Line, l.Address)
  745. }
  746. }
  747. return tb
  748. }
  749. func TestInferUnits(t *testing.T) {
  750. var tagFilterTests = []struct {
  751. name string
  752. tagVals []map[string][]int64
  753. tagUnits []map[string][]string
  754. wantUnits map[string]string
  755. wantIgnoredUnits map[string][]string
  756. }{
  757. {
  758. "One sample, multiple keys, different specified units",
  759. []map[string][]int64{{"key1": {131072}, "key2": {128}}},
  760. []map[string][]string{{"key1": {"bytes"}, "key2": {"kilobytes"}}},
  761. map[string]string{"key1": "bytes", "key2": "kilobytes"},
  762. map[string][]string{},
  763. },
  764. {
  765. "One sample, one key with one value, unit specified",
  766. []map[string][]int64{{"key1": {8}}},
  767. []map[string][]string{{"key1": {"bytes"}}},
  768. map[string]string{"key1": "bytes"},
  769. map[string][]string{},
  770. },
  771. {
  772. "Key bytes, unit not specified",
  773. []map[string][]int64{{"bytes": {8}}},
  774. []map[string][]string{nil},
  775. map[string]string{"bytes": "bytes"},
  776. map[string][]string{},
  777. },
  778. {
  779. "One sample, one key with one value, unit not specified",
  780. []map[string][]int64{{"kilobytes": {8}}},
  781. []map[string][]string{nil},
  782. map[string]string{"kilobytes": "kilobytes"},
  783. map[string][]string{},
  784. },
  785. {
  786. "Key request, unit not specified",
  787. []map[string][]int64{{"request": {8}}},
  788. []map[string][]string{nil},
  789. map[string]string{"request": "bytes"},
  790. map[string][]string{},
  791. },
  792. {
  793. "Key alignment, unit not specified",
  794. []map[string][]int64{{"alignment": {8}}},
  795. []map[string][]string{nil},
  796. map[string]string{"alignment": "bytes"},
  797. map[string][]string{},
  798. },
  799. {
  800. "One sample, one key with multiple values and different units",
  801. []map[string][]int64{{"key1": {8, 8}}},
  802. []map[string][]string{{"key1": {"bytes", "kilobytes"}}},
  803. map[string]string{"key1": "bytes"},
  804. map[string][]string{"key1": {"kilobytes"}},
  805. },
  806. {
  807. "Two samples, one key, different units specified",
  808. []map[string][]int64{{"key1": {8}}, {"key1": {8}}},
  809. []map[string][]string{{"key1": {"bytes"}}, {"key1": {"kilobytes"}}},
  810. map[string]string{"key1": "bytes"},
  811. map[string][]string{"key1": {"kilobytes"}},
  812. },
  813. {
  814. "Keys alignment, request, and bytes have units specified",
  815. []map[string][]int64{{
  816. "alignment": {8},
  817. "request": {8},
  818. "bytes": {8},
  819. }},
  820. []map[string][]string{{
  821. "alignment": {"seconds"},
  822. "request": {"minutes"},
  823. "bytes": {"hours"},
  824. }},
  825. map[string]string{
  826. "alignment": "seconds",
  827. "request": "minutes",
  828. "bytes": "hours",
  829. },
  830. map[string][]string{},
  831. },
  832. }
  833. for _, test := range tagFilterTests {
  834. p := &Profile{Sample: make([]*Sample, len(test.tagVals))}
  835. for i, numLabel := range test.tagVals {
  836. s := Sample{
  837. NumLabel: numLabel,
  838. NumUnit: test.tagUnits[i],
  839. }
  840. p.Sample[i] = &s
  841. }
  842. units, ignoredUnits := p.NumLabelUnits()
  843. for key, wantUnit := range test.wantUnits {
  844. unit := units[key]
  845. if wantUnit != unit {
  846. t.Errorf("%s: for key %s, got unit %s, want unit %s", test.name, key, unit, wantUnit)
  847. }
  848. }
  849. for key, ignored := range ignoredUnits {
  850. wantIgnored := test.wantIgnoredUnits[key]
  851. if len(wantIgnored) != len(ignored) {
  852. t.Errorf("%s: for key %s, got ignored units %v, got ignored units %v", test.name, key, ignored, wantIgnored)
  853. continue
  854. }
  855. for i, want := range wantIgnored {
  856. if got := ignored[i]; want != got {
  857. t.Errorf("%s: for key %s, got ignored units %v, want ignored units %v", test.name, key, ignored, wantIgnored)
  858. break
  859. }
  860. }
  861. }
  862. }
  863. }
  864. func TestSetMain(t *testing.T) {
  865. testProfile1.massageMappings()
  866. if testProfile1.Mapping[0].File != mainBinary {
  867. t.Errorf("got %s for main", testProfile1.Mapping[0].File)
  868. }
  869. }
  870. // parallel runs n copies of fn in parallel.
  871. func parallel(n int, fn func()) {
  872. var wg sync.WaitGroup
  873. wg.Add(n)
  874. for i := 0; i < n; i++ {
  875. go func() {
  876. fn()
  877. wg.Done()
  878. }()
  879. }
  880. wg.Wait()
  881. }
  882. func TestThreadSafety(t *testing.T) {
  883. src := testProfile1.Copy()
  884. parallel(4, func() { src.Copy() })
  885. parallel(4, func() {
  886. var b bytes.Buffer
  887. src.WriteUncompressed(&b)
  888. })
  889. parallel(4, func() {
  890. var b bytes.Buffer
  891. src.Write(&b)
  892. })
  893. }