shithub: hugo

Download patch

ref: 051fa343d06d6c070df742f7cbd125432fcab665
parent: 23f69efb3914946b39ce673fcc0f2e3a9ed9d878
author: Bjørn Erik Pedersen <[email protected]>
date: Sat Dec 2 06:44:18 EST 2017

Improve site benchmarks

--- a/hugolib/site_benchmark_test.go
+++ b/hugolib/site_benchmark_test.go
@@ -27,6 +27,7 @@
 type siteBuildingBenchmarkConfig struct {
 	Frontmatter  string
 	NumPages     int
+	NumLangs     int
 	RootSections int
 	Render       bool
 	Shortcodes   bool
@@ -39,6 +40,8 @@
 	// To make it a short as possible, we only shows bools when enabled and ints when >= 0 (RootSections > 1)
 	sep := ","
 	id := s.Frontmatter + sep
+	id += fmt.Sprintf("num_langs=%d%s", s.NumLangs, sep)
+
 	if s.RootSections > 1 {
 		id += fmt.Sprintf("num_root_sections=%d%s", s.RootSections, sep)
 	}
@@ -61,9 +64,10 @@
 	}
 
 	return strings.TrimSuffix(id, sep)
-
 }
 
+var someLangs = []string{"en", "fr", "nn"}
+
 func BenchmarkSiteBuilding(b *testing.B) {
 	var (
 		// The below represents the full matrix of benchmarks. Big!
@@ -74,6 +78,7 @@
 		allNumPages        = []int{1, 10, 100, 500, 1000, 5000, 10000}
 		allDoRender        = []bool{false, true}
 		allDoShortCodes    = []bool{false, true}
+		allNumLangs        = []int{1, 3}
 	)
 
 	var runDefault bool
@@ -99,21 +104,24 @@
 	}
 
 	var conf siteBuildingBenchmarkConfig
-	for _, frontmatter := range allFrontmatters {
-		conf.Frontmatter = frontmatter
-		for _, rootSections := range allNumRootSections {
-			conf.RootSections = rootSections
-			for _, numTags := range allNumTags {
-				conf.NumTags = numTags
-				for _, tagsPerPage := range allTagsPerPage {
-					conf.TagsPerPage = tagsPerPage
-					for _, numPages := range allNumPages {
-						conf.NumPages = numPages
-						for _, render := range allDoRender {
-							conf.Render = render
-							for _, shortcodes := range allDoShortCodes {
-								conf.Shortcodes = shortcodes
-								doBenchMarkSiteBuilding(conf, b)
+	for _, numLangs := range allNumLangs {
+		conf.NumLangs = numLangs
+		for _, frontmatter := range allFrontmatters {
+			conf.Frontmatter = frontmatter
+			for _, rootSections := range allNumRootSections {
+				conf.RootSections = rootSections
+				for _, numTags := range allNumTags {
+					conf.NumTags = numTags
+					for _, tagsPerPage := range allTagsPerPage {
+						conf.TagsPerPage = tagsPerPage
+						for _, numPages := range allNumPages {
+							conf.NumPages = numPages
+							for _, render := range allDoRender {
+								conf.Render = render
+								for _, shortcodes := range allDoShortCodes {
+									conf.Shortcodes = shortcodes
+									doBenchMarkSiteBuilding(conf, b)
+								}
 							}
 						}
 					}
@@ -199,11 +207,29 @@
 paginate = 10
 defaultContentLanguage = "en"
 
+[languages]
+%s
+
 [Taxonomies]
 tag = "tags"
 category = "categories"
 `
 
+	langConfigTemplate := `
+[languages.%s]
+languageName = "Lang %s"
+weight = %d
+`
+
+	langConfig := ""
+
+	for i := 0; i < cfg.NumLangs; i++ {
+		langCode := someLangs[i]
+		langConfig += fmt.Sprintf(langConfigTemplate, langCode, langCode, i+1)
+	}
+
+	siteConfig = fmt.Sprintf(siteConfig, langConfig)
+
 	numTags := cfg.NumTags
 
 	if cfg.TagsPerPage > numTags {
@@ -248,37 +274,49 @@
 
 		fs := th.Fs
 
-		pagesPerSection := cfg.NumPages / cfg.RootSections
+		pagesPerSection := cfg.NumPages / cfg.RootSections / cfg.NumLangs
+		for li := 0; li < cfg.NumLangs; li++ {
+			fileLangCodeID := ""
+			if li > 0 {
+				fileLangCodeID = "." + someLangs[li] + "."
+			}
 
-		for i := 0; i < cfg.RootSections; i++ {
-			for j := 0; j < pagesPerSection; j++ {
-				var tagsSlice []string
+			for i := 0; i < cfg.RootSections; i++ {
+				for j := 0; j < pagesPerSection; j++ {
+					var tagsSlice []string
 
-				if numTags > 0 {
-					tagsStart := rand.Intn(numTags) - cfg.TagsPerPage
-					if tagsStart < 0 {
-						tagsStart = 0
+					if numTags > 0 {
+						tagsStart := rand.Intn(numTags) - cfg.TagsPerPage
+						if tagsStart < 0 {
+							tagsStart = 0
+						}
+						tagsSlice = tags[tagsStart : tagsStart+cfg.TagsPerPage]
 					}
-					tagsSlice = tags[tagsStart : tagsStart+cfg.TagsPerPage]
-				}
 
-				if cfg.Frontmatter == "TOML" {
-					pageTemplate = pageTemplateTOML
-					tagsStr = "[]"
-					if cfg.TagsPerPage > 0 {
-						tagsStr = strings.Replace(fmt.Sprintf("%q", tagsSlice), " ", ", ", -1)
+					if cfg.Frontmatter == "TOML" {
+						pageTemplate = pageTemplateTOML
+						tagsStr = "[]"
+						if cfg.TagsPerPage > 0 {
+							tagsStr = strings.Replace(fmt.Sprintf("%q", tagsSlice), " ", ", ", -1)
+						}
+					} else {
+						// YAML
+						pageTemplate = pageTemplateYAML
+						for _, tag := range tagsSlice {
+							tagsStr += "\n- " + tag
+						}
 					}
-				} else {
-					// YAML
-					pageTemplate = pageTemplateYAML
-					for _, tag := range tagsSlice {
-						tagsStr += "\n- " + tag
-					}
-				}
 
-				content := fmt.Sprintf(pageTemplate, fmt.Sprintf("Title%d_%d", i, j), tagsStr, contentPagesContent[rand.Intn(3)])
+					content := fmt.Sprintf(pageTemplate, fmt.Sprintf("Title%d_%d", i, j), tagsStr, contentPagesContent[rand.Intn(3)])
 
-				writeSource(b, fs, filepath.Join("content", fmt.Sprintf("sect%d", i), fmt.Sprintf("page%d.md", j)), content)
+					contentFilename := fmt.Sprintf("page%d%s.md", j, fileLangCodeID)
+
+					writeSource(b, fs, filepath.Join("content", fmt.Sprintf("sect%d", i), contentFilename), content)
+				}
+
+				content := fmt.Sprintf(pageTemplate, fmt.Sprintf("Section %d", i), "[]", contentPagesContent[rand.Intn(3)])
+				indexContentFilename := fmt.Sprintf("_index%s.md", fileLangCodeID)
+				writeSource(b, fs, filepath.Join("content", fmt.Sprintf("sect%d", i), indexContentFilename), content)
 			}
 		}