|
| 1 | +package extract |
| 2 | + |
| 3 | +import ( |
| 4 | + "os" |
| 5 | + "testing" |
| 6 | +) |
| 7 | + |
| 8 | +const benchAllSyntaxesHTML = `<!DOCTYPE html> |
| 9 | +<html lang="en"> |
| 10 | +<head> |
| 11 | + <meta charset="UTF-8"> |
| 12 | + <title>Combined all formats benchmark</title> |
| 13 | + <meta property="og:title" content="OpenGraph Article Title"/> |
| 14 | + <meta property="og:type" content="article"/> |
| 15 | + <meta property="og:url" content="https://www.example.com/article"/> |
| 16 | + <meta name="twitter:card" content="summary_large_image"/> |
| 17 | + <meta name="twitter:title" content="X Cards Article Title"/> |
| 18 | + <meta name="DC.title" content="Dublin Core Title"/> |
| 19 | + <meta name="DC.creator" content="Jane Doe"/> |
| 20 | + <meta name="DC.subject" content="Technology"/> |
| 21 | + <script type="application/ld+json"> |
| 22 | + {"@context":"https://schema.org/","@type":"Article","name":"JSON-LD Article","author":{"@type":"Person","name":"John Doe"}} |
| 23 | + </script> |
| 24 | +</head> |
| 25 | +<body> |
| 26 | +<div itemscope itemtype="https://schema.org/Person"> |
| 27 | + <span itemprop="name">John Doe</span> |
| 28 | + <span itemprop="jobTitle">Engineer</span> |
| 29 | +</div> |
| 30 | +<div vocab="https://schema.org/" typeof="Person"> |
| 31 | + <span property="name">Jane Doe</span> |
| 32 | + <span property="jobTitle">Designer</span> |
| 33 | +</div> |
| 34 | +<div class="h-card"> |
| 35 | + <span class="p-name">Test Person</span> |
| 36 | + <a class="u-url" href="https://example.com">example.com</a> |
| 37 | +</div> |
| 38 | +</body> |
| 39 | +</html>` |
| 40 | + |
| 41 | +func readBenchFixture(b *testing.B, path string) string { |
| 42 | + b.Helper() |
| 43 | + data, err := os.ReadFile(path) |
| 44 | + if err != nil { |
| 45 | + b.Fatalf("failed to read fixture %s: %v", path, err) |
| 46 | + } |
| 47 | + return string(data) |
| 48 | +} |
| 49 | + |
| 50 | +func BenchmarkExtract_OpenGraph(b *testing.B) { |
| 51 | + content := readBenchFixture(b, "test/test-11-opengraph-article.html") |
| 52 | + b.ReportAllocs() |
| 53 | + b.ResetTimer() |
| 54 | + for i := 0; i < b.N; i++ { |
| 55 | + e := New().SetSyntaxes([]Syntax{SyntaxOpenGraph}) |
| 56 | + _, _ = e.Extract("http://example.com", &content) |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +func BenchmarkExtract_XCards(b *testing.B) { |
| 61 | + content := readBenchFixture(b, "test/test-25-xcards-article.html") |
| 62 | + b.ReportAllocs() |
| 63 | + b.ResetTimer() |
| 64 | + for i := 0; i < b.N; i++ { |
| 65 | + e := New().SetSyntaxes([]Syntax{SyntaxXCards}) |
| 66 | + _, _ = e.Extract("http://example.com", &content) |
| 67 | + } |
| 68 | +} |
| 69 | + |
| 70 | +func BenchmarkExtract_JSONLD(b *testing.B) { |
| 71 | + content := readBenchFixture(b, "test/test-31-ldjson-multiple-objects.html") |
| 72 | + b.ReportAllocs() |
| 73 | + b.ResetTimer() |
| 74 | + for i := 0; i < b.N; i++ { |
| 75 | + e := New().SetSyntaxes([]Syntax{SyntaxJSONLD}) |
| 76 | + _, _ = e.Extract("http://example.com", &content) |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +func BenchmarkExtract_Microdata(b *testing.B) { |
| 81 | + content := readBenchFixture(b, "test/test-34-w3cmicrodata-extended.html") |
| 82 | + b.ReportAllocs() |
| 83 | + b.ResetTimer() |
| 84 | + for i := 0; i < b.N; i++ { |
| 85 | + e := New().SetSyntaxes([]Syntax{SyntaxMicrodata}) |
| 86 | + _, _ = e.Extract("http://example.com", &content) |
| 87 | + } |
| 88 | +} |
| 89 | + |
| 90 | +func BenchmarkExtract_RDFa(b *testing.B) { |
| 91 | + content := readBenchFixture(b, "test/test-42-rdfa-multiple.html") |
| 92 | + b.ReportAllocs() |
| 93 | + b.ResetTimer() |
| 94 | + for i := 0; i < b.N; i++ { |
| 95 | + e := New().SetSyntaxes([]Syntax{SyntaxRDFa}) |
| 96 | + _, _ = e.Extract("http://example.com", &content) |
| 97 | + } |
| 98 | +} |
| 99 | + |
| 100 | +func BenchmarkExtract_DublinCore(b *testing.B) { |
| 101 | + content := readBenchFixture(b, "test/test-47-dublincore-multiple.html") |
| 102 | + b.ReportAllocs() |
| 103 | + b.ResetTimer() |
| 104 | + for i := 0; i < b.N; i++ { |
| 105 | + e := New().SetSyntaxes([]Syntax{SyntaxDublinCore}) |
| 106 | + _, _ = e.Extract("http://example.com", &content) |
| 107 | + } |
| 108 | +} |
| 109 | + |
| 110 | +func BenchmarkExtract_Microformats(b *testing.B) { |
| 111 | + content := readBenchFixture(b, "test/test-53-microformats-extended.html") |
| 112 | + b.ReportAllocs() |
| 113 | + b.ResetTimer() |
| 114 | + for i := 0; i < b.N; i++ { |
| 115 | + e := New().SetSyntaxes([]Syntax{SyntaxMicroformats}) |
| 116 | + _, _ = e.Extract("http://example.com", &content) |
| 117 | + } |
| 118 | +} |
| 119 | + |
| 120 | +func BenchmarkExtract_AllSyntaxes(b *testing.B) { |
| 121 | + content := benchAllSyntaxesHTML |
| 122 | + b.ReportAllocs() |
| 123 | + b.ResetTimer() |
| 124 | + for i := 0; i < b.N; i++ { |
| 125 | + e := New() |
| 126 | + _, _ = e.Extract("http://example.com", &content) |
| 127 | + } |
| 128 | +} |
0 commit comments