Profile Haskell without installing installing prof

2019-02-12 04:14发布

问题:

I wish to profile my program written in Haskell.

On compilation, I am told that I do not have profiling libraries for certain dependencies (e.g. criterion) installed and cabal aborts.

I have no interest in profiling parts of those dependencies; code called from main doesn't even use them.

How can I profile my application without installing profiling libraries I don't need and without removing all those dependencies?

回答1:

A good way to circumvent having to compile everything with profiling is to use cabal sandbox. It allows you to set up a sandbox for one application only, and thereby you won't have to re-install your entire ~/.cabal prefix. You'll need a recent version of Cabal, so run cabal update && cabal install cabal-install first.

Once you initialise a sandbox, create a file cabal.config to include the necessary directives (in your case library-profiling: True; executable-profiling: True may also be handy.)

A side-effect of this is that you can test your code with dependencies that need not be installed globally, for example, experimental versions, or outdated versions.

EDIT: btw, I don't think you need to have profiling enabled for criterion to work. In any case, it works for me without profiling being enabled. Just write a Main module that contains main = defaultMain benchmarks where benchmarks has type [Benchmark], i.e. a list of benchmarks that you've written.

You then compile that file (say, we call it benchmarks.hs with ghc --make -o bench benchmarks.hs, and run the program, ./bench with the appropriate arguments (consult the criterion documentation for details. A good default argument is, say ./bench -o benchmarks.html which will generate a nifty report similar to this one)



回答2:

I had the same problem this week, and although I had recompiled everything by hand, I was instructed in the IRC channel to do the following:

  1. Go to your cabal config file (in case you don't know where)
  2. Edit the line for enable library profiling (and while you are at it, enable documentation)
  3. Run Cabal Install World


回答3:

As mentioned in the question you refer to in your comment, a good way to solve this problem in the future is to enable profiling in the cabal configuration. This way all libraries are installed with profiling support. This might not be a satisfying solution but I guess many are opting for it.

If you are only interested in getting an impression of the memory usage of your program you can generate a heap profile of your program using -hT. More precisely, you have to compile the program with -rtsopts to enable RTS options then execute it using +RTS -hT. The compiler generates a file with the extension hp. You can convert the hp file into a postscript file with a heap profile using hp2ps. This should work without any profiling support, but note that I am to lazy to verify it as I have installed all libraries with profiling support ; )