-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Backend] Benchmark Hashing and weather api
- Loading branch information
Showing
13 changed files
with
274 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
package benchmark | ||
|
||
import ( | ||
"fmt" | ||
"hash/fnv" | ||
"testing" | ||
|
||
"github.com/cespare/xxhash/v2" | ||
"github.com/zeebo/xxh3" | ||
) | ||
|
||
// TODO: The main benchmarks live in the xxhash package now, so the only purpose | ||
// of this is to compare different hash functions. Consider deleting xxhashbench | ||
// or replacing it with a more minimal comparison. | ||
|
||
var sink uint64 | ||
|
||
var benchmarks = []struct { | ||
name string | ||
directBytes func([]byte) uint64 | ||
directString func(string) uint64 | ||
digestBytes func([]byte) uint64 | ||
digestString func(string) uint64 | ||
}{ | ||
{ | ||
name: "xxhash", | ||
directBytes: xxhash.Sum64, | ||
directString: xxhash.Sum64String, | ||
digestBytes: func(b []byte) uint64 { | ||
h := xxhash.New() | ||
h.Write(b) | ||
return h.Sum64() | ||
}, | ||
digestString: func(s string) uint64 { | ||
h := xxhash.New() | ||
h.WriteString(s) | ||
return h.Sum64() | ||
}, | ||
}, | ||
{ | ||
name: "xxh3", | ||
directBytes: xxh3.Hash, | ||
directString: xxh3.HashString, | ||
digestBytes: func(b []byte) uint64 { | ||
h := xxh3.New() | ||
h.Write(b) | ||
return h.Sum64() | ||
}, | ||
digestString: func(s string) uint64 { | ||
h := xxh3.New() | ||
h.WriteString(s) | ||
return h.Sum64() | ||
}, | ||
}, | ||
{ | ||
name: "FNV-1a", | ||
digestBytes: func(b []byte) uint64 { | ||
h := fnv.New64() | ||
h.Write(b) | ||
return h.Sum64() | ||
}, | ||
digestString: func(s string) uint64 { | ||
h := fnv.New64a() | ||
h.Write([]byte(s)) | ||
return h.Sum64() | ||
}, | ||
}, | ||
} | ||
|
||
func BenchmarkHashes(b *testing.B) { | ||
for _, bb := range benchmarks { | ||
for _, benchSize := range []struct { | ||
name string | ||
n int | ||
}{ | ||
{"5B", 5}, | ||
{"100B", 100}, | ||
{"4KB", 4e3}, | ||
{"10MB", 10e6}, | ||
{"1GB", 10e9}, | ||
} { | ||
input := make([]byte, benchSize.n) | ||
for i := range input { | ||
input[i] = byte(i) | ||
} | ||
inputString := string(input) | ||
if bb.directBytes != nil { | ||
name := fmt.Sprintf("%s,direct,bytes,n=%s", bb.name, benchSize.name) | ||
b.Run(name, func(b *testing.B) { | ||
benchmarkHashBytes(b, input, bb.directBytes) | ||
}) | ||
} | ||
if bb.directString != nil { | ||
name := fmt.Sprintf("%s,direct,string,n=%s", bb.name, benchSize.name) | ||
b.Run(name, func(b *testing.B) { | ||
benchmarkHashString(b, inputString, bb.directString) | ||
}) | ||
} | ||
if bb.digestBytes != nil { | ||
name := fmt.Sprintf("%s,digest,bytes,n=%s", bb.name, benchSize.name) | ||
b.Run(name, func(b *testing.B) { | ||
benchmarkHashBytes(b, input, bb.digestBytes) | ||
}) | ||
} | ||
if bb.digestString != nil { | ||
name := fmt.Sprintf("%s,digest,string,n=%s", bb.name, benchSize.name) | ||
b.Run(name, func(b *testing.B) { | ||
benchmarkHashString(b, inputString, bb.digestString) | ||
}) | ||
} | ||
} | ||
} | ||
} | ||
|
||
func benchmarkHashBytes(b *testing.B, input []byte, fn func([]byte) uint64) { | ||
b.SetBytes(int64(len(input))) | ||
for i := 0; i < b.N; i++ { | ||
sink = fn(input) | ||
} | ||
} | ||
|
||
func benchmarkHashString(b *testing.B, input string, fn func(string) uint64) { | ||
b.SetBytes(int64(len(input))) | ||
for i := 0; i < b.N; i++ { | ||
sink = fn(input) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package kakao | ||
|
||
// WeatherResponse | ||
type WeatherResponse struct { | ||
Codes Codes `json:"codes"` | ||
WeatherInfos WeatherInfos `json:"weatherInfos"` | ||
} | ||
|
||
type Codes struct { | ||
ResultCode string `json:"resultCode"` | ||
Hcode Code `json:"hcode"` | ||
Bcode Code `json:"bcode"` | ||
} | ||
|
||
type Code struct { | ||
Type string `json:"type"` | ||
Code string `json:"code"` | ||
Name string `json:"name"` | ||
FullName string `json:"fullName"` | ||
RegionId string `json:"regionId"` | ||
Name0 string `json:"name0"` | ||
Code1 string `json:"code1"` | ||
Name1 string `json:"name1"` | ||
Code2 string `json:"code2"` | ||
Name2 string `json:"name2"` | ||
Code3 string `json:"code3"` | ||
Name3 string `json:"name3"` | ||
Childcount float64 `json:"childcount"` | ||
X float64 `json:"x"` | ||
Y float64 `json:"y"` | ||
} | ||
|
||
type WeatherInfos struct { | ||
Current WeatherInfo `json:"current"` | ||
Forecast WeatherInfo `json:"forecast"` | ||
} | ||
|
||
type WeatherInfo struct { | ||
Type string `json:"type"` | ||
Rcode string `json:"rcode"` | ||
IconId string `json:"iconId"` | ||
Temperature string `json:"temperature"` | ||
Desc string `json:"desc"` | ||
Humidity string `json:"humidity"` | ||
Rainfall string `json:"rainfall"` | ||
Snowfall string `json:"snowfall"` | ||
} | ||
|
||
type WeatherRequest struct { | ||
Temperature string `json:"temperature"` | ||
Desc string `json:"desc"` | ||
IconImage string `json:"iconImage"` | ||
Humidity string `json:"humidity"` | ||
Rainfall string `json:"rainfall"` | ||
Snowfall string `json:"snowfall"` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.