Skip to content

Commit ef1cc01

Browse files
authored
Support AI.INFO (#7)
1 parent a979d3a commit ef1cc01

File tree

2 files changed

+78
-0
lines changed

2 files changed

+78
-0
lines changed

redisai/commands.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
package redisai
22

33
import (
4+
"errors"
5+
"fmt"
46
"github.com/gomodule/redigo/redis"
7+
"strconv"
58
)
69

710
// TensorSet sets a tensor
@@ -163,3 +166,35 @@ func (c *Client) LoadBackend(backend_identifier string, location string) (err er
163166
_, err = c.DoOrSend("AI.CONFIG", args, nil)
164167
return
165168
}
169+
170+
// Returns information about the execution a model or a script.
171+
func (c *Client) Info(key string) (map[string]string, error) {
172+
reply, err := c.DoOrSend("AI.INFO", redis.Args{key}, nil)
173+
values, err := redis.Values(reply, err)
174+
if err != nil {
175+
return nil, err
176+
}
177+
if len(values)%2 != 0 {
178+
return nil, errors.New("expects even number of values result")
179+
}
180+
181+
m := make(map[string]string, len(values)/2)
182+
for i := 0; i < len(values); i += 2 {
183+
k := string(values[i].([]byte))
184+
switch v := values[i+1].(type) {
185+
case []byte:
186+
m[k] = string(values[i+1].([]byte))
187+
break
188+
case int64:
189+
m[k] = strconv.FormatInt(values[i+1].(int64), 10)
190+
default:
191+
return nil, fmt.Errorf("unexpected element type for (Ints,String), got type %T", v)
192+
}
193+
}
194+
return m, nil
195+
}
196+
197+
// Resets all statistics associated with the key
198+
func (c *Client) ResetStat(key string) (string, error) {
199+
return redis.String(c.DoOrSend("AI.INFO", redis.Args{key, "RESETSTAT"}, nil))
200+
}

redisai/commands_test.go

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -825,3 +825,46 @@ func TestCommand_LoadBackend(t *testing.T) {
825825
})
826826
}
827827
}
828+
829+
func TestCommand_Info(t *testing.T) {
830+
c := createTestClient()
831+
keyModel1 := "test:Info:1"
832+
data, err := ioutil.ReadFile("./../tests/test_data/graph.pb")
833+
if err != nil {
834+
t.Errorf("Error preparing for Info(), while issuing ModelSet. error = %v", err)
835+
return
836+
}
837+
err = c.ModelSet(keyModel1, BackendTF, DeviceCPU, data, []string{"a", "b"}, []string{"mul"})
838+
if err != nil {
839+
t.Errorf("Error preparing for Info(), while issuing ModelSet. error = %v", err)
840+
return
841+
}
842+
843+
// first inited info
844+
info, err := c.Info(keyModel1)
845+
assert.NotNil(t, info)
846+
assert.Equal(t, keyModel1, info["key"])
847+
assert.Equal(t, DeviceCPU, info["device"])
848+
assert.Equal(t, BackendTF, info["backend"])
849+
assert.Equal(t, "0", info["calls"])
850+
851+
err = c.TensorSet("a", TypeFloat32, []int{1}, []float32{1.1})
852+
assert.Nil(t, err)
853+
err = c.TensorSet("b", TypeFloat32, []int{1}, []float32{4.4})
854+
assert.Nil(t, err)
855+
err = c.ModelRun(keyModel1, []string{"a", "b"}, []string{"mul"})
856+
assert.Nil(t, err)
857+
info, err = c.Info(keyModel1)
858+
// one model runs
859+
assert.Equal(t, "1", info["calls"])
860+
861+
// reset
862+
ret, err := c.ResetStat(keyModel1)
863+
assert.Equal(t, "OK", ret)
864+
info, err = c.Info(keyModel1)
865+
assert.Equal(t, "0", info["calls"])
866+
867+
// not exits
868+
ret, err = c.ResetStat("notExits")
869+
assert.NotNil(t, err)
870+
}

0 commit comments

Comments
 (0)