No Description

profile_test.go 34KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390
  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. "flag"
  18. "fmt"
  19. "io/ioutil"
  20. "path/filepath"
  21. "reflect"
  22. "strings"
  23. "sync"
  24. "testing"
  25. "github.com/google/pprof/internal/proftest"
  26. )
  27. var update = flag.Bool("update", false, "Update the golden files")
  28. func TestParse(t *testing.T) {
  29. const path = "testdata/"
  30. for _, source := range []string{
  31. "go.crc32.cpu",
  32. "go.godoc.thread",
  33. "gobench.cpu",
  34. "gobench.heap",
  35. "cppbench.cpu",
  36. "cppbench.heap",
  37. "cppbench.contention",
  38. "cppbench.growth",
  39. "cppbench.thread",
  40. "cppbench.thread.all",
  41. "cppbench.thread.none",
  42. "java.cpu",
  43. "java.heap",
  44. "java.contention",
  45. } {
  46. inbytes, err := ioutil.ReadFile(filepath.Join(path, source))
  47. if err != nil {
  48. t.Fatal(err)
  49. }
  50. p, err := Parse(bytes.NewBuffer(inbytes))
  51. if err != nil {
  52. t.Fatalf("%s: %s", source, err)
  53. }
  54. js := p.String()
  55. goldFilename := path + source + ".string"
  56. if *update {
  57. err := ioutil.WriteFile(goldFilename, []byte(js), 0644)
  58. if err != nil {
  59. t.Errorf("failed to update the golden file file %q: %v", goldFilename, err)
  60. }
  61. }
  62. gold, err := ioutil.ReadFile(goldFilename)
  63. if err != nil {
  64. t.Fatalf("%s: %v", source, err)
  65. }
  66. if js != string(gold) {
  67. t.Errorf("diff %s %s", source, goldFilename)
  68. d, err := proftest.Diff(gold, []byte(js))
  69. if err != nil {
  70. t.Fatalf("%s: %v", source, err)
  71. }
  72. t.Error(source + "\n" + string(d) + "\n" + "new profile at:\n" + leaveTempfile([]byte(js)))
  73. }
  74. // Reencode and decode.
  75. var bw bytes.Buffer
  76. if err := p.Write(&bw); err != nil {
  77. t.Fatalf("%s: %v", source, err)
  78. }
  79. if p, err = Parse(&bw); err != nil {
  80. t.Fatalf("%s: %v", source, err)
  81. }
  82. js2 := p.String()
  83. if js2 != string(gold) {
  84. d, err := proftest.Diff(gold, []byte(js2))
  85. if err != nil {
  86. t.Fatalf("%s: %v", source, err)
  87. }
  88. t.Error(source + "\n" + string(d) + "\n" + "gold:\n" + goldFilename +
  89. "\nnew profile at:\n" + leaveTempfile([]byte(js)))
  90. }
  91. }
  92. }
  93. func TestParseError(t *testing.T) {
  94. testcases := []string{
  95. "",
  96. "garbage text",
  97. "\x1f\x8b", // truncated gzip header
  98. "\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
  99. }
  100. for i, input := range testcases {
  101. _, err := Parse(strings.NewReader(input))
  102. if err == nil {
  103. t.Errorf("got nil, want error for input #%d", i)
  104. }
  105. }
  106. }
  107. func TestParseConcatentated(t *testing.T) {
  108. prof := testProfile1.Copy()
  109. // Write the profile twice to buffer to create concatented profile.
  110. var buf bytes.Buffer
  111. prof.Write(&buf)
  112. prof.Write(&buf)
  113. _, err := Parse(&buf)
  114. if err == nil {
  115. t.Fatalf("got nil, want error")
  116. }
  117. if got, want := err.Error(), "parsing profile: concatenated profiles detected"; want != got {
  118. t.Fatalf("got error %q, want error %q", got, want)
  119. }
  120. }
  121. func TestCheckValid(t *testing.T) {
  122. const path = "testdata/java.cpu"
  123. inbytes, err := ioutil.ReadFile(path)
  124. if err != nil {
  125. t.Fatalf("failed to read profile file %q: %v", path, err)
  126. }
  127. p, err := Parse(bytes.NewBuffer(inbytes))
  128. if err != nil {
  129. t.Fatalf("failed to parse profile %q: %s", path, err)
  130. }
  131. for _, tc := range []struct {
  132. mutateFn func(*Profile)
  133. wantErr string
  134. }{
  135. {
  136. mutateFn: func(p *Profile) { p.SampleType = nil },
  137. wantErr: "missing sample type information",
  138. },
  139. {
  140. mutateFn: func(p *Profile) { p.Sample[0] = nil },
  141. wantErr: "profile has nil sample",
  142. },
  143. {
  144. mutateFn: func(p *Profile) { p.Sample[0].Value = append(p.Sample[0].Value, 0) },
  145. wantErr: "sample has 3 values vs. 2 types",
  146. },
  147. {
  148. mutateFn: func(p *Profile) { p.Sample[0].Location[0] = nil },
  149. wantErr: "sample has nil location",
  150. },
  151. {
  152. mutateFn: func(p *Profile) { p.Location[0] = nil },
  153. wantErr: "profile has nil location",
  154. },
  155. {
  156. mutateFn: func(p *Profile) { p.Mapping = append(p.Mapping, nil) },
  157. wantErr: "profile has nil mapping",
  158. },
  159. {
  160. mutateFn: func(p *Profile) { p.Function[0] = nil },
  161. wantErr: "profile has nil function",
  162. },
  163. } {
  164. t.Run(tc.wantErr, func(t *testing.T) {
  165. p := p.Copy()
  166. tc.mutateFn(p)
  167. if err := p.CheckValid(); err == nil {
  168. t.Errorf("CheckValid(): got no error, want error %q", tc.wantErr)
  169. } else if !strings.Contains(err.Error(), tc.wantErr) {
  170. t.Errorf("CheckValid(): got error %v, want error %q", err, tc.wantErr)
  171. }
  172. })
  173. }
  174. }
  175. // leaveTempfile leaves |b| in a temporary file on disk and returns the
  176. // temp filename. This is useful to recover a profile when the test
  177. // fails.
  178. func leaveTempfile(b []byte) string {
  179. f1, err := ioutil.TempFile("", "profile_test")
  180. if err != nil {
  181. panic(err)
  182. }
  183. if _, err := f1.Write(b); err != nil {
  184. panic(err)
  185. }
  186. return f1.Name()
  187. }
  188. const mainBinary = "/bin/main"
  189. var cpuM = []*Mapping{
  190. {
  191. ID: 1,
  192. Start: 0x10000,
  193. Limit: 0x40000,
  194. File: mainBinary,
  195. HasFunctions: true,
  196. HasFilenames: true,
  197. HasLineNumbers: true,
  198. HasInlineFrames: true,
  199. },
  200. {
  201. ID: 2,
  202. Start: 0x1000,
  203. Limit: 0x4000,
  204. File: "/lib/lib.so",
  205. HasFunctions: true,
  206. HasFilenames: true,
  207. HasLineNumbers: true,
  208. HasInlineFrames: true,
  209. },
  210. {
  211. ID: 3,
  212. Start: 0x4000,
  213. Limit: 0x5000,
  214. File: "/lib/lib2_c.so.6",
  215. HasFunctions: true,
  216. HasFilenames: true,
  217. HasLineNumbers: true,
  218. HasInlineFrames: true,
  219. },
  220. {
  221. ID: 4,
  222. Start: 0x5000,
  223. Limit: 0x9000,
  224. File: "/lib/lib.so_6 (deleted)",
  225. HasFunctions: true,
  226. HasFilenames: true,
  227. HasLineNumbers: true,
  228. HasInlineFrames: true,
  229. },
  230. }
  231. var cpuF = []*Function{
  232. {ID: 1, Name: "main", SystemName: "main", Filename: "main.c"},
  233. {ID: 2, Name: "foo", SystemName: "foo", Filename: "foo.c"},
  234. {ID: 3, Name: "foo_caller", SystemName: "foo_caller", Filename: "foo.c"},
  235. }
  236. var cpuL = []*Location{
  237. {
  238. ID: 1000,
  239. Mapping: cpuM[1],
  240. Address: 0x1000,
  241. Line: []Line{
  242. {Function: cpuF[0], Line: 1},
  243. },
  244. },
  245. {
  246. ID: 2000,
  247. Mapping: cpuM[0],
  248. Address: 0x2000,
  249. Line: []Line{
  250. {Function: cpuF[1], Line: 2},
  251. {Function: cpuF[2], Line: 1},
  252. },
  253. },
  254. {
  255. ID: 3000,
  256. Mapping: cpuM[0],
  257. Address: 0x3000,
  258. Line: []Line{
  259. {Function: cpuF[1], Line: 2},
  260. {Function: cpuF[2], Line: 1},
  261. },
  262. },
  263. {
  264. ID: 3001,
  265. Mapping: cpuM[0],
  266. Address: 0x3001,
  267. Line: []Line{
  268. {Function: cpuF[2], Line: 2},
  269. },
  270. },
  271. {
  272. ID: 3002,
  273. Mapping: cpuM[0],
  274. Address: 0x3002,
  275. Line: []Line{
  276. {Function: cpuF[2], Line: 3},
  277. },
  278. },
  279. }
  280. var testProfile1 = &Profile{
  281. TimeNanos: 10000,
  282. PeriodType: &ValueType{Type: "cpu", Unit: "milliseconds"},
  283. Period: 1,
  284. DurationNanos: 10e9,
  285. SampleType: []*ValueType{
  286. {Type: "samples", Unit: "count"},
  287. {Type: "cpu", Unit: "milliseconds"},
  288. },
  289. Sample: []*Sample{
  290. {
  291. Location: []*Location{cpuL[0]},
  292. Value: []int64{1000, 1000},
  293. Label: map[string][]string{
  294. "key1": {"tag1"},
  295. "key2": {"tag1"},
  296. },
  297. },
  298. {
  299. Location: []*Location{cpuL[1], cpuL[0]},
  300. Value: []int64{100, 100},
  301. Label: map[string][]string{
  302. "key1": {"tag2"},
  303. "key3": {"tag2"},
  304. },
  305. },
  306. {
  307. Location: []*Location{cpuL[2], cpuL[0]},
  308. Value: []int64{10, 10},
  309. Label: map[string][]string{
  310. "key1": {"tag3"},
  311. "key2": {"tag2"},
  312. },
  313. },
  314. {
  315. Location: []*Location{cpuL[3], cpuL[0]},
  316. Value: []int64{10000, 10000},
  317. Label: map[string][]string{
  318. "key1": {"tag4"},
  319. "key2": {"tag1"},
  320. },
  321. },
  322. {
  323. Location: []*Location{cpuL[4], cpuL[0]},
  324. Value: []int64{1, 1},
  325. Label: map[string][]string{
  326. "key1": {"tag4"},
  327. "key2": {"tag1"},
  328. },
  329. },
  330. },
  331. Location: cpuL,
  332. Function: cpuF,
  333. Mapping: cpuM,
  334. }
  335. var testProfile1NoMapping = &Profile{
  336. PeriodType: &ValueType{Type: "cpu", Unit: "milliseconds"},
  337. Period: 1,
  338. DurationNanos: 10e9,
  339. SampleType: []*ValueType{
  340. {Type: "samples", Unit: "count"},
  341. {Type: "cpu", Unit: "milliseconds"},
  342. },
  343. Sample: []*Sample{
  344. {
  345. Location: []*Location{cpuL[0]},
  346. Value: []int64{1000, 1000},
  347. Label: map[string][]string{
  348. "key1": {"tag1"},
  349. "key2": {"tag1"},
  350. },
  351. },
  352. {
  353. Location: []*Location{cpuL[1], cpuL[0]},
  354. Value: []int64{100, 100},
  355. Label: map[string][]string{
  356. "key1": {"tag2"},
  357. "key3": {"tag2"},
  358. },
  359. },
  360. {
  361. Location: []*Location{cpuL[2], cpuL[0]},
  362. Value: []int64{10, 10},
  363. Label: map[string][]string{
  364. "key1": {"tag3"},
  365. "key2": {"tag2"},
  366. },
  367. },
  368. {
  369. Location: []*Location{cpuL[3], cpuL[0]},
  370. Value: []int64{10000, 10000},
  371. Label: map[string][]string{
  372. "key1": {"tag4"},
  373. "key2": {"tag1"},
  374. },
  375. },
  376. {
  377. Location: []*Location{cpuL[4], cpuL[0]},
  378. Value: []int64{1, 1},
  379. Label: map[string][]string{
  380. "key1": {"tag4"},
  381. "key2": {"tag1"},
  382. },
  383. },
  384. },
  385. Location: cpuL,
  386. Function: cpuF,
  387. }
  388. var testProfile2 = &Profile{
  389. PeriodType: &ValueType{Type: "cpu", Unit: "milliseconds"},
  390. Period: 1,
  391. DurationNanos: 10e9,
  392. SampleType: []*ValueType{
  393. {Type: "samples", Unit: "count"},
  394. {Type: "cpu", Unit: "milliseconds"},
  395. },
  396. Sample: []*Sample{
  397. {
  398. Location: []*Location{cpuL[0]},
  399. Value: []int64{70, 1000},
  400. Label: map[string][]string{
  401. "key1": {"tag1"},
  402. "key2": {"tag1"},
  403. },
  404. },
  405. {
  406. Location: []*Location{cpuL[1], cpuL[0]},
  407. Value: []int64{60, 100},
  408. Label: map[string][]string{
  409. "key1": {"tag2"},
  410. "key3": {"tag2"},
  411. },
  412. },
  413. {
  414. Location: []*Location{cpuL[2], cpuL[0]},
  415. Value: []int64{50, 10},
  416. Label: map[string][]string{
  417. "key1": {"tag3"},
  418. "key2": {"tag2"},
  419. },
  420. },
  421. {
  422. Location: []*Location{cpuL[3], cpuL[0]},
  423. Value: []int64{40, 10000},
  424. Label: map[string][]string{
  425. "key1": {"tag4"},
  426. "key2": {"tag1"},
  427. },
  428. },
  429. {
  430. Location: []*Location{cpuL[4], cpuL[0]},
  431. Value: []int64{1, 1},
  432. Label: map[string][]string{
  433. "key1": {"tag4"},
  434. "key2": {"tag1"},
  435. },
  436. },
  437. },
  438. Location: cpuL,
  439. Function: cpuF,
  440. Mapping: cpuM,
  441. }
  442. var testProfile3 = &Profile{
  443. PeriodType: &ValueType{Type: "cpu", Unit: "milliseconds"},
  444. Period: 1,
  445. DurationNanos: 10e9,
  446. SampleType: []*ValueType{
  447. {Type: "samples", Unit: "count"},
  448. },
  449. Sample: []*Sample{
  450. {
  451. Location: []*Location{cpuL[0]},
  452. Value: []int64{1000},
  453. Label: map[string][]string{
  454. "key1": {"tag1"},
  455. "key2": {"tag1"},
  456. },
  457. },
  458. },
  459. Location: cpuL,
  460. Function: cpuF,
  461. Mapping: cpuM,
  462. }
  463. var testProfile4 = &Profile{
  464. PeriodType: &ValueType{Type: "cpu", Unit: "milliseconds"},
  465. Period: 1,
  466. DurationNanos: 10e9,
  467. SampleType: []*ValueType{
  468. {Type: "samples", Unit: "count"},
  469. },
  470. Sample: []*Sample{
  471. {
  472. Location: []*Location{cpuL[0]},
  473. Value: []int64{1000},
  474. NumLabel: map[string][]int64{
  475. "key1": {10},
  476. "key2": {30},
  477. },
  478. NumUnit: map[string][]string{
  479. "key1": {"bytes"},
  480. "key2": {"bytes"},
  481. },
  482. },
  483. },
  484. Location: cpuL,
  485. Function: cpuF,
  486. Mapping: cpuM,
  487. }
  488. var testProfile5 = &Profile{
  489. PeriodType: &ValueType{Type: "cpu", Unit: "milliseconds"},
  490. Period: 1,
  491. DurationNanos: 10e9,
  492. SampleType: []*ValueType{
  493. {Type: "samples", Unit: "count"},
  494. },
  495. Sample: []*Sample{
  496. {
  497. Location: []*Location{cpuL[0]},
  498. Value: []int64{1000},
  499. NumLabel: map[string][]int64{
  500. "key1": {10},
  501. "key2": {30},
  502. },
  503. NumUnit: map[string][]string{
  504. "key1": {"bytes"},
  505. "key2": {"bytes"},
  506. },
  507. },
  508. {
  509. Location: []*Location{cpuL[0]},
  510. Value: []int64{1000},
  511. NumLabel: map[string][]int64{
  512. "key1": {10},
  513. "key2": {30},
  514. },
  515. NumUnit: map[string][]string{
  516. "key1": {"kilobytes"},
  517. "key2": {"kilobytes"},
  518. },
  519. },
  520. },
  521. Location: cpuL,
  522. Function: cpuF,
  523. Mapping: cpuM,
  524. }
  525. var aggTests = map[string]aggTest{
  526. "precise": {true, true, true, true, 5},
  527. "fileline": {false, true, true, true, 4},
  528. "inline_function": {false, true, false, true, 3},
  529. "function": {false, true, false, false, 2},
  530. }
  531. type aggTest struct {
  532. precise, function, fileline, inlineFrame bool
  533. rows int
  534. }
  535. const totalSamples = int64(11111)
  536. func TestAggregation(t *testing.T) {
  537. prof := testProfile1.Copy()
  538. for _, resolution := range []string{"precise", "fileline", "inline_function", "function"} {
  539. a := aggTests[resolution]
  540. if !a.precise {
  541. if err := prof.Aggregate(a.inlineFrame, a.function, a.fileline, a.fileline, false); err != nil {
  542. t.Error("aggregating to " + resolution + ":" + err.Error())
  543. }
  544. }
  545. if err := checkAggregation(prof, &a); err != nil {
  546. t.Error("failed aggregation to " + resolution + ": " + err.Error())
  547. }
  548. }
  549. }
  550. // checkAggregation verifies that the profile remained consistent
  551. // with its aggregation.
  552. func checkAggregation(prof *Profile, a *aggTest) error {
  553. // Check that the total number of samples for the rows was preserved.
  554. total := int64(0)
  555. samples := make(map[string]bool)
  556. for _, sample := range prof.Sample {
  557. tb := locationHash(sample)
  558. samples[tb] = true
  559. total += sample.Value[0]
  560. }
  561. if total != totalSamples {
  562. return fmt.Errorf("sample total %d, want %d", total, totalSamples)
  563. }
  564. // Check the number of unique sample locations
  565. if a.rows != len(samples) {
  566. return fmt.Errorf("number of samples %d, want %d", len(samples), a.rows)
  567. }
  568. // Check that all mappings have the right detail flags.
  569. for _, m := range prof.Mapping {
  570. if m.HasFunctions != a.function {
  571. return fmt.Errorf("unexpected mapping.HasFunctions %v, want %v", m.HasFunctions, a.function)
  572. }
  573. if m.HasFilenames != a.fileline {
  574. return fmt.Errorf("unexpected mapping.HasFilenames %v, want %v", m.HasFilenames, a.fileline)
  575. }
  576. if m.HasLineNumbers != a.fileline {
  577. return fmt.Errorf("unexpected mapping.HasLineNumbers %v, want %v", m.HasLineNumbers, a.fileline)
  578. }
  579. if m.HasInlineFrames != a.inlineFrame {
  580. return fmt.Errorf("unexpected mapping.HasInlineFrames %v, want %v", m.HasInlineFrames, a.inlineFrame)
  581. }
  582. }
  583. // Check that aggregation has removed finer resolution data.
  584. for _, l := range prof.Location {
  585. if !a.inlineFrame && len(l.Line) > 1 {
  586. return fmt.Errorf("found %d lines on location %d, want 1", len(l.Line), l.ID)
  587. }
  588. for _, ln := range l.Line {
  589. if !a.fileline && (ln.Function.Filename != "" || ln.Line != 0) {
  590. return fmt.Errorf("found line %s:%d on location %d, want :0",
  591. ln.Function.Filename, ln.Line, l.ID)
  592. }
  593. if !a.function && (ln.Function.Name != "") {
  594. return fmt.Errorf(`found file %s location %d, want ""`,
  595. ln.Function.Name, l.ID)
  596. }
  597. }
  598. }
  599. return nil
  600. }
  601. // TestMergeMain tests merge leaves the main binary in place.
  602. func TestMergeMain(t *testing.T) {
  603. prof := testProfile1.Copy()
  604. p1, err := Merge([]*Profile{prof})
  605. if err != nil {
  606. t.Fatalf("merge error: %v", err)
  607. }
  608. if cpuM[0].File != p1.Mapping[0].File {
  609. t.Errorf("want Mapping[0]=%s got %s", cpuM[0].File, p1.Mapping[0].File)
  610. }
  611. }
  612. func TestMerge(t *testing.T) {
  613. // Aggregate a profile with itself and once again with a factor of
  614. // -2. Should end up with an empty profile (all samples for a
  615. // location should add up to 0).
  616. prof := testProfile1.Copy()
  617. prof.Comments = []string{"comment1"}
  618. p1, err := Merge([]*Profile{prof, prof})
  619. if err != nil {
  620. t.Errorf("merge error: %v", err)
  621. }
  622. prof.Scale(-2)
  623. prof, err = Merge([]*Profile{p1, prof})
  624. if err != nil {
  625. t.Errorf("merge error: %v", err)
  626. }
  627. if got, want := len(prof.Comments), 1; got != want {
  628. t.Errorf("len(prof.Comments) = %d, want %d", got, want)
  629. }
  630. // Use aggregation to merge locations at function granularity.
  631. if err := prof.Aggregate(false, true, false, false, false); err != nil {
  632. t.Errorf("aggregating after merge: %v", err)
  633. }
  634. samples := make(map[string]int64)
  635. for _, s := range prof.Sample {
  636. tb := locationHash(s)
  637. samples[tb] = samples[tb] + s.Value[0]
  638. }
  639. for s, v := range samples {
  640. if v != 0 {
  641. t.Errorf("nonzero value for sample %s: %d", s, v)
  642. }
  643. }
  644. }
  645. func TestMergeAll(t *testing.T) {
  646. // Aggregate 10 copies of the profile.
  647. profs := make([]*Profile, 10)
  648. for i := 0; i < 10; i++ {
  649. profs[i] = testProfile1.Copy()
  650. }
  651. prof, err := Merge(profs)
  652. if err != nil {
  653. t.Errorf("merge error: %v", err)
  654. }
  655. samples := make(map[string]int64)
  656. for _, s := range prof.Sample {
  657. tb := locationHash(s)
  658. samples[tb] = samples[tb] + s.Value[0]
  659. }
  660. for _, s := range testProfile1.Sample {
  661. tb := locationHash(s)
  662. if samples[tb] != s.Value[0]*10 {
  663. t.Errorf("merge got wrong value at %s : %d instead of %d", tb, samples[tb], s.Value[0]*10)
  664. }
  665. }
  666. }
  667. func TestIsFoldedMerge(t *testing.T) {
  668. testProfile1Folded := testProfile1.Copy()
  669. testProfile1Folded.Location[0].IsFolded = true
  670. testProfile1Folded.Location[1].IsFolded = true
  671. for _, tc := range []struct {
  672. name string
  673. profs []*Profile
  674. wantLocationLen int
  675. }{
  676. {
  677. name: "folded and non-folded locations not merged",
  678. profs: []*Profile{testProfile1.Copy(), testProfile1Folded.Copy()},
  679. wantLocationLen: 7,
  680. },
  681. {
  682. name: "identical folded locations are merged",
  683. profs: []*Profile{testProfile1Folded.Copy(), testProfile1Folded.Copy()},
  684. wantLocationLen: 5,
  685. },
  686. } {
  687. t.Run(tc.name, func(t *testing.T) {
  688. prof, err := Merge(tc.profs)
  689. if err != nil {
  690. t.Fatalf("merge error: %v", err)
  691. }
  692. if got, want := len(prof.Location), tc.wantLocationLen; got != want {
  693. t.Fatalf("got %d locations, want %d locations", got, want)
  694. }
  695. })
  696. }
  697. }
  698. func TestNumLabelMerge(t *testing.T) {
  699. for _, tc := range []struct {
  700. name string
  701. profs []*Profile
  702. wantNumLabels []map[string][]int64
  703. wantNumUnits []map[string][]string
  704. }{
  705. {
  706. name: "different label units not merged",
  707. profs: []*Profile{testProfile4.Copy(), testProfile5.Copy()},
  708. wantNumLabels: []map[string][]int64{
  709. {
  710. "key1": {10},
  711. "key2": {30},
  712. },
  713. {
  714. "key1": {10},
  715. "key2": {30},
  716. },
  717. },
  718. wantNumUnits: []map[string][]string{
  719. {
  720. "key1": {"bytes"},
  721. "key2": {"bytes"},
  722. },
  723. {
  724. "key1": {"kilobytes"},
  725. "key2": {"kilobytes"},
  726. },
  727. },
  728. },
  729. } {
  730. t.Run(tc.name, func(t *testing.T) {
  731. prof, err := Merge(tc.profs)
  732. if err != nil {
  733. t.Errorf("merge error: %v", err)
  734. }
  735. if want, got := len(tc.wantNumLabels), len(prof.Sample); want != got {
  736. t.Fatalf("got %d samples, want %d samples", got, want)
  737. }
  738. for i, wantLabels := range tc.wantNumLabels {
  739. numLabels := prof.Sample[i].NumLabel
  740. if !reflect.DeepEqual(wantLabels, numLabels) {
  741. t.Errorf("got numeric labels %v, want %v", numLabels, wantLabels)
  742. }
  743. wantUnits := tc.wantNumUnits[i]
  744. numUnits := prof.Sample[i].NumUnit
  745. if !reflect.DeepEqual(wantUnits, numUnits) {
  746. t.Errorf("got numeric labels %v, want %v", numUnits, wantUnits)
  747. }
  748. }
  749. })
  750. }
  751. }
  752. func TestEmptyMappingMerge(t *testing.T) {
  753. // Aggregate a profile with itself and once again with a factor of
  754. // -2. Should end up with an empty profile (all samples for a
  755. // location should add up to 0).
  756. prof1 := testProfile1.Copy()
  757. prof2 := testProfile1NoMapping.Copy()
  758. p1, err := Merge([]*Profile{prof2, prof1})
  759. if err != nil {
  760. t.Errorf("merge error: %v", err)
  761. }
  762. prof2.Scale(-2)
  763. prof, err := Merge([]*Profile{p1, prof2})
  764. if err != nil {
  765. t.Errorf("merge error: %v", err)
  766. }
  767. // Use aggregation to merge locations at function granularity.
  768. if err := prof.Aggregate(false, true, false, false, false); err != nil {
  769. t.Errorf("aggregating after merge: %v", err)
  770. }
  771. samples := make(map[string]int64)
  772. for _, s := range prof.Sample {
  773. tb := locationHash(s)
  774. samples[tb] = samples[tb] + s.Value[0]
  775. }
  776. for s, v := range samples {
  777. if v != 0 {
  778. t.Errorf("nonzero value for sample %s: %d", s, v)
  779. }
  780. }
  781. }
  782. func TestNormalizeBySameProfile(t *testing.T) {
  783. pb := testProfile1.Copy()
  784. p := testProfile1.Copy()
  785. if err := p.Normalize(pb); err != nil {
  786. t.Fatal(err)
  787. }
  788. for i, s := range p.Sample {
  789. for j, v := range s.Value {
  790. expectedSampleValue := testProfile1.Sample[i].Value[j]
  791. if v != expectedSampleValue {
  792. t.Errorf("For sample %d, value %d want %d got %d", i, j, expectedSampleValue, v)
  793. }
  794. }
  795. }
  796. }
  797. func TestNormalizeByDifferentProfile(t *testing.T) {
  798. p := testProfile1.Copy()
  799. pb := testProfile2.Copy()
  800. if err := p.Normalize(pb); err != nil {
  801. t.Fatal(err)
  802. }
  803. expectedSampleValues := [][]int64{
  804. {19, 1000},
  805. {1, 100},
  806. {0, 10},
  807. {198, 10000},
  808. {0, 1},
  809. }
  810. for i, s := range p.Sample {
  811. for j, v := range s.Value {
  812. if v != expectedSampleValues[i][j] {
  813. t.Errorf("For sample %d, value %d want %d got %d", i, j, expectedSampleValues[i][j], v)
  814. }
  815. }
  816. }
  817. }
  818. func TestNormalizeByMultipleOfSameProfile(t *testing.T) {
  819. pb := testProfile1.Copy()
  820. for i, s := range pb.Sample {
  821. for j, v := range s.Value {
  822. pb.Sample[i].Value[j] = 10 * v
  823. }
  824. }
  825. p := testProfile1.Copy()
  826. err := p.Normalize(pb)
  827. if err != nil {
  828. t.Fatal(err)
  829. }
  830. for i, s := range p.Sample {
  831. for j, v := range s.Value {
  832. expectedSampleValue := 10 * testProfile1.Sample[i].Value[j]
  833. if v != expectedSampleValue {
  834. t.Errorf("For sample %d, value %d, want %d got %d", i, j, expectedSampleValue, v)
  835. }
  836. }
  837. }
  838. }
  839. func TestNormalizeIncompatibleProfiles(t *testing.T) {
  840. p := testProfile1.Copy()
  841. pb := testProfile3.Copy()
  842. if err := p.Normalize(pb); err == nil {
  843. t.Errorf("Expected an error")
  844. }
  845. }
  846. // locationHash constructs a string to use as a hashkey for a sample, based on its locations
  847. func locationHash(s *Sample) string {
  848. var tb string
  849. for _, l := range s.Location {
  850. for _, ln := range l.Line {
  851. tb = tb + fmt.Sprintf("%s:%d@%d ", ln.Function.Name, ln.Line, l.Address)
  852. }
  853. }
  854. return tb
  855. }
  856. func TestHasLabel(t *testing.T) {
  857. var testcases = []struct {
  858. desc string
  859. labels map[string][]string
  860. key string
  861. value string
  862. wantHasLabel bool
  863. }{
  864. {
  865. desc: "empty label does not have label",
  866. labels: map[string][]string{},
  867. key: "key",
  868. value: "value",
  869. wantHasLabel: false,
  870. },
  871. {
  872. desc: "label with one key and value has label",
  873. labels: map[string][]string{"key": {"value"}},
  874. key: "key",
  875. value: "value",
  876. wantHasLabel: true,
  877. },
  878. {
  879. desc: "label with one key and value does not have label",
  880. labels: map[string][]string{"key": {"value"}},
  881. key: "key1",
  882. value: "value1",
  883. wantHasLabel: false,
  884. },
  885. {
  886. desc: "label with many keys and values has label",
  887. labels: map[string][]string{
  888. "key1": {"value2", "value1"},
  889. "key2": {"value1", "value2", "value2"},
  890. "key3": {"value1", "value2", "value2"},
  891. },
  892. key: "key1",
  893. value: "value1",
  894. wantHasLabel: true,
  895. },
  896. {
  897. desc: "label with many keys and values does not have label",
  898. labels: map[string][]string{
  899. "key1": {"value2", "value1"},
  900. "key2": {"value1", "value2", "value2"},
  901. "key3": {"value1", "value2", "value2"},
  902. },
  903. key: "key5",
  904. value: "value5",
  905. wantHasLabel: false,
  906. },
  907. }
  908. for _, tc := range testcases {
  909. t.Run(tc.desc, func(t *testing.T) {
  910. sample := &Sample{
  911. Label: tc.labels,
  912. }
  913. if gotHasLabel := sample.HasLabel(tc.key, tc.value); gotHasLabel != tc.wantHasLabel {
  914. t.Errorf("sample.HasLabel(%q, %q) got %v, want %v", tc.key, tc.value, gotHasLabel, tc.wantHasLabel)
  915. }
  916. })
  917. }
  918. }
  919. func TestDiffBaseSample(t *testing.T) {
  920. var testcases = []struct {
  921. desc string
  922. labels map[string][]string
  923. wantDiffBaseSample bool
  924. }{
  925. {
  926. desc: "empty label does not have label",
  927. labels: map[string][]string{},
  928. wantDiffBaseSample: false,
  929. },
  930. {
  931. desc: "label with one key and value, including diff base label",
  932. labels: map[string][]string{"pprof::base": {"true"}},
  933. wantDiffBaseSample: true,
  934. },
  935. {
  936. desc: "label with one key and value, not including diff base label",
  937. labels: map[string][]string{"key": {"value"}},
  938. wantDiffBaseSample: false,
  939. },
  940. {
  941. desc: "label with many keys and values, including diff base label",
  942. labels: map[string][]string{
  943. "pprof::base": {"value2", "true"},
  944. "key2": {"true", "value2", "value2"},
  945. "key3": {"true", "value2", "value2"},
  946. },
  947. wantDiffBaseSample: true,
  948. },
  949. {
  950. desc: "label with many keys and values, not including diff base label",
  951. labels: map[string][]string{
  952. "key1": {"value2", "value1"},
  953. "key2": {"value1", "value2", "value2"},
  954. "key3": {"value1", "value2", "value2"},
  955. },
  956. wantDiffBaseSample: false,
  957. },
  958. }
  959. for _, tc := range testcases {
  960. t.Run(tc.desc, func(t *testing.T) {
  961. sample := &Sample{
  962. Label: tc.labels,
  963. }
  964. if gotHasLabel := sample.DiffBaseSample(); gotHasLabel != tc.wantDiffBaseSample {
  965. t.Errorf("sample.DiffBaseSample() got %v, want %v", gotHasLabel, tc.wantDiffBaseSample)
  966. }
  967. })
  968. }
  969. }
  970. func TestRemove(t *testing.T) {
  971. var testcases = []struct {
  972. desc string
  973. samples []*Sample
  974. removeKey string
  975. wantLabels []map[string][]string
  976. }{
  977. {
  978. desc: "some samples have label already",
  979. samples: []*Sample{
  980. {
  981. Location: []*Location{cpuL[0]},
  982. Value: []int64{1000},
  983. },
  984. {
  985. Location: []*Location{cpuL[0]},
  986. Value: []int64{1000},
  987. Label: map[string][]string{
  988. "key1": {"value1", "value2", "value3"},
  989. "key2": {"value1"},
  990. },
  991. },
  992. {
  993. Location: []*Location{cpuL[0]},
  994. Value: []int64{1000},
  995. Label: map[string][]string{
  996. "key1": {"value2"},
  997. },
  998. },
  999. },
  1000. removeKey: "key1",
  1001. wantLabels: []map[string][]string{
  1002. {},
  1003. {"key2": {"value1"}},
  1004. {},
  1005. },
  1006. },
  1007. }
  1008. for _, tc := range testcases {
  1009. t.Run(tc.desc, func(t *testing.T) {
  1010. profile := testProfile1.Copy()
  1011. profile.Sample = tc.samples
  1012. profile.RemoveLabel(tc.removeKey)
  1013. if got, want := len(profile.Sample), len(tc.wantLabels); got != want {
  1014. t.Fatalf("got %v samples, want %v samples", got, want)
  1015. }
  1016. for i, sample := range profile.Sample {
  1017. wantLabels := tc.wantLabels[i]
  1018. if got, want := len(sample.Label), len(wantLabels); got != want {
  1019. t.Errorf("got %v label keys for sample %v, want %v", got, i, want)
  1020. continue
  1021. }
  1022. for wantKey, wantValues := range wantLabels {
  1023. if gotValues, ok := sample.Label[wantKey]; ok {
  1024. if !reflect.DeepEqual(gotValues, wantValues) {
  1025. t.Errorf("for key %s, got values %v, want values %v", wantKey, gotValues, wantValues)
  1026. }
  1027. } else {
  1028. t.Errorf("for key %s got no values, want %v", wantKey, wantValues)
  1029. }
  1030. }
  1031. }
  1032. })
  1033. }
  1034. }
  1035. func TestSetLabel(t *testing.T) {
  1036. var testcases = []struct {
  1037. desc string
  1038. samples []*Sample
  1039. setKey string
  1040. setVal []string
  1041. wantLabels []map[string][]string
  1042. }{
  1043. {
  1044. desc: "some samples have label already",
  1045. samples: []*Sample{
  1046. {
  1047. Location: []*Location{cpuL[0]},
  1048. Value: []int64{1000},
  1049. },
  1050. {
  1051. Location: []*Location{cpuL[0]},
  1052. Value: []int64{1000},
  1053. Label: map[string][]string{
  1054. "key1": {"value1", "value2", "value3"},
  1055. "key2": {"value1"},
  1056. },
  1057. },
  1058. {
  1059. Location: []*Location{cpuL[0]},
  1060. Value: []int64{1000},
  1061. Label: map[string][]string{
  1062. "key1": {"value2"},
  1063. },
  1064. },
  1065. },
  1066. setKey: "key1",
  1067. setVal: []string{"value1"},
  1068. wantLabels: []map[string][]string{
  1069. {"key1": {"value1"}},
  1070. {"key1": {"value1"}, "key2": {"value1"}},
  1071. {"key1": {"value1"}},
  1072. },
  1073. },
  1074. {
  1075. desc: "no samples have labels",
  1076. samples: []*Sample{
  1077. {
  1078. Location: []*Location{cpuL[0]},
  1079. Value: []int64{1000},
  1080. },
  1081. },
  1082. setKey: "key1",
  1083. setVal: []string{"value1"},
  1084. wantLabels: []map[string][]string{
  1085. {"key1": {"value1"}},
  1086. },
  1087. },
  1088. {
  1089. desc: "all samples have some labels, but not key being added",
  1090. samples: []*Sample{
  1091. {
  1092. Location: []*Location{cpuL[0]},
  1093. Value: []int64{1000},
  1094. Label: map[string][]string{
  1095. "key2": {"value2"},
  1096. },
  1097. },
  1098. {
  1099. Location: []*Location{cpuL[0]},
  1100. Value: []int64{1000},
  1101. Label: map[string][]string{
  1102. "key3": {"value3"},
  1103. },
  1104. },
  1105. },
  1106. setKey: "key1",
  1107. setVal: []string{"value1"},
  1108. wantLabels: []map[string][]string{
  1109. {"key1": {"value1"}, "key2": {"value2"}},
  1110. {"key1": {"value1"}, "key3": {"value3"}},
  1111. },
  1112. },
  1113. {
  1114. desc: "all samples have key being added",
  1115. samples: []*Sample{
  1116. {
  1117. Location: []*Location{cpuL[0]},
  1118. Value: []int64{1000},
  1119. Label: map[string][]string{
  1120. "key1": {"value1"},
  1121. },
  1122. },
  1123. {
  1124. Location: []*Location{cpuL[0]},
  1125. Value: []int64{1000},
  1126. Label: map[string][]string{
  1127. "key1": {"value1"},
  1128. },
  1129. },
  1130. },
  1131. setKey: "key1",
  1132. setVal: []string{"value1"},
  1133. wantLabels: []map[string][]string{
  1134. {"key1": {"value1"}},
  1135. {"key1": {"value1"}},
  1136. },
  1137. },
  1138. }
  1139. for _, tc := range testcases {
  1140. t.Run(tc.desc, func(t *testing.T) {
  1141. profile := testProfile1.Copy()
  1142. profile.Sample = tc.samples
  1143. profile.SetLabel(tc.setKey, tc.setVal)
  1144. if got, want := len(profile.Sample), len(tc.wantLabels); got != want {
  1145. t.Fatalf("got %v samples, want %v samples", got, want)
  1146. }
  1147. for i, sample := range profile.Sample {
  1148. wantLabels := tc.wantLabels[i]
  1149. if got, want := len(sample.Label), len(wantLabels); got != want {
  1150. t.Errorf("got %v label keys for sample %v, want %v", got, i, want)
  1151. continue
  1152. }
  1153. for wantKey, wantValues := range wantLabels {
  1154. if gotValues, ok := sample.Label[wantKey]; ok {
  1155. if !reflect.DeepEqual(gotValues, wantValues) {
  1156. t.Errorf("for key %s, got values %v, want values %v", wantKey, gotValues, wantValues)
  1157. }
  1158. } else {
  1159. t.Errorf("for key %s got no values, want %v", wantKey, wantValues)
  1160. }
  1161. }
  1162. }
  1163. })
  1164. }
  1165. }
  1166. func TestNumLabelUnits(t *testing.T) {
  1167. var tagFilterTests = []struct {
  1168. desc string
  1169. tagVals []map[string][]int64
  1170. tagUnits []map[string][]string
  1171. wantUnits map[string]string
  1172. wantIgnoredUnits map[string][]string
  1173. }{
  1174. {
  1175. "One sample, multiple keys, different specified units",
  1176. []map[string][]int64{{"key1": {131072}, "key2": {128}}},
  1177. []map[string][]string{{"key1": {"bytes"}, "key2": {"kilobytes"}}},
  1178. map[string]string{"key1": "bytes", "key2": "kilobytes"},
  1179. map[string][]string{},
  1180. },
  1181. {
  1182. "One sample, one key with one value, unit specified",
  1183. []map[string][]int64{{"key1": {8}}},
  1184. []map[string][]string{{"key1": {"bytes"}}},
  1185. map[string]string{"key1": "bytes"},
  1186. map[string][]string{},
  1187. },
  1188. {
  1189. "One sample, one key with one value, empty unit specified",
  1190. []map[string][]int64{{"key1": {8}}},
  1191. []map[string][]string{{"key1": {""}}},
  1192. map[string]string{"key1": "key1"},
  1193. map[string][]string{},
  1194. },
  1195. {
  1196. "Key bytes, unit not specified",
  1197. []map[string][]int64{{"bytes": {8}}},
  1198. []map[string][]string{nil},
  1199. map[string]string{"bytes": "bytes"},
  1200. map[string][]string{},
  1201. },
  1202. {
  1203. "One sample, one key with one value, unit not specified",
  1204. []map[string][]int64{{"kilobytes": {8}}},
  1205. []map[string][]string{nil},
  1206. map[string]string{"kilobytes": "kilobytes"},
  1207. map[string][]string{},
  1208. },
  1209. {
  1210. "Key request, unit not specified",
  1211. []map[string][]int64{{"request": {8}}},
  1212. []map[string][]string{nil},
  1213. map[string]string{"request": "bytes"},
  1214. map[string][]string{},
  1215. },
  1216. {
  1217. "Key alignment, unit not specified",
  1218. []map[string][]int64{{"alignment": {8}}},
  1219. []map[string][]string{nil},
  1220. map[string]string{"alignment": "bytes"},
  1221. map[string][]string{},
  1222. },
  1223. {
  1224. "One sample, one key with multiple values and two different units",
  1225. []map[string][]int64{{"key1": {8, 8}}},
  1226. []map[string][]string{{"key1": {"bytes", "kilobytes"}}},
  1227. map[string]string{"key1": "bytes"},
  1228. map[string][]string{"key1": {"kilobytes"}},
  1229. },
  1230. {
  1231. "One sample, one key with multiple values and three different units",
  1232. []map[string][]int64{{"key1": {8, 8}}},
  1233. []map[string][]string{{"key1": {"bytes", "megabytes", "kilobytes"}}},
  1234. map[string]string{"key1": "bytes"},
  1235. map[string][]string{"key1": {"kilobytes", "megabytes"}},
  1236. },
  1237. {
  1238. "Two samples, one key, different units specified",
  1239. []map[string][]int64{{"key1": {8}}, {"key1": {8}}},
  1240. []map[string][]string{{"key1": {"bytes"}}, {"key1": {"kilobytes"}}},
  1241. map[string]string{"key1": "bytes"},
  1242. map[string][]string{"key1": {"kilobytes"}},
  1243. },
  1244. {
  1245. "Keys alignment, request, and bytes have units specified",
  1246. []map[string][]int64{{
  1247. "alignment": {8},
  1248. "request": {8},
  1249. "bytes": {8},
  1250. }},
  1251. []map[string][]string{{
  1252. "alignment": {"seconds"},
  1253. "request": {"minutes"},
  1254. "bytes": {"hours"},
  1255. }},
  1256. map[string]string{
  1257. "alignment": "seconds",
  1258. "request": "minutes",
  1259. "bytes": "hours",
  1260. },
  1261. map[string][]string{},
  1262. },
  1263. }
  1264. for _, test := range tagFilterTests {
  1265. p := &Profile{Sample: make([]*Sample, len(test.tagVals))}
  1266. for i, numLabel := range test.tagVals {
  1267. s := Sample{
  1268. NumLabel: numLabel,
  1269. NumUnit: test.tagUnits[i],
  1270. }
  1271. p.Sample[i] = &s
  1272. }
  1273. units, ignoredUnits := p.NumLabelUnits()
  1274. if !reflect.DeepEqual(test.wantUnits, units) {
  1275. t.Errorf("%s: got %v units, want %v", test.desc, units, test.wantUnits)
  1276. }
  1277. if !reflect.DeepEqual(test.wantIgnoredUnits, ignoredUnits) {
  1278. t.Errorf("%s: got %v ignored units, want %v", test.desc, ignoredUnits, test.wantIgnoredUnits)
  1279. }
  1280. }
  1281. }
  1282. func TestSetMain(t *testing.T) {
  1283. testProfile1.massageMappings()
  1284. if testProfile1.Mapping[0].File != mainBinary {
  1285. t.Errorf("got %s for main", testProfile1.Mapping[0].File)
  1286. }
  1287. }
  1288. // parallel runs n copies of fn in parallel.
  1289. func parallel(n int, fn func()) {
  1290. var wg sync.WaitGroup
  1291. wg.Add(n)
  1292. for i := 0; i < n; i++ {
  1293. go func() {
  1294. fn()
  1295. wg.Done()
  1296. }()
  1297. }
  1298. wg.Wait()
  1299. }
  1300. func TestThreadSafety(t *testing.T) {
  1301. src := testProfile1.Copy()
  1302. parallel(4, func() { src.Copy() })
  1303. parallel(4, func() {
  1304. var b bytes.Buffer
  1305. src.WriteUncompressed(&b)
  1306. })
  1307. parallel(4, func() {
  1308. var b bytes.Buffer
  1309. src.Write(&b)
  1310. })
  1311. }