-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathresource_memory.go
115 lines (90 loc) · 2.68 KB
/
resource_memory.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
package main
import (
"fmt"
"math/rand"
"runtime"
"time"
petname "github.com/dustinkirkland/golang-petname"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
func resourceMemory() *schema.Resource {
return &schema.Resource{
Create: resourceMemoryCreate,
Read: resourceMemoryRead,
Update: resourceMemoryUpdate,
Delete: resourceMemoryDelete,
Schema: map[string]*schema.Schema{
"duration": {
Type: schema.TypeInt,
Required: true,
},
"size": {
Type: schema.TypeInt,
Required: true,
},
},
}
}
func resourceMemoryCreate(d *schema.ResourceData, m interface{}) error {
// Below is an example of using our PrintMemUsage() function
// Print our starting memory usage (should be around 0mb)
PrintMemUsage()
size := d.Get("size").(int)
sizeInBytes := size * (1024 * 1024)
// taken from https://golang.org/src/bytes/buffer_test.go
var testBytes []byte
testBytes = make([]byte, sizeInBytes)
for i := 0; i < sizeInBytes; i++ {
testBytes[i] = 'a' + byte(i%26)
}
PrintMemUsage()
duration := d.Get("duration").(int)
time.Sleep(time.Second * time.Duration(duration))
// Force GC to clear up, should see a memory drop
runtime.GC()
PrintMemUsage()
rand.Seed(time.Now().UTC().UnixNano())
id := petname.Generate(3, "-")
d.SetId(id)
return resourceMemoryRead(d, m)
}
func resourceMemoryRead(d *schema.ResourceData, m interface{}) error {
return nil
}
func resourceMemoryUpdate(d *schema.ResourceData, m interface{}) error {
// Below is an example of using our PrintMemUsage() function
// Print our starting memory usage (should be around 0mb)
PrintMemUsage()
size := d.Get("size").(int)
sizeInBytes := size * (1024 * 1024)
// taken from https://golang.org/src/bytes/buffer_test.go
var testBytes []byte
testBytes = make([]byte, sizeInBytes)
for i := 0; i < sizeInBytes; i++ {
testBytes[i] = 'a' + byte(i%26)
}
PrintMemUsage()
duration := d.Get("duration").(int)
time.Sleep(time.Second * time.Duration(duration))
// Force GC to clear up, should see a memory drop
runtime.GC()
PrintMemUsage()
return resourceMemoryRead(d, m)
}
func resourceMemoryDelete(d *schema.ResourceData, m interface{}) error {
return nil
}
// PrintMemUsage outputs the current, total and OS memory being used. As well as the number
// of garage collection cycles completed.
func PrintMemUsage() {
var m runtime.MemStats
runtime.ReadMemStats(&m)
// For info on each, see: https://golang.org/pkg/runtime/#MemStats
fmt.Printf("Alloc = %v MiB", bToMb(m.Alloc))
fmt.Printf("\tTotalAlloc = %v MiB", bToMb(m.TotalAlloc))
fmt.Printf("\tSys = %v MiB", bToMb(m.Sys))
fmt.Printf("\tNumGC = %v\n", m.NumGC)
}
func bToMb(b uint64) uint64 {
return b / 1024 / 1024
}