Skip to content

Commit

Permalink
allow user to delete value or set value with TTL in second (#2)
Browse files Browse the repository at this point in the history
* feat: init delete keyvalue

Signed-off-by: minhhn3 <minhhn3@vng.com.vn>

* feat: init delete keyvalue

Signed-off-by: minhhn3 <minhhn3@vng.com.vn>

* feat: init delete keyvalue

Signed-off-by: minhhn3 <minhhn3@vng.com.vn>

* feat: init delete keyvalue

Signed-off-by: minhhn3 <minhhn3@vng.com.vn>

* feat: init delete keyvalue

Signed-off-by: minhhn3 <minhhn3@vng.com.vn>

* feat: init delete keyvalue

Signed-off-by: minhhn3 <minhhn3@vng.com.vn>

* feat: init delete keyvalue

Signed-off-by: minhhn3 <minhhn3@vng.com.vn>

* feat: init delete keyvalue

Signed-off-by: minhhn3 <minhhn3@vng.com.vn>

* feat: init delete keyvalue

Signed-off-by: minhhn3 <minhhn3@vng.com.vn>

* feat: init delete keyvalue

Signed-off-by: minhhn3 <minhhn3@vng.com.vn>

* feat: remove MacOS file

Signed-off-by: minhhn3 <minhhn3@vng.com.vn>

Co-authored-by: minhhn3 <minhhn3@vng.com.vn>
  • Loading branch information
minhhoangvn and minhhn3 authored Jan 3, 2022
1 parent 7f26838 commit 7cee4e1
Show file tree
Hide file tree
Showing 4 changed files with 127 additions and 32 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

# Output of the go coverage tool, specifically when used with LiteIDE
*.out

.DS_Store
# Dependency directories (remove the comment below to include it)
# vendor/
k6
88 changes: 65 additions & 23 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,23 +40,49 @@ export const options = {
results: {
exec: 'results',
executor: 'per-vu-iterations',
startTime: '5s',
startTime: '1s',
maxDuration: '2s',
vus: 1,
},
ttl: {
exec: 'ttl',
executor: 'constant-vus',
startTime: '3s',
vus: 1,
duration: '2s',
},
},
};

const client = new kv.Client();

export function generator() {
client.set(`hello_${__VU}`, 'world');
client.setWithTTLInSecond(`ttl_${__VU}`, `ttl_${__VU}`, 5);
}

export function results() {
console.log(client.get("hello_1"));
client.delete("hello_1");
try {
var keyDeleteValue = client.get("hello_1");
console.log(typeof (keyDeleteValue));
}
catch (err) {
console.log("empty value", err);
}
var r = client.viewPrefix("hello");
for (var key in r) {
console.log(key,r[key])
console.log(key, r[key])
}
}

export function ttl() {
try {
console.log(client.get('ttl_1'));
}
catch (err) {
console.log("empty value", err);
}
}
```
Expand All @@ -73,29 +99,45 @@ $ ./k6 run script.js
/ __________ \ |__| \__\ \_____/ .io
execution: local
script: ../example.js
script: example.js
output: -
scenarios: (100.00%) 2 scenarios, 6 max VUs, 10m35s max duration (incl. graceful stop):
scenarios: (100.00%) 3 scenarios, 7 max VUs, 10m30s max duration (incl. graceful stop):
* generator: 1 iterations for each of 5 VUs (maxDuration: 10m0s, exec: generator, gracefulStop: 30s)
* results: 1 iterations for each of 1 VUs (maxDuration: 10m0s, exec: results, startTime: 5s, gracefulStop: 30s)
INFO[0005] world source=console
INFO[0005] hello_3 world source=console
INFO[0005] hello_4 world source=console
INFO[0005] hello_5 world source=console
INFO[0005] hello_6 world source=console
INFO[0005] hello_1 world source=console
INFO[0005] hello_2 world source=console
running (00m05.0s), 0/6 VUs, 6 complete and 0 interrupted iterations
* results: 1 iterations for each of 1 VUs (maxDuration: 2s, exec: results, startTime: 1s, gracefulStop: 30s)
* ttl: 1 looping VUs for 2s (exec: ttl, startTime: 3s, gracefulStop: 30s)
INFO[0001] world source=console
INFO[0001] empty value error in get value with key hello_1 source=console
INFO[0001] hello_12 world source=console
INFO[0001] hello_2 world source=console
INFO[0001] hello_7 world source=console
INFO[0001] hello_9 world source=console
INFO[0001] hello_5 world source=console
INFO[0001] hello_8 world source=console
INFO[0001] hello_4 world source=console
INFO[0001] hello_6 world source=console
INFO[0001] hello_10 world source=console
INFO[0001] hello_11 world source=console
INFO[0001] hello_13 world source=console
INFO[0001] hello_14 world source=console
INFO[0001] hello_15 world source=console
INFO[0001] hello_3 world source=console
INFO[0003] ttl_1 source=console
INFO[0005] empty value error in get value with key ttl_1 source=console
INFO[0005] empty value error in get value with key ttl_1 source=console
INFO[0005] empty value error in get value with key ttl_1 source=console
INFO[0005] empty value error in get value with key ttl_1 source=console
running (00m05.0s), 0/7 VUs, 47297 complete and 0 interrupted iterations
generator ✓ [======================================] 5 VUs 00m00.0s/10m0s 5/5 iters, 1 per VU
results ✓ [======================================] 1 VUs 00m00.0s/10m0s 1/1 iters, 1 per VU
data_received........: 0 B 0 B/s
data_sent............: 0 B 0 B/s
iteration_duration...: avg=145.98µs min=34.94µs med=67.46µs max=550.58µs p(90)=321.92µs p(95)=436.25µs
iterations...........: 6 1.199378/s
vus..................: 0 min=0 max=0
vus_max..............: 6 min=6 max=6
results ✓ [======================================] 1 VUs 0.0s/2s 1/1 iters, 1 per VU
ttl ✓ [======================================] 1 VUs 2s
data_received........: 0 B 0 B/s
data_sent............: 0 B 0 B/s
iteration_duration...: avg=36.8µs min=15.66µs med=22.64µs max=53.39ms p(90)=79.34µs p(95)=95.68µs
iterations...........: 47297 9457.107597/s
vus..................: 1 min=0 max=1
vus_max..............: 7 min=7 max=7
```
30 changes: 28 additions & 2 deletions example.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,48 @@ export const options = {
results: {
exec: 'results',
executor: 'per-vu-iterations',
startTime: '5s',
startTime: '1s',
maxDuration: '2s',
vus: 1,
},
ttl: {
exec: 'ttl',
executor: 'constant-vus',
startTime: '3s',
vus: 1,
duration: '2s',
},
},
};

const client = new kv.Client();

export function generator() {
client.set(`hello_${__VU}`, 'world');
client.setWithTTLInSecond(`ttl_${__VU}`, `ttl_${__VU}`, 5);
}

export function results() {
console.log(client.get("hello_1"));
client.delete("hello_1");
try {
var keyDeleteValue = client.get("hello_1");
console.log(typeof (keyDeleteValue));
}
catch (err) {
console.log("empty value", err);
}
var r = client.viewPrefix("hello");
for (var key in r) {
console.log(key,r[key])
console.log(key, r[key])
}
}

export function ttl() {
try {
console.log(client.get('ttl_1'));
}
catch (err) {
console.log("empty value", err);
}
}
39 changes: 33 additions & 6 deletions kv.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package kv

import (
"context"
"fmt"
"time"

badger "github.com/dgraph-io/badger/v3"
"go.k6.io/k6/js/common"
Expand Down Expand Up @@ -54,18 +56,30 @@ func (c *Client) Set(key string, value string) error {
return err
}

// Set the given key with the given value with TTL in second
func (c *Client) SetWithTTLInSecond(key string, value string, ttl int) error {
err := c.db.Update(func(txn *badger.Txn) error {
e := badger.NewEntry([]byte(key), []byte(value)).WithTTL((time.Duration(ttl) * time.Second))
err := txn.SetEntry(e)
return err
})
return err
}

// Get returns the value for the given key.
func (c *Client) Get(key string) string {
func (c *Client) Get(key string) (string, error) {
var valCopy []byte
_ = c.db.View(func(txn *badger.Txn) error {
item, _ := txn.Get([]byte(key))
_ = item.Value(func(val []byte) error {
valCopy = append([]byte{}, val...)
return nil
})
if item != nil {
valCopy, _ = item.ValueCopy(nil)
}
return nil
})
return string(valCopy)
if len(valCopy) > 0 {
return string(valCopy), nil
}
return "", fmt.Errorf("error in get value with key %s", key)
}

// ViewPrefix return all the key value pairs where the key starts with some prefix.
Expand All @@ -90,3 +104,16 @@ func (c *Client) ViewPrefix(prefix string) map[string]string {
})
return m
}

// Delete the given key
func (c *Client) Delete(key string) error {
err := c.db.Update(func(txn *badger.Txn) error {
item, _ := txn.Get([]byte(key))
if item != nil {
err := txn.Delete([]byte(key))
return err
}
return nil
})
return err
}

0 comments on commit 7cee4e1

Please sign in to comment.