-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
182 additions
and
57 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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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,78 @@ | ||
package cache | ||
|
||
import ( | ||
"log" | ||
"os" | ||
"strconv" | ||
"strings" | ||
|
||
"github.com/bradfitz/gomemcache/memcache" | ||
"github.com/joho/godotenv" | ||
) | ||
|
||
// NOTE: | ||
// Try to establish a connection with Memcached container or Local Memcached | ||
|
||
// EXPIRY_TIME - TTL for an item int cache | ||
var EXPIRY_TIME int | ||
|
||
func init() { | ||
dir, _ := os.Getwd() | ||
envFile := "variables.env" | ||
if strings.Contains(dir, "test") { | ||
envFile = "../variables.env" | ||
} | ||
|
||
if err := godotenv.Load(envFile); err != nil { | ||
log.Fatal("Error: No Environment File Found, cacheConnection.go", err) | ||
} | ||
|
||
// in seconds | ||
EXPIRY_TIME, _ = strconv.Atoi(os.Getenv("EXPIRATION_TIME")) | ||
} | ||
|
||
func tryMemcached(domain string) *memcache.Client { | ||
mc := memcache.New(domain) | ||
|
||
inputUrl := "https://stackoverflow.com/questions/58442596/golang-base64-to-hex-conversion" | ||
newUrl := "https://goRubu/MTAyNDE=" | ||
|
||
err := mc.Set(&memcache.Item{ | ||
Key: newUrl, | ||
Value: []byte(inputUrl), | ||
Expiration: int32(EXPIRY_TIME), | ||
}) | ||
|
||
if err != nil { | ||
log.Printf("Err: %v, Domain: %v", err, domain) | ||
return nil | ||
} | ||
|
||
return mc | ||
} | ||
|
||
// CreateCon - Create Memcached Connection | ||
// as this is called by mainService init function only once, hence checking whether local/docker memcache | ||
// is up will happen only once. | ||
func CreateCon() *memcache.Client { | ||
var cacheDomain = os.Getenv("MEMCACHED_DOMAIN_DOCKER") | ||
var client *memcache.Client | ||
|
||
client = tryMemcached(cacheDomain) | ||
|
||
if client == nil { | ||
log.Println("Connection Failed while trying to connect with Memcached Container") | ||
|
||
cacheDomain = os.Getenv("MEMCACHED_DOMAIN_LOCALHOST") | ||
client = tryMemcached(cacheDomain) | ||
if client == nil { | ||
log.Fatal("Connection Failed while Trying to connect with Local Memcached") | ||
} | ||
|
||
log.Println("Connected to Local Memcached!!") | ||
} else { | ||
log.Println("Connected to Memcached Container!!") | ||
} | ||
|
||
return client | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
# BenchMarking Shortened Url | ||
|
||
## Setup and Working | ||
|
||
You need to have [k6](https://k6.io/) installed. It's written in Go and scriptable in JS. | ||
|
||
1. K6 works with the concept of Virtual Users (VU) | ||
2. Each VU executes your script in a completely separate JS runtime, parallel to all of the other running VU. Code inside the default function is called VU code, and is run over and over, for as long as the test is running. | ||
3. Every virtual user (VU) performs the GET requests, in a continuous loop, as fast as it can. | ||
|
||
|
||
### Benchmarking | ||
|
||
*** For Shorten Url Endpoint *** | ||
1. For 1VU, in 30s. | ||
```bash | ||
k6 run -d 5s -u 1 ./load_test_shorten_url.js | ||
|
||
http_req_duration..........: avg=5ms min=1.66ms med=3.15ms max=222.44ms p(90)=8.7ms p(95)=12.5ms | ||
http_reqs..................: 934 186.794732/s | ||
``` | ||
|
||
> Conclusion | ||
95% of our users got served a response in under 12.5ms. | ||
In the 30 second test duration we served 934 responses, at a rate of ~187 requests per second (RPS). |
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,27 @@ | ||
import { check } from "k6"; | ||
import http from "k6/http"; | ||
import { Rate } from 'k6/metrics'; | ||
|
||
|
||
export let errorRate = new Rate('errors'); | ||
|
||
export default function() { | ||
var url = "http://localhost:8080/all/shorten_url"; | ||
var params = { | ||
headers: { | ||
'Content-Type': 'application/json' | ||
} | ||
}; | ||
|
||
var data = JSON.stringify({ | ||
"Url": "https://stackoverflow.com/questions/58442596/golang-base64-to-hex-conversion" | ||
}); | ||
|
||
check(http.post(url, data, params), { | ||
'status is 20': r => r.status == 200 | ||
}) || errorRate.add(1); | ||
|
||
// check(res, { | ||
// "is status 200": (r) => r.status === 200 | ||
// }); | ||
} |
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