-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathmjml_benchmark_test.go
182 lines (135 loc) · 3.35 KB
/
mjml_benchmark_test.go
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
package mjml
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"os"
"os/exec"
"testing"
"time"
)
type testTemplate struct {
file string
input string
output string
}
func getTestTemplates() ([]testTemplate, error) {
var result []testTemplate
files := []string{
"black-friday",
"one-page",
"reactivation-email",
"real-estate",
"recast",
"receipt-email",
}
for _, file := range files {
input, err := os.ReadFile("testdata/" + file + ".mjml")
if err != nil {
return result, fmt.Errorf("error reading input %s.mjml: %w", file, err)
}
output, err := os.ReadFile("testdata/" + file + ".html")
if err != nil {
return result, fmt.Errorf("error reading output %s.mjml: %w", file, err)
}
result = append(result, testTemplate{
file: file,
input: string(input),
output: string(output),
})
}
return result, nil
}
func BenchmarkNodeJS(b *testing.B) {
serverBin := "node-test-server/server"
cmd := exec.Command(serverBin)
err := cmd.Start()
if err != nil {
b.Fatalf("Error starting Node.js reference server: %s", err)
}
defer cmd.Process.Kill()
nodeAddr := "http://localhost:8888"
client := &http.Client{}
var tries int
for {
_, err := client.Get(nodeAddr)
if err == nil {
break
}
if tries > 5 {
b.Fatalf("Timed out while waiting for Node.js reference server to be available: %s", err)
}
time.Sleep(1 * time.Second)
tries++
}
testCases, err := getTestTemplates()
if err != nil {
b.Fatalf("Error getting test cases: %s", err)
}
type request struct {
MJML string `json:"mjml"`
Options map[string]string `json:"options"`
}
for _, testCase := range testCases {
b.Run(testCase.file, func(b *testing.B) {
b.ResetTimer()
for i := 0; i < b.N; i++ {
r := request{
MJML: testCase.input,
Options: map[string]string{
"validationLevel": "skip",
},
}
jsonRequest, err := json.Marshal(r)
if err != nil {
b.Fatalf("Error marshaling json request: %s", err)
}
result, err := client.Post(nodeAddr, "application/json", bytes.NewReader(jsonRequest))
if err != nil {
b.Fatalf("Error posting request to Node.js server: %s", err)
}
var decoded jsonResult
body, err := io.ReadAll(result.Body)
if err != nil {
b.Fatalf("Error reading response body: %s", err)
}
err = json.Unmarshal(body, &decoded)
if err != nil {
b.Fatalf("Error decoding response; %s", err)
}
if decoded.Error != nil {
b.Fatalf("Error converting input to HTML: %s", err)
}
if decoded.HTML != testCase.output {
b.Fatalf("Output for input (%s.mjml) does not match expected response", testCase.file)
}
}
})
}
}
func BenchmarkMJMLGo(b *testing.B) {
testCases, err := getTestTemplates()
if err != nil {
b.Fatalf("Error getting test cases: %s", err)
}
if err != nil {
b.Fatalf("Error setting max workers: %s", err)
}
for _, testCase := range testCases {
b.Run(testCase.file, func(b *testing.B) {
b.ResetTimer()
for i := 0; i < b.N; i++ {
result, err := ToHTML(context.Background(), testCase.input, WithValidationLevel(Skip))
if err != nil {
b.Fatalf("Error converting input to HTML: %s", err)
}
if result != testCase.output {
b.Fatalf("Output for input (%s.mjml) does not match expected response", testCase.file)
}
}
})
}
}