Brak opisu

profile_test.go 34KB

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