<commit_before>package perf

import (
	"fmt"
	"io"
	"net/http"
	"sort"
	"sync"
	"text/tabwriter"
)

var (
	mu     sync.RWMutex
	events = map[string]*Event{}
)

func init() {
	http.HandleFunc("/debug/perf", func(w http.ResponseWriter, r *http.Request) {
		w.Header().Set("Content-Type", "text/plain; charset=UTF-8")
		WriteEventsTable(w)
	})
}

func WriteEventsTable(w io.Writer) {
	tw := tabwriter.NewWriter(w, 0, 0, 2, ' ', 0)
	fmt.Fprint(tw, "description\tcount\tmin\tmean\tmax\n")
	type t struct {
		d string
		e *Event
	}
	mu.RLock()
	es := make([]t, 0, len(events))
	for d, e := range events {
		e.Mu.RLock()
		es = append(es, t{d, e})
		e.Mu.RUnlock()
	}
	mu.RUnlock()
	sort.Slice(es, func(i, j int) bool {
		return es[i].e.MeanTime() < es[j].e.MeanTime()
	})
	for _, el := range es {
		e := el.e
		fmt.Fprintf(tw, "%s\t%d\t%s\t%s\t%s\n", el.d, e.Count, e.Min, e.MeanTime(), e.Max)
	}
	tw.Flush()
}<commit_msg>perf.WriteEventsTable: Copy events and include totals<commit_after>package perf

import (
	"fmt"
	"io"
	"net/http"
	"sort"
	"sync"
	"text/tabwriter"
)

var (
	mu     sync.RWMutex
	events = map[string]*Event{}
)

func init() {
	http.HandleFunc("/debug/perf", func(w http.ResponseWriter, r *http.Request) {
		w.Header().Set("Content-Type", "text/plain; charset=UTF-8")
		WriteEventsTable(w)
	})
}

func WriteEventsTable(w io.Writer) {
	tw := tabwriter.NewWriter(w, 0, 0, 2, ' ', 0)
	fmt.Fprint(tw, "description\ttotal\tcount\tmin\tmean\tmax\n")
	type t struct {
		d string
		e Event
	}
	mu.RLock()
	es := make([]t, 0, len(events))
	for d, e := range events {
		e.Mu.RLock()
		es = append(es, t{d, *e})
		e.Mu.RUnlock()
	}
	mu.RUnlock()
	sort.Slice(es, func(i, j int) bool {
		return es[i].e.MeanTime() < es[j].e.MeanTime()
	})
	for _, el := range es {
		e := el.e
		fmt.Fprintf(tw, "%s\t%v\t%v\t%v\t%v\t%v\n", el.d, e.Total, e.Count, e.Min, e.MeanTime(), e.Max)
	}
	tw.Flush()
}
{'fraction_non_alphanumeric': 0.2087737843551797, 'fraction_numerical': 0.006342494714587738, 'mean_word_length': 3.4541176470588235, 'pattern_counts': {'":': 0, '<': 5, '<?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': '7410960', 'n_tokens_mistral': 893, 'n_tokens_neox': 857, 'n_words': 170}