Skip to content

Commit

Permalink
[Backend] Benchmark Hashing and weather api
Browse files Browse the repository at this point in the history
  • Loading branch information
Alfex4936 committed Mar 30, 2024
1 parent 016c6f3 commit eb0c429
Show file tree
Hide file tree
Showing 13 changed files with 274 additions and 12 deletions.
127 changes: 127 additions & 0 deletions backend/benchmark/hash_test.go
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)
}
}
56 changes: 56 additions & 0 deletions backend/dto/kakao/weather_dto.go
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"`
}
2 changes: 2 additions & 0 deletions backend/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ require (
github.com/gorilla/websocket v1.5.1 // indirect
github.com/josharian/intern v1.0.0 // indirect
github.com/klauspost/compress v1.17.7 // indirect
github.com/klauspost/cpuid/v2 v2.0.9 // indirect
github.com/mailru/easyjson v0.7.7 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
Expand All @@ -74,6 +75,7 @@ require (
github.com/valyala/bytebufferpool v1.0.0 // indirect
github.com/valyala/fasthttp v1.52.0 // indirect
github.com/valyala/tcplisten v1.0.0 // indirect
github.com/zeebo/xxh3 v1.0.2 // indirect
go.uber.org/multierr v1.11.0 // indirect
golang.org/x/exp v0.0.0-20240325151524-a685a6edb6d8 // indirect
golang.org/x/net v0.22.0 // indirect
Expand Down
4 changes: 4 additions & 0 deletions backend/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,8 @@ github.com/josharian/intern v1.0.0 h1:vlS4z54oSdjm0bgjRigI+G1HpF+tI+9rE5LLzOg8Hm
github.com/josharian/intern v1.0.0/go.mod h1:5DoeVV0s6jJacbCEi61lwdGj/aVlrQvzHFFd8Hwg//Y=
github.com/klauspost/compress v1.17.7 h1:ehO88t2UGzQK66LMdE8tibEd1ErmzZjNEqWkjLAKQQg=
github.com/klauspost/compress v1.17.7/go.mod h1:Di0epgTjJY877eYKx5yC51cX2A2Vl2ibi7bDH9ttBbw=
github.com/klauspost/cpuid/v2 v2.0.9 h1:lgaqFMSdTdQYdZ04uHyN2d/eKdOMyi2YLSvlQIBFYa4=
github.com/klauspost/cpuid/v2 v2.0.9/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa0213Md/qVLRg=
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk=
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
Expand Down Expand Up @@ -162,6 +164,8 @@ github.com/valyala/fasthttp v1.52.0/go.mod h1:hf5C4QnVMkNXMspnsUlfM3WitlgYflyhHY
github.com/valyala/tcplisten v1.0.0 h1:rBHj/Xf+E1tRGZyWIWwJDiRY0zc1Js+CV5DqwacVSA8=
github.com/valyala/tcplisten v1.0.0/go.mod h1:T0xQ8SeCZGxckz9qRXTfG43PvQ/mcWh7FwZEA7Ioqkc=
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
github.com/zeebo/xxh3 v1.0.2 h1:xZmwmqxHZA8AI603jOQ0tMqmBr9lPeFwGg6d+xy9DC0=
github.com/zeebo/xxh3 v1.0.2/go.mod h1:5NWz9Sef7zIDm2JHfFlcQvNekmcEl9ekUZQQKCYaDcA=
go.uber.org/goleak v1.3.0 h1:2K3zAYmnTNqV73imy9J1T3WC+gmCePx2hEGkimedGto=
go.uber.org/goleak v1.3.0/go.mod h1:CoHD4mav9JJNrW/WLlf7HGZPjdw8EucARQHekz1X6bE=
go.uber.org/multierr v1.11.0 h1:blXXJkSxSSfBVBlC76pxqeO+LN3aDfLQo+309xJstO0=
Expand Down
5 changes: 3 additions & 2 deletions backend/handlers/marker_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ func UpdateMarker(c *fiber.Ctx) error {
func DeleteMarkerHandler(c *fiber.Ctx) error {
// Auth
userID := c.Locals("userID").(int)
userRole := c.Locals("role").(string)

// Get MarkerID from the URL parameter
markerIDParam := c.Params("markerID")
Expand All @@ -147,9 +148,9 @@ func DeleteMarkerHandler(c *fiber.Ctx) error {
}

// Call the service function to delete the marker, now passing userID as well
err = services.DeleteMarker(userID, markerID)
err = services.DeleteMarker(userID, markerID, userRole)
if err != nil {
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{"error": "Failed to delete marker: " + err.Error()})
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{"error": "Failed to delete marker"})
}

go services.RemoveMarkerClick(markerID)
Expand Down
21 changes: 21 additions & 0 deletions backend/handlers/marker_location_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,27 @@ func GetMarkersClosebyAdmin(c *fiber.Ctx) error {
return c.JSON(markers)
}

func GetWeatherByWGS84Handler(c *fiber.Ctx) error {
latParam := c.Query("latitude")
longParam := c.Query("longitude")

lat, err := strconv.ParseFloat(latParam, 64)
if err != nil {
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{"error": "Invalid latitude"})
}

long, err := strconv.ParseFloat(longParam, 64)
if err != nil {
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{"error": "Invalid longitude"})
}

result, err := services.FetchWeatherFromAddress(lat, long)
if err != nil {
return c.Status(fiber.StatusConflict).JSON(fiber.Map{"error": "Failed to fetch weather from address: " + err.Error()})
}

return c.JSON(result)
}
func ConvertWGS84ToWCONGNAMULHandler(c *fiber.Ctx) error {
latParam := c.Query("latitude")
longParam := c.Query("longitude")
Expand Down
4 changes: 2 additions & 2 deletions backend/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ import (
"github.com/gofiber/storage/redis/v3"
"github.com/gofiber/swagger"
"github.com/gofiber/template/django/v3"
"github.com/joho/godotenv"
_ "github.com/joho/godotenv/autoload"
"go.uber.org/zap"
"golang.org/x/oauth2"
Expand All @@ -51,7 +50,7 @@ import (
// @host localhost:9452
// @BasePath /api/v1/
func main() {
godotenv.Overload()
// godotenv.Overload()

// Increase GOMAXPROCS
runtime.GOMAXPROCS(runtime.NumCPU() * 2) // twice the number of CPUs
Expand Down Expand Up @@ -289,6 +288,7 @@ func main() {
api.Get("/markers/ranking", handlers.GetMarkerRankingHandler)
api.Get("/markers/area-ranking", handlers.GetCurrentAreaMarkerRankingHandler)
api.Get("/markers/convert", handlers.ConvertWGS84ToWCONGNAMULHandler)
api.Get("/markers/weather", handlers.GetWeatherByWGS84Handler)

api.Post("/markers/upload", middlewares.AdminOnly, handlers.UploadMarkerPhotoToS3Handler)

Expand Down
6 changes: 4 additions & 2 deletions backend/middlewares/auth_middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,11 @@ func AuthMiddleware(c *fiber.Ctx) error {
}

// Fetch UserID and Username based on Email
userQuery := `SELECT Username, Email FROM Users WHERE UserID = ?`
userQuery := `SELECT Username, Email, Role FROM Users WHERE UserID = ?`
var username string
var email string
err = database.DB.QueryRow(userQuery, userID).Scan(&username, &email)
var role string
err = database.DB.QueryRow(userQuery, userID).Scan(&username, &email, &role)
if err != nil {
cookie := utils.ClearLoginCookie()
c.Cookie(&cookie)
Expand All @@ -53,6 +54,7 @@ func AuthMiddleware(c *fiber.Ctx) error {
c.Locals("userID", userID)
c.Locals("username", username)
c.Locals("email", email)
c.Locals("role", role)

// log.Printf("[DEBUG] Authenticated. %s", email)
return c.Next()
Expand Down
Binary file added backend/profile-chat
Binary file not shown.
4 changes: 2 additions & 2 deletions backend/services/chat_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ import (
"sync"
"time"

"github.com/cespare/xxhash/v2"
"github.com/goccy/go-json"
"github.com/gofiber/contrib/websocket"
csmap "github.com/mhmtszr/concurrent-swiss-map"
"github.com/redis/go-redis/v9"
"github.com/zeebo/xxh3"

"github.com/google/uuid"
)
Expand Down Expand Up @@ -100,7 +100,7 @@ type RoomConnectionManager struct {
// NewRoomConnectionManager initializes a ConnectionManager with a new haxmap instance
func NewRoomConnectionManager() *RoomConnectionManager {
hasher := func(key string) uint64 {
return xxhash.Sum64String(key)
return xxh3.HashString(key)
}

manager := &RoomConnectionManager{
Expand Down
Loading

0 comments on commit eb0c429

Please sign in to comment.