No Description

sample.go 1.4KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. // Copyright 2017 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. // sample program that is used to produce some of the files in
  15. // pprof/internal/report/testdata.
  16. package main
  17. import (
  18. "flag"
  19. "fmt"
  20. "log"
  21. "math"
  22. "os"
  23. "runtime/pprof"
  24. )
  25. var cpuProfile = flag.String("cpuprofile", "", "where to write cpu profile")
  26. func main() {
  27. flag.Parse()
  28. f, err := os.Create(*cpuProfile)
  29. if err != nil {
  30. log.Fatal("could not create CPU profile: ", err)
  31. }
  32. if err := pprof.StartCPUProfile(f); err != nil {
  33. log.Fatal("could not start CPU profile: ", err)
  34. }
  35. defer pprof.StopCPUProfile()
  36. busyLoop()
  37. }
  38. func busyLoop() {
  39. m := make(map[int]int)
  40. for i := 0; i < 1000000; i++ {
  41. m[i] = i + 10
  42. }
  43. var sum float64
  44. for i := 0; i < 100; i++ {
  45. for _, v := range m {
  46. sum += math.Abs(float64(v))
  47. }
  48. }
  49. fmt.Println("Sum", sum)
  50. }