-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathbenchmark_test.go
More file actions
101 lines (94 loc) · 2.31 KB
/
Copy pathbenchmark_test.go
File metadata and controls
101 lines (94 loc) · 2.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
package einvoice
import (
"bytes"
"os"
"testing"
)
// Benchmark suite for measuring performance across all formats and profiles.
// Run with: go test -bench=BenchmarkSuite -benchmem
// Created in response to https://github.com/speedata/einvoice/issues/143
// benchmarkFixtures defines representative test files for each format/profile.
var benchmarkFixtures = []struct {
name string
file string
}{
{"CII/Minimum", "testdata/cii/minimum/zugferd-minimum-rechnung.xml"},
{"CII/Basic", "testdata/cii/basic/zugferd-basic-einfach.xml"},
{"CII/EN16931", "testdata/cii/en16931/CII_example1.xml"},
{"CII/Extended", "testdata/cii/extended/zugferd-extended-warenrechnung.xml"},
{"CII/XRechnung", "testdata/cii/xrechnung/zugferd-xrechnung-einfach.xml"},
{"UBL/Invoice", "testdata/ubl/invoice/ubl-tc434-example1.xml"},
{"UBL/CreditNote", "testdata/ubl/creditnote/ubl-tc434-creditnote1.xml"},
{"PEPPOL/Invoice", "testdata/peppol/valid/base-example.xml"},
}
func BenchmarkSuiteParse(b *testing.B) {
for _, bm := range benchmarkFixtures {
data, err := os.ReadFile(bm.file)
if err != nil {
continue
}
b.Run(bm.name, func(b *testing.B) {
b.ReportAllocs()
b.SetBytes(int64(len(data)))
b.ResetTimer()
for b.Loop() {
if _, err := ParseReader(bytes.NewReader(data)); err != nil {
b.Fatal(err)
}
}
})
}
}
func BenchmarkSuiteValidate(b *testing.B) {
for _, bm := range benchmarkFixtures {
inv, err := ParseXMLFile(bm.file)
if err != nil {
continue
}
b.Run(bm.name, func(b *testing.B) {
b.ReportAllocs()
b.ResetTimer()
for b.Loop() {
_ = inv.Validate()
}
})
}
}
func BenchmarkSuiteCalculate(b *testing.B) {
for _, bm := range benchmarkFixtures {
inv, err := ParseXMLFile(bm.file)
if err != nil {
continue
}
if inv.ProfileLevel() < 3 {
continue
}
b.Run(bm.name, func(b *testing.B) {
b.ReportAllocs()
b.ResetTimer()
for b.Loop() {
inv.UpdateApplicableTradeTax(nil)
inv.UpdateTotals()
}
})
}
}
func BenchmarkSuiteWrite(b *testing.B) {
for _, bm := range benchmarkFixtures {
inv, err := ParseXMLFile(bm.file)
if err != nil {
continue
}
b.Run(bm.name, func(b *testing.B) {
var buf bytes.Buffer
b.ReportAllocs()
b.ResetTimer()
for b.Loop() {
buf.Reset()
if err := inv.Write(&buf); err != nil {
b.Fatal(err)
}
}
})
}
}