Repository navigation
Bound the skills list width search #8140
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
402ec08
2df5cf8
aa6b7d3
fb7d076
c1365d3
b2a2be6
055acad
79c67cd
4530158
b61b62d
2906664
ddbdb00
41dfd3d
417ae32
c75c37c
fcd2902
497ff28
53c8155
4844df8
b262160
c3c22dd
6729933
3ac93e6
b10ce6a
c4a5e80
ca310ed
047b7e2
5dd7276
126f116
50bad47
fe1d82b
2208734
607d861
0416fe8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,6 +4,7 @@ import ( | |
| "bytes" | ||
| "context" | ||
| "fmt" | ||
| "math/rand/v2" | ||
| "strings" | ||
| "testing" | ||
| "time" | ||
|
|
@@ -152,6 +153,109 @@ func TestRenderTextKeepsWidestFittingWidth(t *testing.T) { | |
| assert.NotContains(t, out, "databricks skills search") | ||
| } | ||
|
|
||
| // renderTextScan is a brute-force reference for the width search: it tries | ||
| // every width from the widest down and keeps the first that fits. It shares | ||
| // oneLineSkills and layoutText with renderText, so it checks the search only. | ||
| func renderTextScan(rules string, list []libskills.Skill) string { | ||
| if len(list) == 0 { | ||
| return withRules(rules) + "No skills found.\n" | ||
| } | ||
| list = oneLineSkills(list) | ||
| names := layoutText(rules, list, 0) | ||
| if len(names)-len(searchHint)-1 > textBudget { | ||
| return names | ||
| } | ||
| maxLen := 0 | ||
| for _, s := range list { | ||
| maxLen = max(maxLen, len(s.Description)) | ||
| } | ||
| for w := min(maxLen, textBudget); w > 0; w-- { | ||
| if out := layoutText(rules, list, w); len(out) <= textBudget { | ||
| return out | ||
| } | ||
| } | ||
| return names | ||
| } | ||
|
|
||
| // randomCatalog returns a catalog whose sizes, names, descriptions and rules | ||
| // vary with the random source, so the width search meets every case. | ||
| func randomCatalog(r *rand.Rand) (string, []libskills.Skill) { | ||
| words := []string{"a", "deploy", "the", "workspace", "x", "notebook-run", "describes", "it.", "averyveryverylongwordthatkeepsgoing", " "} | ||
| var rules string | ||
| if r.IntN(2) == 0 { | ||
| rules = strings.Repeat("Pick the skill whose description matches. ", 1+r.IntN(3)) | ||
| } | ||
| // Some catalogs have names long enough that the names alone exceed the budget. | ||
| nameLen := 70 | ||
| if r.IntN(6) == 0 { | ||
| nameLen = 200 | ||
| } | ||
| var list []libskills.Skill | ||
| for i := range r.IntN(60) { | ||
| var desc []string | ||
| for range r.IntN(40) { | ||
| desc = append(desc, words[r.IntN(len(words))]) | ||
| } | ||
| sep := " " | ||
| if r.IntN(10) == 0 { | ||
| sep = "\n" | ||
| } | ||
| text := strings.Join(desc, sep) | ||
| if r.IntN(8) == 0 { | ||
| // An unbreakable description has no word boundary to cut at. | ||
| text = strings.Repeat("x", r.IntN(300)) | ||
| } | ||
| list = append(list, libskills.Skill{ | ||
| Name: fmt.Sprintf("skill-%d-%s", i, strings.Repeat("n", r.IntN(nameLen))), | ||
| Description: text, | ||
| }) | ||
| } | ||
| return rules, list | ||
| } | ||
|
|
||
| func TestRenderTextMatchesWidthScan(t *testing.T) { | ||
| for seed := range uint64(300) { | ||
| r := rand.New(rand.NewPCG(seed, 1)) | ||
| rules, list := randomCatalog(r) | ||
| want := renderTextScan(rules, list) | ||
| got := renderText(rules, list) | ||
| if !assert.Equal(t, want, got, "seed %d", seed) { | ||
| return | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // fourHundredLongSkills returns 400 skills whose descriptions are each far | ||
| // longer than the text budget. | ||
| func fourHundredLongSkills() []libskills.Skill { | ||
| var list []libskills.Skill | ||
| for i := range 400 { | ||
| desc := strings.Repeat(fmt.Sprintf("word%d describes the skill in detail. ", i), 40) | ||
| list = append(list, libskills.Skill{Name: fmt.Sprintf("s%03d", i), Description: desc}) | ||
| } | ||
| return list | ||
| } | ||
|
|
||
| func TestRenderTextFourHundredLongDescriptionsFitsBudget(t *testing.T) { | ||
| out := renderText("Pick the skill whose description matches the task.", fourHundredLongSkills()) | ||
| assert.LessOrEqual(t, len(out), textBudget) | ||
| assert.Contains(t, out, "s399") | ||
| // Names alone would also fit, so check that descriptions are kept. | ||
| assert.Contains(t, out, "s000: word0") | ||
| } | ||
|
|
||
| // BenchmarkRenderTextFourHundredLongDescriptions measures the width search on | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Changed in 607d861. The benchmark comment no longer cites the 50 ms figure. It now says the full width scan takes seconds on the same catalog. |
||
| // a 400-skill catalog with long descriptions. The time per op is the figure to | ||
| // watch; the full width scan takes seconds on the same catalog. | ||
| func BenchmarkRenderTextFourHundredLongDescriptions(b *testing.B) { | ||
| list := fourHundredLongSkills() | ||
| const rules = "Pick the skill whose description matches the task." | ||
| b.ReportAllocs() | ||
| for b.Loop() { | ||
| renderText(rules, list) | ||
| } | ||
| } | ||
|
|
||
| func TestRenderTextNoSearchHintWithoutDescriptions(t *testing.T) { | ||
| list := []libskills.Skill{{Name: "alpha"}, {Name: "beta"}} | ||
| assert.Equal(t, "alpha\nbeta\n"+readHint+"\n", renderText("", list)) | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[nice to have]This test passes even ifrenderTextfalls back to names only. The names-only layout of this catalog is also undertextBudgetand also containss399. Today the output keeps about 100 descriptions. Comparing againstrenderTextScan(rules, list), or asserting that some description text such as"s000: word0"is present, would make it catch that regression.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Changed in 607d861. The test now also asserts
s000: word0is in the output, so a fall back to names only fails it. I did not compare againstrenderTextScanbecause the full scan takes seconds on this catalog.