-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcountry_db_test.go
82 lines (69 loc) · 1.41 KB
/
country_db_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
package database
import (
"encoding/json"
"io/ioutil"
"strconv"
"testing"
)
func BenchmarkPopulate(b *testing.B) {
data := map[string]Country{}
for _, v := range []string{"pop.json", "geo.json"} {
byt, err := ioutil.ReadFile(v)
if err != nil {
panic(err)
}
switch v {
case "pop.json":
jdata := []struct {
Country, Population string
}{}
err = json.Unmarshal(byt, &jdata)
if err != nil {
panic(err)
}
for _, v := range jdata {
vv := Country{}
copy(vv.Name[:], []byte(v.Country))
vv.Population, err = strconv.ParseUint(v.Population, 10, 64)
if err != nil {
vv.Population = 0
}
}
case "geo.json":
jdata := []struct {
Country string
North, South, East, West string
}{}
err = json.Unmarshal(byt, &jdata)
if err != nil {
panic(err)
}
for _, v := range jdata {
vv := data[v.Country]
vv.North, err = strconv.ParseFloat(v.North, 64)
vv.South, err = strconv.ParseFloat(v.South, 64)
vv.East, err = strconv.ParseFloat(v.East, 64)
vv.West, err = strconv.ParseFloat(v.West, 64)
data[v.Country] = vv
}
}
}
db, err := NewCountryDBImpl("test.bin")
if err != nil {
panic(err)
}
for n := 0; n < b.N; n++ {
for k, v := range data {
err = db.Set(k, v)
if err != nil {
panic(err)
}
}
for k := range data {
_, err := db.Del(k)
if err != nil {
panic(err)
}
}
}
}