github.com/yusys-cloud/go-jsonstore-rest@v0.0.0-20230228115429-0a54aa4a27a6/jsonstore/jsonstore_test.go (about)

     1  // Author: yangzq80@gmail.com
     2  // Date: 2021-03-30
     3  //
     4  package jsonstore
     5  
     6  import (
     7  	"io/ioutil"
     8  	"os"
     9  	"regexp"
    10  	"strconv"
    11  	"testing"
    12  )
    13  
    14  type Human struct {
    15  	Name   string
    16  	Height float64
    17  }
    18  
    19  func testFile() *os.File {
    20  	f, err := ioutil.TempFile(".", "jsonstore")
    21  	if err != nil {
    22  		panic(err)
    23  	}
    24  	return f
    25  }
    26  
    27  func TestOpen(t *testing.T) {
    28  	f := testFile()
    29  	defer os.Remove(f.Name())
    30  	ioutil.WriteFile(f.Name(), []byte(`{"hello":"world"}`), 0644)
    31  	ks, err := Open(f.Name())
    32  	if err != nil {
    33  		t.Error(err)
    34  	}
    35  	if len(ks.Data) != 1 {
    36  		t.Errorf("expected %d got %d", 1, len(ks.Data))
    37  	}
    38  	if world, ok := ks.Data["hello"]; !ok || string(world) != `world` {
    39  		t.Errorf("expected %s got %s", "world", world)
    40  	}
    41  }
    42  
    43  func TestGeneral(t *testing.T) {
    44  	f := testFile()
    45  	defer os.Remove(f.Name())
    46  	ks := new(JSONStore)
    47  	err := ks.Set("hello", "world")
    48  	if err != nil {
    49  		t.Error(err)
    50  	}
    51  	if err = Save(ks, f.Name()); err != nil {
    52  		t.Error(err)
    53  	}
    54  
    55  	ks2, _ := Open(f.Name())
    56  	var a string
    57  	var b string
    58  	ks.Get("hello", &a)
    59  	ks2.Get("hello", &b)
    60  	if a != b {
    61  		t.Errorf("expected '%s' got '%s'", a, b)
    62  	}
    63  
    64  	// Set a object, using a Gzipped JSON
    65  	type Human struct {
    66  		Name   string
    67  		Height float64
    68  	}
    69  	ks.Set("human:1", Human{"Dante", 5.4})
    70  	Save(ks, "test2.json.gz") // TODO: use tmp file and remove when done
    71  	Save(ks, "test2.json")    // TODO: use tmp file and remove when done
    72  	var human Human
    73  
    74  	ks2, err = Open("test2.json")
    75  	if err != nil {
    76  		t.Errorf(err.Error())
    77  	}
    78  	ks2.Get("human:1", &human)
    79  	if human.Height != 5.4 {
    80  		t.Errorf("expected '%v', got '%v'", Human{"Dante", 5.4}, human)
    81  	}
    82  
    83  	ks2, err = Open("test2.json.gz")
    84  	if err != nil {
    85  		t.Errorf(err.Error())
    86  	}
    87  	ks2.Get("human:1", &human)
    88  	if human.Height != 5.4 {
    89  		t.Errorf("expected '%v', got '%v'", Human{"Dante", 5.4}, human)
    90  	}
    91  
    92  }
    93  
    94  func TestRegex(t *testing.T) {
    95  	f := testFile()
    96  	defer os.Remove(f.Name())
    97  	ks := new(JSONStore)
    98  	ks.Set("hello:1", "world1")
    99  	ks.Set("hello:2", "world2")
   100  	ks.Set("hello:3", "world3")
   101  	ks.Set("world:1", "hello1")
   102  	if len(ks.GetAll(regexp.MustCompile(`hello`))) != len(ks.Keys())-1 {
   103  		t.Errorf("Problem getting all")
   104  	}
   105  	if len(ks.GetAll(nil)) != len(ks.Keys()) {
   106  		t.Errorf("Problem getting all")
   107  	}
   108  }
   109  
   110  func BenchmarkOpen100(b *testing.B) {
   111  	f := testFile()
   112  	defer os.Remove(f.Name())
   113  	ks := new(JSONStore)
   114  	for i := 1; i < 100; i++ {
   115  		ks.Set("hello:"+strconv.Itoa(i), "world"+strconv.Itoa(i))
   116  	}
   117  	Save(ks, f.Name())
   118  
   119  	var err error
   120  	b.ResetTimer()
   121  	for i := 0; i < b.N; i++ {
   122  		ks, err = Open(f.Name())
   123  		if err != nil {
   124  			panic(err)
   125  		}
   126  	}
   127  	Save(ks, f.Name())
   128  }
   129  
   130  func BenchmarkOpen10000(b *testing.B) {
   131  	f := testFile()
   132  	defer os.Remove(f.Name())
   133  	ks := new(JSONStore)
   134  	for i := 1; i < 10000; i++ {
   135  		ks.Set("hello:"+strconv.Itoa(i), "world"+strconv.Itoa(i))
   136  	}
   137  	Save(ks, f.Name())
   138  
   139  	var err error
   140  	b.ResetTimer()
   141  	for i := 0; i < b.N; i++ {
   142  		ks, err = Open(f.Name())
   143  		if err != nil {
   144  			panic(err)
   145  		}
   146  	}
   147  	Save(ks, f.Name())
   148  }
   149  
   150  func BenchmarkGet(b *testing.B) {
   151  	ks := new(JSONStore)
   152  	err := ks.Set("human:1", Human{"Dante", 5.4})
   153  	if err != nil {
   154  		panic(err)
   155  	}
   156  	b.ResetTimer()
   157  	for i := 0; i < b.N; i++ {
   158  		var human Human
   159  		ks.Get("human:1", &human)
   160  	}
   161  }
   162  
   163  func BenchmarkSet(b *testing.B) {
   164  	ks := new(JSONStore)
   165  	b.ResetTimer()
   166  	// set a key to any object you want
   167  	for i := 0; i < b.N; i++ {
   168  		err := ks.Set("human:"+strconv.Itoa(i), Human{"Dante", 5.4})
   169  		if err != nil {
   170  			panic(err)
   171  		}
   172  	}
   173  }
   174  
   175  func BenchmarkSave100(b *testing.B) {
   176  	ks := new(JSONStore)
   177  	for i := 1; i < 100; i++ {
   178  		ks.Set("hello:"+strconv.Itoa(i), "world"+strconv.Itoa(i))
   179  	}
   180  	b.ResetTimer()
   181  	for i := 0; i < b.N; i++ {
   182  		Save(ks, "benchmark.json")
   183  	}
   184  }
   185  
   186  func BenchmarkSave10000(b *testing.B) {
   187  	ks := new(JSONStore)
   188  	for i := 1; i < 10000; i++ {
   189  		ks.Set("hello:"+strconv.Itoa(i), "world"+strconv.Itoa(i))
   190  	}
   191  	b.ResetTimer()
   192  	for i := 0; i < b.N; i++ {
   193  		Save(ks, "benchmark.json")
   194  	}
   195  }
   196  
   197  func BenchmarkSaveGz100(b *testing.B) {
   198  	ks := new(JSONStore)
   199  	for i := 1; i < 100; i++ {
   200  		ks.Set("hello:"+strconv.Itoa(i), "world"+strconv.Itoa(i))
   201  	}
   202  	b.ResetTimer()
   203  	for i := 0; i < b.N; i++ {
   204  		Save(ks, "benchmark.json.gz")
   205  	}
   206  }
   207  
   208  func BenchmarkSaveGz10000(b *testing.B) {
   209  	ks := new(JSONStore)
   210  	for i := 1; i < 10000; i++ {
   211  		ks.Set("hello:"+strconv.Itoa(i), "world"+strconv.Itoa(i))
   212  	}
   213  	b.ResetTimer()
   214  	for i := 0; i < b.N; i++ {
   215  		Save(ks, "benchmark.json.gz")
   216  	}
   217  }