<commit_before>package app

import (
	"flag"
	"net/http"
	"net/url"
	"os"
	"path/filepath"
	"testing"

	"github.com/weaveworks/scope/render"
	"github.com/weaveworks/scope/render/detailed"
	"github.com/weaveworks/scope/report"
	"github.com/weaveworks/scope/test/fixture"
)

var (
	benchReportPath = flag.String("bench-report-path", "", "report file, or dir with files, to use for benchmarking (relative to this package)")
)

func readReportFiles(b *testing.B, path string) []report.Report {
	reports := []report.Report{}
	if err := filepath.Walk(path,
		func(p string, info os.FileInfo, err error) error {
			if err != nil {
				return err
			}
			if info.IsDir() {
				return nil
			}
			rpt, err := report.MakeFromFile(p)
			if err != nil {
				return err
			}
			reports = append(reports, rpt)
			return nil
		}); err != nil {
		b.Fatal(err)
	}
	return reports
}

func BenchmarkReportUnmarshal(b *testing.B) {
	b.ResetTimer()
	for i := 0; i < b.N; i++ {
		readReportFiles(b, *benchReportPath)
	}
}

func upgradeReports(reports []report.Report) []report.Report {
	upgraded := make([]report.Report, len(reports))
	for i, r := range reports {
		upgraded[i] = r.Upgrade()
	}
	return upgraded
}

func BenchmarkReportUpgrade(b *testing.B) {
	reports := readReportFiles(b, *benchReportPath)
	b.ResetTimer()
	for i := 0; i < b.N; i++ {
		upgradeReports(reports)
	}
}

func BenchmarkReportMerge(b *testing.B) {
	reports := upgradeReports(readReportFiles(b, *benchReportPath))
	merger := NewFastMerger()
	b.ResetTimer()
	for i := 0; i < b.N; i++ {<commit_msg>Shuffle reports before merging, in benchmark<commit_after>package app

import (
	"flag"
	"math/rand"
	"net/http"
	"net/url"
	"os"
	"path/filepath"
	"testing"

	"github.com/weaveworks/scope/render"
	"github.com/weaveworks/scope/render/detailed"
	"github.com/weaveworks/scope/report"
	"github.com/weaveworks/scope/test/fixture"
)

var (
	benchReportPath = flag.String("bench-report-path", "", "report file, or dir with files, to use for benchmarking (relative to this package)")
)

func readReportFiles(b *testing.B, path string) []report.Report {
	reports := []report.Report{}
	if err := filepath.Walk(path,
		func(p string, info os.FileInfo, err error) error {
			if err != nil {
				return err
			}
			if info.IsDir() {
				return nil
			}
			rpt, err := report.MakeFromFile(p)
			if err != nil {
				return err
			}
			reports = append(reports, rpt)
			return nil
		}); err != nil {
		b.Fatal(err)
	}
	return reports
}

func BenchmarkReportUnmarshal(b *testing.B) {
	b.ResetTimer()
	for i := 0; i < b.N; i++ {
		readReportFiles(b, *benchReportPath)
	}
}

func upgradeReports(reports []report.Report) []report.Report {
	upgraded := make([]report.Report, len(reports))
	for i, r := range reports {
		upgraded[i] = r.Upgrade()
	}
	return upgraded
}

func BenchmarkReportUpgrade(b *testing.B) {
	reports := readReportFiles(b, *benchReportPath)
	b.ResetTimer()
	for i := 0; i < b.N; i++ {
		upgradeReports(reports)
	}
}

func BenchmarkReportMerge(b *testing.B) {
	reports := upgradeReports(readReportFiles(b, *benchReportPath))
	rand.Shuffle(len(reports), func(i, j int) {
		reports[i], reports[j] = reports[j], reports[i]
	})
	merger := NewFastMerger()
	b.ResetTimer()
	for i := 0; i < b.N; i++ {
{'fraction_non_alphanumeric': 0.15408320493066255, 'fraction_numerical': 0.0018489984591679508, 'mean_word_length': 3.9406392694063928, 'pattern_counts': {'":': 0, '<': 9, '<?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': '4600817', 'n_tokens_mistral': 1319, 'n_tokens_neox': 1207, 'n_words': 278}