Repository navigation
conformance: add a corpus compile report #3
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,151 @@ | ||
| // Corpus compile report. | ||
| // | ||
| // The corpus tests assert outcomes; this report measures them. It answers | ||
| // three questions for every collection: how many queries parse to the IR, how | ||
| // many of those the IR translates to each back-end, and how many translate | ||
| // end-to-end. Run it with | ||
| // | ||
| // make corpus-report | ||
| // | ||
| // and paste the printed table into README.md's "Conformance report" section. | ||
| package conformance | ||
|
|
||
| import ( | ||
| "flag" | ||
| "fmt" | ||
| "path/filepath" | ||
| "strings" | ||
| "testing" | ||
|
|
||
| "github.com/be-thomas/openspl/pkg/ast" | ||
| "github.com/be-thomas/openspl/pkg/compiler" | ||
| ) | ||
|
|
||
| var corpusReport = flag.Bool("corpus-report", false, | ||
| "print the corpus compile report") | ||
|
|
||
| // reportTally counts one row of the report. | ||
| type reportTally struct { | ||
| queries int | ||
| parsed int | ||
| esql int | ||
| promql int | ||
| } | ||
|
|
||
| func (r *reportTally) add(other reportTally) { | ||
| r.queries += other.queries | ||
| r.parsed += other.parsed | ||
| r.esql += other.esql | ||
| r.promql += other.promql | ||
| } | ||
|
|
||
| type reportRow struct { | ||
| name string | ||
| total bool | ||
| tally reportTally | ||
| } | ||
|
|
||
| // label is the first cell: a code-formatted collection name, or the bold | ||
| // total row. | ||
| func (r reportRow) label() string { | ||
| if r.total { | ||
| return "**Total**" | ||
| } | ||
| return "`" + r.name + "`" | ||
| } | ||
|
|
||
| // cell formats one count or percentage, bold on the total row. The printed | ||
| // table is meant to be pasted into README.md unchanged. | ||
| func (r reportRow) cell(s string) string { | ||
| if r.total { | ||
| return "**" + s + "**" | ||
| } | ||
| return s | ||
| } | ||
|
|
||
| func TestCorpusReport(t *testing.T) { | ||
| if !*corpusReport { | ||
| t.Skip("set -corpus-report to print the compile report") | ||
| } | ||
| var rows []reportRow | ||
| var total reportTally | ||
| for _, dir := range corpusDirs(t) { | ||
| meta, queries := loadCorpus(t, dir) | ||
| got := measure(t, meta, queries) | ||
| rows = append(rows, reportRow{name: filepath.Base(dir), tally: got}) | ||
| total.add(got) | ||
| } | ||
| rows = append(rows, reportRow{name: "Total", total: true, tally: total}) | ||
|
|
||
| fmt.Println("| Collection | Queries | → IR | SPL→IR | → ES\\|QL | SPL→ES\\|QL | IR→ES\\|QL | → PromQL | SPL→PromQL | IR→PromQL |") | ||
| fmt.Println("|---|---|---|---|---|---|---|---|---|---|") | ||
| for _, row := range rows { | ||
| t := row.tally | ||
| fmt.Printf("| %s | %s | %s | %s | %s | %s | %s | %s | %s | %s |\n", | ||
| row.label(), | ||
| row.cell(count(t.queries)), row.cell(count(t.parsed)), | ||
| row.cell(percent(t.parsed, t.queries)), | ||
| row.cell(count(t.esql)), row.cell(percent(t.esql, t.queries)), | ||
| row.cell(percent(t.esql, t.parsed)), | ||
| row.cell(count(t.promql)), row.cell(percent(t.promql, t.queries)), | ||
| row.cell(percent(t.promql, t.parsed))) | ||
| } | ||
| } | ||
|
|
||
| // measure counts, for one collection, the queries that parse and the queries | ||
| // that also emit a non-empty query for each back-end. | ||
| func measure(t *testing.T, meta corpusMeta, queries []corpusQuery) reportTally { | ||
| t.Helper() | ||
| version := compiler.SPLVersion1 | ||
| if meta.Dialect == "spl2" { | ||
| version = compiler.SPLVersion2 | ||
| } | ||
| var tally reportTally | ||
| for _, q := range queries { | ||
| tally.queries++ | ||
| p, err := compiler.ParseVersion(q.Query, version) | ||
| if err != nil { | ||
| continue | ||
| } | ||
| tally.parsed++ | ||
| if emits(p, compiler.ESQL) { | ||
| tally.esql++ | ||
| } | ||
| if emits(p, compiler.PromQL) { | ||
| tally.promql++ | ||
| } | ||
| } | ||
| return tally | ||
| } | ||
|
|
||
| func emits(p *ast.Pipeline, target compiler.Target) bool { | ||
| res, err := compiler.Transpile(p, target, compiler.Options{}) | ||
| if err != nil || res == nil { | ||
| return false | ||
| } | ||
| return strings.TrimSpace(res.Output) != "" | ||
| } | ||
|
|
||
| // count formats a count with thousands separators, so the printed table can be | ||
| // pasted into the README unchanged. | ||
| func count(n int) string { | ||
| s := fmt.Sprintf("%d", n) | ||
| if len(s) <= 3 { | ||
| return s | ||
| } | ||
| var b strings.Builder | ||
| for i, r := range s { | ||
| if i > 0 && (len(s)-i)%3 == 0 { | ||
| b.WriteByte(',') | ||
| } | ||
| b.WriteRune(r) | ||
| } | ||
| return b.String() | ||
| } | ||
|
|
||
| func percent(n, of int) string { | ||
| if of == 0 { | ||
| return "—" | ||
| } | ||
| return fmt.Sprintf("%.1f%%", 100*float64(n)/float64(of)) | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.