2017-06-28 11:01:36 +00:00
|
|
|
package enry
|
|
|
|
|
|
|
|
import (
|
|
|
|
"flag"
|
2018-12-26 20:21:39 +00:00
|
|
|
"fmt"
|
2017-06-28 11:01:36 +00:00
|
|
|
"io/ioutil"
|
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"testing"
|
2017-07-18 11:29:46 +00:00
|
|
|
)
|
2017-06-28 11:01:36 +00:00
|
|
|
|
|
|
|
type sample struct {
|
|
|
|
filename string
|
|
|
|
content []byte
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
slow bool
|
|
|
|
overcomeLanguage string
|
|
|
|
overcomeLanguages []string
|
|
|
|
samples []*sample
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestMain(m *testing.M) {
|
|
|
|
flag.BoolVar(&slow, "slow", false, "run benchmarks per sample for strategies too")
|
|
|
|
flag.Parse()
|
2017-07-18 11:29:46 +00:00
|
|
|
|
2022-11-23 18:06:24 +00:00
|
|
|
tmpLinguistDir, cleanupNeeded, err := maybeCloneLinguist()
|
|
|
|
if err != nil {
|
2017-07-18 11:29:46 +00:00
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2022-11-23 18:06:24 +00:00
|
|
|
if cleanupNeeded {
|
|
|
|
defer os.RemoveAll(tmpLinguistDir)
|
2017-10-26 13:37:18 +00:00
|
|
|
}
|
2017-07-18 11:29:46 +00:00
|
|
|
|
2022-11-23 18:06:24 +00:00
|
|
|
samplesDir := filepath.Join(tmpLinguistDir, "samples")
|
2017-06-28 11:01:36 +00:00
|
|
|
samples, err = getSamples(samplesDir)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2019-02-14 11:47:45 +00:00
|
|
|
os.Exit(m.Run())
|
2017-07-18 11:29:46 +00:00
|
|
|
}
|
|
|
|
|
2017-06-28 11:01:36 +00:00
|
|
|
func getSamples(dir string) ([]*sample, error) {
|
|
|
|
samples := make([]*sample, 0, 2000)
|
|
|
|
err := filepath.Walk(dir, func(path string, info os.FileInfo, err error) error {
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if info.IsDir() {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
content, err := ioutil.ReadFile(path)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
s := &sample{
|
|
|
|
filename: path,
|
|
|
|
content: content,
|
|
|
|
}
|
|
|
|
samples = append(samples, s)
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
return samples, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkGetLanguageTotal(b *testing.B) {
|
|
|
|
if slow {
|
|
|
|
b.SkipNow()
|
|
|
|
}
|
|
|
|
|
|
|
|
var o string
|
|
|
|
b.Run("GetLanguage()_TOTAL", func(b *testing.B) {
|
|
|
|
for n := 0; n < b.N; n++ {
|
|
|
|
for _, sample := range samples {
|
|
|
|
o = GetLanguage(sample.filename, sample.content)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
overcomeLanguage = o
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkClassifyTotal(b *testing.B) {
|
|
|
|
if slow {
|
|
|
|
b.SkipNow()
|
|
|
|
}
|
|
|
|
|
|
|
|
var o []string
|
|
|
|
b.Run("Classify()_TOTAL", func(b *testing.B) {
|
|
|
|
for n := 0; n < b.N; n++ {
|
|
|
|
for _, sample := range samples {
|
2019-10-29 16:56:13 +00:00
|
|
|
o = defaultClassifier.classify(sample.content, nil)
|
2017-06-28 11:01:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
overcomeLanguages = o
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkStrategiesTotal(b *testing.B) {
|
|
|
|
if slow {
|
|
|
|
b.SkipNow()
|
|
|
|
}
|
|
|
|
|
2018-12-26 20:21:39 +00:00
|
|
|
benchmarks := benchmarkForAllStrategies("TOTAL")
|
2017-06-28 11:01:36 +00:00
|
|
|
|
|
|
|
var o []string
|
|
|
|
for _, benchmark := range benchmarks {
|
|
|
|
b.Run(benchmark.name, func(b *testing.B) {
|
|
|
|
for n := 0; n < b.N; n++ {
|
|
|
|
for _, sample := range samples {
|
|
|
|
o = benchmark.strategy(sample.filename, sample.content, benchmark.candidates)
|
|
|
|
}
|
|
|
|
|
|
|
|
overcomeLanguages = o
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkGetLanguagePerSample(b *testing.B) {
|
|
|
|
if !slow {
|
|
|
|
b.SkipNow()
|
|
|
|
}
|
|
|
|
|
|
|
|
var o string
|
|
|
|
for _, sample := range samples {
|
|
|
|
b.Run("GetLanguage()_SAMPLE_"+sample.filename, func(b *testing.B) {
|
|
|
|
for n := 0; n < b.N; n++ {
|
|
|
|
o = GetLanguage(sample.filename, sample.content)
|
|
|
|
}
|
|
|
|
|
|
|
|
overcomeLanguage = o
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkClassifyPerSample(b *testing.B) {
|
|
|
|
if !slow {
|
|
|
|
b.SkipNow()
|
|
|
|
}
|
|
|
|
|
|
|
|
var o []string
|
|
|
|
for _, sample := range samples {
|
|
|
|
b.Run("Classify()_SAMPLE_"+sample.filename, func(b *testing.B) {
|
|
|
|
for n := 0; n < b.N; n++ {
|
2019-10-29 16:56:13 +00:00
|
|
|
o = defaultClassifier.classify(sample.content, nil)
|
2017-06-28 11:01:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
overcomeLanguages = o
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkStrategiesPerSample(b *testing.B) {
|
|
|
|
if !slow {
|
|
|
|
b.SkipNow()
|
|
|
|
}
|
|
|
|
|
2018-12-26 20:21:39 +00:00
|
|
|
benchmarks := benchmarkForAllStrategies("SAMPLE")
|
2017-06-28 11:01:36 +00:00
|
|
|
|
|
|
|
var o []string
|
|
|
|
for _, benchmark := range benchmarks {
|
|
|
|
for _, sample := range samples {
|
|
|
|
b.Run(benchmark.name+sample.filename, func(b *testing.B) {
|
|
|
|
for n := 0; n < b.N; n++ {
|
|
|
|
o = benchmark.strategy(sample.filename, sample.content, benchmark.candidates)
|
|
|
|
}
|
|
|
|
|
|
|
|
overcomeLanguages = o
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-12-26 20:21:39 +00:00
|
|
|
|
|
|
|
type strategyName struct {
|
|
|
|
name string
|
|
|
|
strategy Strategy
|
|
|
|
candidates []string
|
|
|
|
}
|
|
|
|
|
|
|
|
func benchmarkForAllStrategies(class string) []strategyName {
|
|
|
|
return []strategyName{
|
|
|
|
{name: fmt.Sprintf("GetLanguagesByModeline()_%s_", class), strategy: GetLanguagesByModeline},
|
|
|
|
{name: fmt.Sprintf("GetLanguagesByFilename()_%s_", class), strategy: GetLanguagesByFilename},
|
|
|
|
{name: fmt.Sprintf("GetLanguagesByShebang()_%s_", class), strategy: GetLanguagesByShebang},
|
|
|
|
{name: fmt.Sprintf("GetLanguagesByExtension()_%s_", class), strategy: GetLanguagesByExtension},
|
|
|
|
{name: fmt.Sprintf("GetLanguagesByContent()_%s_", class), strategy: GetLanguagesByContent},
|
|
|
|
}
|
|
|
|
}
|