-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.go
More file actions
157 lines (126 loc) · 2.7 KB
/
main.go
File metadata and controls
157 lines (126 loc) · 2.7 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
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
package main
import (
"bufio"
"fmt"
"os"
"runtime"
"sync"
"github.com/guerinoni/sieve"
lru "github.com/hashicorp/golang-lru/v2"
s3fifo "github.com/scalalang2/golang-fifo/s3fifo"
golangsieve "github.com/scalalang2/golang-fifo/sieve"
)
const fileName = "input"
const capacity = 100
func main() {
// printMemoryUsage()
file, err := os.Open(fileName)
if err != nil {
fmt.Println(err)
return
}
defer file.Close()
scanner := bufio.NewScanner(file)
scanner.Split(bufio.ScanLines)
data := make([]string, 0)
for scanner.Scan() {
data = append(data, scanner.Text())
}
var wg sync.WaitGroup
wg.Add(5)
go func() {
missCountSieve := doSieve(data)
fmt.Printf("Miss count sieve: %d\n", missCountSieve)
wg.Done()
}()
go func() {
missCountSieveST := doSieveSingleThread(data)
fmt.Printf("Miss count sieve-single-thread: %d\n", missCountSieveST)
wg.Done()
}()
go func() {
missCountLRU := doLRU(data)
fmt.Printf("Miss count golang-lru: %d\n", missCountLRU)
wg.Done()
}()
go func() {
missCountFifo := doFifo(data)
fmt.Printf("Miss count s3-fifo: %d\n", missCountFifo)
wg.Done()
}()
go func() {
missCountGolangSieve := doGolangSieve(data)
fmt.Printf("Miss count golang-sieve: %d\n", missCountGolangSieve)
wg.Done()
}()
wg.Wait()
// printMemoryUsage()
}
func doSieve(input []string) int {
mc := 0
cache := sieve.New[string, string](capacity)
for _, d := range input {
if _, ok := cache.Get(d); !ok {
mc += 1
cache.Set(d, d)
}
}
return mc
}
func doSieveSingleThread(input []string) int {
mc := 0
cache := sieve.NewSingleThread[string, string](capacity)
for _, d := range input {
if _, ok := cache.Get(d); !ok {
mc += 1
cache.Set(d, d)
}
}
return mc
}
func doLRU(input []string) int {
mc := 0
cache, err := lru.New[string, string](capacity)
if err != nil {
fmt.Println(err)
return mc
}
for _, d := range input {
if _, ok := cache.Get(d); !ok {
mc += 1
cache.Add(d, d)
}
}
return mc
}
func doFifo(input []string) int {
mc := 0
cache := s3fifo.New[string, string](capacity, 0) // 0 TTL = no expiration
for _, d := range input {
if _, ok := cache.Get(d); !ok {
mc += 1
cache.Set(d, d)
}
}
return mc
}
func doGolangSieve(input []string) int {
mc := 0
cache := golangsieve.New[string, string](capacity, 0) // 0 TTL = no expiration
for _, d := range input {
if _, ok := cache.Get(d); !ok {
mc += 1
cache.Set(d, d)
}
}
return mc
}
func printMemoryUsage() {
var m runtime.MemStats
runtime.ReadMemStats(&m)
fmt.Printf("Alloc = %v KB\n", m.Alloc/1024)
fmt.Printf("TotalAlloc = %v KB\n", m.TotalAlloc/1024)
fmt.Printf("Sys = %v KB\n", m.Sys/1024)
fmt.Printf("NumGC = %v\n", m.NumGC)
fmt.Println("------")
}