Skip to content

Instantly share code, notes, and snippets.

@samuell
Created August 8, 2013 10:21
Show Gist options
  • Select an option

  • Save samuell/6183503 to your computer and use it in GitHub Desktop.

Select an option

Save samuell/6183503 to your computer and use it in GitHub Desktop.
pprof not working
package main
import (
"github.com/samuell/blow"
"github.com/trustmaster/goflow"
"runtime"
"runtime/pprof"
"flag"
"os"
"log"
)
const (
NUMTHREADS = 4
)
// ---------------------------------------------------------
// BaseComplementer network
// ---------------------------------------------------------
type BaseComplementerApp struct {
flow.Graph
}
func NewBaseCompelenterApp() *BaseComplementerApp {
network := new(BaseComplementerApp)
network.InitGraphState()
// Add components
network.Add(new(blow.FileReader), "filereader")
network.Add(new(blow.BaseComplementer), "bc1")
network.Add(new(blow.BaseComplementer), "bc2")
network.Add(new(blow.BaseComplementer), "bc3")
network.Add(new(blow.BaseComplementer), "bc4")
network.Add(new(blow.Printer), "printer")
// Connect components
network.MapInPort("In", "filereader", "FileName")
network.Connect("filereader", "Line", "bc1", "Sequence", make(chan []byte))
network.Connect("bc1", "BaseComplementedSequence", "bc2", "Sequence", make(chan []byte))
network.Connect("bc2", "BaseComplementedSequence", "bc3", "Sequence", make(chan []byte))
network.Connect("bc3", "BaseComplementedSequence", "bc4", "Sequence", make(chan []byte))
network.Connect("bc4", "BaseComplementedSequence", "printer", "Line", make(chan []byte))
return network
}
// ---------------------------------------------------------
// Main method
// ---------------------------------------------------------
var finish chan bool
// Use this handler to let main() know when the network terminates
func (a *BaseComplementerApp) Finish() {
finish <- true
}
// Profiling stuff ... from http://blog.golang.org/profiling-go-programs
var cpuprofile = flag.String("cpuprofile", "", "write cpu profile to file")
func main() {
flag.Parse()
if *cpuprofile != "" {
f, err := os.Create(*cpuprofile)
if err != nil {
log.Fatal(err)
}
pprof.StartCPUProfile(f)
defer pprof.StopCPUProfile()
}
// Set the number of Operating System-threads to use
runtime.GOMAXPROCS(NUMTHREADS)
// Termination signal channel
finish = make(chan bool)
// Create network
net := NewBaseCompelenterApp()
// Create the "In" channel
in := make(chan string)
net.SetInPort("In", in)
// Run net
flow.RunNet(net)
// Give the Filename to read
in <- "Homo_sapiens.GRCh37.67.dna_rm.chromosome.Y.fa"
close(in)
<-finish
}
@samuell

samuell commented Aug 8, 2013

Copy link
Copy Markdown
Author

Works now, when putting pprof.StopCPUProfile() at end of main(), without defer ... like so:

func main() {
    flag.Parse()
    if *cpuprofile != "" {
        f, err := os.Create(*cpuprofile)
        if err != nil {
            fmt.Println("Error: ", err)
        }
        cpuerr := pprof.StartCPUProfile(f)
        if cpuerr != nil {
            fmt.Println("Start CPU Profile error: ", cpuerr)
        }
    }

    // Set the number of Operating System-threads to use
    runtime.GOMAXPROCS(NUMTHREADS)

    // Termination signal channel
    finish = make(chan bool)
    // Create network
    net := NewBaseCompelenterApp()

    // Create the "In" channel
    in := make(chan string)
    net.SetInPort("In", in)

    // Run net
    flow.RunNet(net)

    // Give the Filename to read
    in <- "Homo_sapiens.GRCh37.67.dna_rm.chromosome.Y.fa"

    close(in)
    <-finish
    pprof.StopCPUProfile()
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment