<commit_before>// +build !production
// http://blog.golang.org/profiling-go-programs

package commands

import (
	"flag"
	"os"
	"runtime/pprof"
)

type memprofile struct {
	logger *Logger
	output string
	_f     *os.File
}

func memProfileCommandLine(cli *flag.FlagSet) CommandLineHandler {
	mp := &memprofile{
		logger: NewLogger("[ostent memprofile] "),
	}
	cli.StringVar(&mp.output, "memprofile", "", "MEM profile output file")
	return func() (AtexitHandler, bool, error) {
		if mp.output == "" {
			return nil, false, nil
		}
		return mp.atexit, false, mp.run()
	}
}

func (mp *memprofile) atexit() {
	mp.logger.Print("Writing MEM profile")
	if err := pprof.WriteHeapProfile(mp._f); err != nil {
		mp.logger.Print(err) // just print
	}
	if err := mp._f.Close(); err != nil {
		mp.logger.Print(err) // just print
	}
}

func (mp *memprofile) run() (err error) {
	mp._f, err = os.Create(mp.output)
	if err != nil {
		mp.logger.Fatal(err) // log with the mp logger
	}
	return err
}

/* ******************************************************************************** */

type cpuprofile struct {
	logger *Logger
	output string
	_f     *os.File
}

func cpuProfileCommandLine(cli *flag.FlagSet) CommandLineHandler {
	cp := &cpuprofile{<commit_msg>pprof profile (heap) with debug=1<commit_after>// +build !production
// http://blog.golang.org/profiling-go-programs

package commands

import (
	"flag"
	"os"
	"runtime/pprof"
)

type memprofile struct {
	logger *Logger
	output string
	_f     *os.File
}

func memProfileCommandLine(cli *flag.FlagSet) CommandLineHandler {
	mp := &memprofile{
		logger: NewLogger("[ostent memprofile] "),
	}
	cli.StringVar(&mp.output, "memprofile", "", "MEM profile output file")
	return func() (AtexitHandler, bool, error) {
		if mp.output == "" {
			return nil, false, nil
		}
		return mp.atexit, false, mp.run()
	}
}

func (mp *memprofile) atexit() {
	mp.logger.Print("Writing MEM profile")
	if err := pprof.Lookup("heap").WriteTo(mp._f, 1); err != nil {
		mp.logger.Print(err) // just print
	}
	if err := mp._f.Close(); err != nil {
		mp.logger.Print(err) // just print
	}
}

func (mp *memprofile) run() (err error) {
	mp._f, err = os.Create(mp.output)
	if err != nil {
		mp.logger.Fatal(err) // log with the mp logger
	}
	return err
}

/* ******************************************************************************** */

type cpuprofile struct {
	logger *Logger
	output string
	_f     *os.File
}

func cpuProfileCommandLine(cli *flag.FlagSet) CommandLineHandler {
	cp := &cpuprofile{
{'fraction_non_alphanumeric': 0.20858164481525626, 'fraction_numerical': 0.0007945967421533572, 'mean_word_length': 3.9469548133595285, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7358590', 'n_tokens_mistral': 915, 'n_tokens_neox': 878, 'n_words': 218}