-
Notifications
You must be signed in to change notification settings - Fork 90
/
bench_test.go
423 lines (329 loc) · 12.6 KB
/
bench_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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
// MIT License
//
// Copyright (c) 2020 Plamen Petrov and EASE lab
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
package main
import (
"context"
"flag"
"fmt"
"os"
"os/exec"
"path/filepath"
"strconv"
"strings"
"sync"
"testing"
"time"
"github.com/vhive-serverless/vhive/metrics"
log "github.com/sirupsen/logrus"
"github.com/stretchr/testify/require"
)
var (
parallelNum = flag.Int("parallel", 1, "Number of parallel instances to start")
iterNum = flag.Int("iter", 1, "Number of iterations to run")
funcName = flag.String("funcName", "helloworld", "Name of the function to benchmark")
)
func TestBenchParallelServe(t *testing.T) {
var (
servedTh uint64
pinnedFuncNum int
isSyncOffload = true
serveMetrics = make([]*metrics.Metric, *parallelNum)
upfMetrics = make([]*metrics.Metric, *parallelNum)
images = getAllImages()
parallel = *parallelNum
vmID = 0
concurrency = 2
)
imageName, isPresent := images[*funcName]
require.True(t, isPresent, "Function is not supported")
funcPool = NewFuncPool(!isSaveMemoryConst, servedTh, pinnedFuncNum, isTestModeConst)
createResultsDir()
// Pull image
resp, _, err := funcPool.Serve(context.Background(), "plr-fnc", imageName, "record")
require.NoError(t, err, "Function returned error")
require.Equal(t, resp.Payload, "Hello, record_response!")
createSnapshots(t, concurrency, vmID, imageName, isSyncOffload)
log.Info("All snapshots created")
createRecords(t, concurrency, vmID, imageName, isSyncOffload)
log.Info("All records done")
// Measure
var vmGroup sync.WaitGroup
if !*isWithCache {
dropPageCache()
}
tStart := time.Now()
for i := 0; i < parallel; i++ {
vmIDString := strconv.Itoa(vmID + i)
vmGroup.Add(1)
go func(i int) {
defer vmGroup.Done()
resp, metr, err := funcPool.Serve(context.Background(), vmIDString, imageName, "replay")
require.NoError(t, err, "Function returned error")
require.Equal(t, resp.Payload, "Hello, replay_response!")
serveMetrics[i] = metr
}(i)
}
vmGroup.Wait()
log.Printf("Started %d instances in %d milliseconds", parallel, time.Since(tStart).Milliseconds())
for i := 0; i < parallel; i++ {
vmIDString := strconv.Itoa(vmID + i)
message, err := funcPool.RemoveInstance(vmIDString, imageName, isSyncOffload)
require.NoError(t, err, "Function returned error, "+message)
if *isUPFEnabledTest {
memManagerMetrics, err := orch.GetUPFLatencyStats(vmIDString + "-0")
require.NoError(t, err, "Failed to ge tupf metrics")
require.Equal(t, len(memManagerMetrics), 1, "wrong length")
upfMetrics[i] = memManagerMetrics[0]
}
}
fusePrintMetrics(t, serveMetrics, upfMetrics, isUPFEnabledTest, true, *funcName, "parallelServe.csv")
}
func TestBenchWarmServe(t *testing.T) {
var (
servedTh uint64
pinnedFuncNum int
isSyncOffload = true
images = getAllImages()
vmID = 0
memManagerMetrics []*metrics.Metric
)
imageName, isPresent := images[*funcName]
require.True(t, isPresent, "Function is not supported")
funcPool = NewFuncPool(!isSaveMemoryConst, servedTh, pinnedFuncNum, isTestModeConst)
createResultsDir()
vmIDString := strconv.Itoa(vmID)
// First time invoke (cold start)
resp, _, err := funcPool.Serve(context.Background(), vmIDString, imageName, "replay")
require.NoError(t, err, "Function returned error")
require.Equal(t, resp.Payload, "Hello, replay_response!")
// memory footprint
memFootprint, err := getMemFootprint()
if err != nil {
log.Warnf("Failed to get memory footprint of VM=%s, image=%s\n", vmIDString, imageName)
}
// Measure warm
serveMetrics := make([]*metrics.Metric, *iterNum)
for k := 0; k < *iterNum; k++ {
if !*isWithCache {
dropPageCache()
}
resp, met, err := funcPool.Serve(context.Background(), vmIDString, imageName, "replay")
require.NoError(t, err, "Function returned error")
require.Equal(t, resp.Payload, "Hello, replay_response!")
serveMetrics[k] = met
}
message, err := funcPool.RemoveInstance(vmIDString, imageName, isSyncOffload)
require.NoError(t, err, "Function returned error, "+message)
// FUSE
if orch.GetUPFEnabled() {
// Page stats
err = funcPool.DumpUPFPageStats(vmIDString, imageName, *funcName, getOutFile("pageStats.csv"))
require.NoError(t, err, "Failed to dump page stats for"+*funcName)
memManagerMetrics, err = orch.GetUPFLatencyStats(vmIDString + "-0")
require.NoError(t, err, "Failed to dump get stats for "+*funcName)
require.Equal(t, len(serveMetrics), len(memManagerMetrics), "different metrics lengths")
}
fusePrintMetrics(t, serveMetrics, memManagerMetrics, isUPFEnabledTest, true, *funcName, "serve.csv")
appendMemFootprint(getOutFile("serve.csv"), memFootprint)
}
func TestBenchServe(t *testing.T) {
var (
servedTh uint64
pinnedFuncNum int
isSyncOffload = true
images = getAllImages()
vmID = 0
memManagerMetrics []*metrics.Metric
)
imageName, isPresent := images[*funcName]
require.True(t, isPresent, "Function is not supported")
funcPool = NewFuncPool(!isSaveMemoryConst, servedTh, pinnedFuncNum, isTestModeConst)
createResultsDir()
// Pull image
resp, _, err := funcPool.Serve(context.Background(), "plr-fnc", imageName, "record")
require.NoError(t, err, "Function returned error")
require.Equal(t, resp.Payload, "Hello, record_response!")
vmIDString := strconv.Itoa(vmID)
createSnapshots(t, 1, vmID, imageName, isSyncOffload)
createRecords(t, 1, vmID, imageName, isSyncOffload)
// Measure
serveMetrics := make([]*metrics.Metric, *iterNum)
for k := 0; k < *iterNum; k++ {
if !*isWithCache {
dropPageCache()
}
resp, met, err := funcPool.Serve(context.Background(), vmIDString, imageName, "replay")
require.NoError(t, err, "Function returned error")
require.Equal(t, resp.Payload, "Hello, replay_response!")
serveMetrics[k] = met
time.Sleep(1 * time.Second) // this helps kworker hanging
message, err := funcPool.RemoveInstance(vmIDString, imageName, isSyncOffload)
require.NoError(t, err, "Function returned error, "+message)
time.Sleep(3 * time.Second) // this helps kworker hanging
}
// FUSE
if orch.GetUPFEnabled() {
// Page stats
err = funcPool.DumpUPFPageStats(vmIDString, imageName, *funcName, getOutFile("pageStats.csv"))
require.NoError(t, err, "Failed to dump page stats for"+*funcName)
memManagerMetrics, err = orch.GetUPFLatencyStats(vmIDString + "-0")
require.NoError(t, err, "Failed to dump get stats for "+*funcName)
require.Equal(t, len(serveMetrics), len(memManagerMetrics), "different metrics lengths")
}
fusePrintMetrics(t, serveMetrics, memManagerMetrics, isUPFEnabledTest, true, *funcName, "serve.csv")
}
// /////////////////////////////////////////////////////////////////////////////
// //////////////////////// Auxialiary functions below /////////////////////////
// /////////////////////////////////////////////////////////////////////////////
func fusePrintMetrics(t *testing.T, serveMetrics, upfMetrics []*metrics.Metric, isUPFEnabledTest *bool, printIndiv bool, funcName, outfile string) {
outFileName := getOutFile(outfile)
if *isUPFEnabledTest {
for i, metr := range serveMetrics {
for k, v := range upfMetrics[i].MetricMap {
metr.MetricMap[k] = v
}
}
}
if printIndiv {
for _, metr := range serveMetrics {
err := metrics.PrintMeanStd(outFileName, funcName, metr)
require.NoError(t, err, "Failed to dump stats")
}
}
err := metrics.PrintMeanStd(outFileName, funcName, serveMetrics...)
require.NoError(t, err, "Failed to dump stats")
}
func createSnapshots(t *testing.T, concurrency, vmID int, imageName string, isSyncOffload bool) {
parallel := *parallelNum
sem := make(chan bool, concurrency)
for i := 0; i < parallel; i++ {
vmIDString := strconv.Itoa(vmID + i)
sem <- true
go func(vmIDString string) {
defer func() { <-sem }()
// Create VM (and snapshot)
resp, _, err := funcPool.Serve(context.Background(), vmIDString, imageName, "record")
require.NoError(t, err, "Function returned error")
require.Equal(t, resp.Payload, "Hello, record_response!")
message, err := funcPool.RemoveInstance(vmIDString, imageName, isSyncOffload)
require.NoError(t, err, "Function returned error, "+message)
}(vmIDString)
}
for i := 0; i < cap(sem); i++ {
sem <- true
}
}
func createRecords(t *testing.T, concurrency, vmID int, imageName string, isSyncOffload bool) {
parallel := *parallelNum
sem := make(chan bool, concurrency)
for i := 0; i < parallel; i++ {
vmIDString := strconv.Itoa(vmID + i)
sem <- true
go func(vmIDString string) {
defer func() { <-sem }()
// Record
resp, _, err := funcPool.Serve(context.Background(), vmIDString, imageName, "record")
require.NoError(t, err, "Function returned error")
require.Equal(t, resp.Payload, "Hello, record_response!")
message, err := funcPool.RemoveInstance(vmIDString, imageName, isSyncOffload)
require.NoError(t, err, "Function returned error, "+message)
}(vmIDString)
}
for i := 0; i < cap(sem); i++ {
sem <- true
}
}
func dropPageCache() {
cmd := exec.Command("sudo", "/bin/sh", "-c", "sync; echo 1 > /proc/sys/vm/drop_caches")
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stdout
if err := cmd.Run(); err != nil {
log.Fatalf("Failed to drop caches: %v", err)
}
}
func createResultsDir() {
if err := os.MkdirAll(*benchDir, 0777); err != nil {
log.Fatalf("Failed to create results dir: %v", err)
}
}
func getOutFile(name string) string {
return filepath.Join(*benchDir, name)
}
func getAllImages() map[string]string {
return map[string]string{
"helloworld": "ghcr.io/ease-lab/helloworld:var_workload",
"chameleon": "ghcr.io/ease-lab/chameleon:var_workload",
"pyaes": "ghcr.io/ease-lab/pyaes:var_workload",
"image_rotate": "ghcr.io/ease-lab/image_rotate:var_workload",
"image_rotate_s3": "ghcr.io/ease-lab/image_rotate_s3:var_workload",
"json_serdes": "ghcr.io/ease-lab/json_serdes:var_workload",
"json_serdes_s3": "ghcr.io/ease-lab/json_serdes_s3:var_workload",
"lr_serving": "ghcr.io/ease-lab/lr_serving:var_workload",
"cnn_serving": "ghcr.io/ease-lab/cnn_serving:var_workload",
"rnn_serving": "ghcr.io/ease-lab/rnn_serving:var_workload",
"lr_training_s3": "ghcr.io/ease-lab/lr_training_s3:var_workload",
"lr_training": "ghcr.io/ease-lab/lr_training:var_workload",
"video_processing_s3": "ghcr.io/ease-lab/video_processing_s3:var_workload",
}
}
// getFirecrackerPid Assumes that only one Firecracker process is running
func getFirecrackerPid() ([]byte, error) {
pidBytes, err := exec.Command("pidof", "firecracker").Output()
if err != nil {
log.Warnf("Failed to get Firecracker PID: %v", err)
}
return pidBytes, err
}
func getMemFootprint() (float64, error) {
pidBytes, err := getFirecrackerPid()
if err != nil {
return 0, err
}
pid, err := strconv.Atoi(strings.TrimSpace(strings.Split(string(pidBytes), "\n")[0]))
if err != nil {
log.Warnf("Pid conversion failed: %v", err)
}
cmd := exec.Command("ps", "-o", "rss", "-p", strconv.Itoa(pid))
stdout, err := cmd.Output()
if err != nil {
log.Warnf("Failed to run ps command: %v", err)
}
infoArr := strings.Split(string(stdout), "\n")[1]
stats := strings.Fields(infoArr)
rss, err := strconv.ParseFloat(stats[0], 64)
if err != nil {
log.Warnf("Error in conversion when computing rss: %v", err)
return 0, err
}
rss *= 1024
return rss, nil
}
func appendMemFootprint(outFileName string, memFootprint float64) {
f, err := os.OpenFile(outFileName, os.O_APPEND|os.O_WRONLY, 0666)
if err != nil {
log.Println(err)
}
defer f.Close()
if _, err := f.WriteString(fmt.Sprintf("MemFootprint\t%12.1f\n", memFootprint)); err != nil {
log.Println(err)
}
}