|
| 1 | +// EXAMPLE: tdigest_tutorial |
| 2 | +// HIDE_START |
| 3 | + |
| 4 | +using NRedisStack.RedisStackCommands; |
| 5 | +using NRedisStack.Tests; |
| 6 | +using StackExchange.Redis; |
| 7 | + |
| 8 | +// HIDE_END |
| 9 | + |
| 10 | +// REMOVE_START |
| 11 | +namespace Doc; |
| 12 | +[Collection("DocsTests")] |
| 13 | +// REMOVE_END |
| 14 | + |
| 15 | +// HIDE_START |
| 16 | +public class Tdigest_tutorial |
| 17 | +{ |
| 18 | + |
| 19 | + [SkipIfRedis(Is.OSSCluster)] |
| 20 | + public void run() |
| 21 | + { |
| 22 | + var muxer = ConnectionMultiplexer.Connect("localhost:6379"); |
| 23 | + var db = muxer.GetDatabase(); |
| 24 | + //REMOVE_START |
| 25 | + // Clear any keys here before using them in tests. |
| 26 | + db.KeyDelete("racer_ages"); |
| 27 | + db.KeyDelete("bikes:sales"); |
| 28 | + //REMOVE_END |
| 29 | + // HIDE_END |
| 30 | + |
| 31 | + |
| 32 | + // STEP_START tdig_start |
| 33 | + bool res1 = db.TDIGEST().Create("bikes:sales", 100); |
| 34 | + Console.WriteLine(res1); // >>> True |
| 35 | + |
| 36 | + bool res2 = db.TDIGEST().Add("bikes:sales", 21); |
| 37 | + Console.WriteLine(res2); // >>> True |
| 38 | + |
| 39 | + bool res3 = db.TDIGEST().Add("bikes:sales", 150, 95, 75, 34); |
| 40 | + Console.WriteLine(res3); // >>> true |
| 41 | + // STEP_END |
| 42 | + |
| 43 | + // Tests for 'tdig_start' step. |
| 44 | + // REMOVE_START |
| 45 | + Assert.True(res1); |
| 46 | + Assert.True(res2); |
| 47 | + Assert.True(res3); |
| 48 | + // REMOVE_END |
| 49 | + |
| 50 | + |
| 51 | + // STEP_START tdig_cdf |
| 52 | + bool res4 = db.TDIGEST().Create("racer_ages"); |
| 53 | + Console.WriteLine(res4); // >>> True |
| 54 | + |
| 55 | + bool res5 = db.TDIGEST().Add("racer_ages", |
| 56 | + 45.88, |
| 57 | + 44.2, |
| 58 | + 58.03, |
| 59 | + 19.76, |
| 60 | + 39.84, |
| 61 | + 69.28, |
| 62 | + 50.97, |
| 63 | + 25.41, |
| 64 | + 19.27, |
| 65 | + 85.71, |
| 66 | + 42.63 |
| 67 | + ); |
| 68 | + Console.WriteLine(res5); // >>> True |
| 69 | + |
| 70 | + long[] res6 = db.TDIGEST().Rank("racer_ages", 50); |
| 71 | + Console.WriteLine(string.Join(", ", res6)); // >>> 7 |
| 72 | + |
| 73 | + long[] res7 = db.TDIGEST().Rank("racer_ages", 50, 40); |
| 74 | + Console.WriteLine(string.Join(", ", res7)); // >>> 7, 4 |
| 75 | + // STEP_END |
| 76 | + |
| 77 | + // Tests for 'tdig_cdf' step. |
| 78 | + // REMOVE_START |
| 79 | + Assert.True(res4); |
| 80 | + Assert.True(res5); |
| 81 | + Assert.Equal("7", string.Join(", ", res6)); |
| 82 | + Assert.Equal("7, 4", string.Join(", ", res7)); |
| 83 | + // REMOVE_END |
| 84 | + |
| 85 | + |
| 86 | + // STEP_START tdig_quant |
| 87 | + double[] res8 = db.TDIGEST().Quantile("racer_ages", 0.5); ; |
| 88 | + Console.WriteLine(string.Join(", ", res8)); // >>> 44.2 |
| 89 | + |
| 90 | + double[] res9 = db.TDIGEST().ByRank("racer_ages", 4); |
| 91 | + Console.WriteLine(string.Join(", ", res9)); // >>> 42.63 |
| 92 | + // STEP_END |
| 93 | + |
| 94 | + // Tests for 'tdig_quant' step. |
| 95 | + // REMOVE_START |
| 96 | + Assert.Equal("44.2", string.Join(", ", res8)); |
| 97 | + Assert.Equal("42.63", string.Join(", ", res9)); |
| 98 | + // REMOVE_END |
| 99 | + |
| 100 | + |
| 101 | + // STEP_START tdig_min |
| 102 | + double res10 = db.TDIGEST().Min("racer_ages"); |
| 103 | + Console.WriteLine(res10); // >>> 19.27 |
| 104 | + |
| 105 | + double res11 = db.TDIGEST().Max("racer_ages"); |
| 106 | + Console.WriteLine(res11); // >>> 85.71 |
| 107 | + // STEP_END |
| 108 | + |
| 109 | + // Tests for 'tdig_min' step. |
| 110 | + // REMOVE_START |
| 111 | + Assert.Equal(19.27, res10); |
| 112 | + Assert.Equal(85.71, res11); |
| 113 | + // REMOVE_END |
| 114 | + |
| 115 | + |
| 116 | + // STEP_START tdig_reset |
| 117 | + bool res12 = db.TDIGEST().Reset("racer_ages"); |
| 118 | + Console.WriteLine(res12); // >>> True |
| 119 | + // STEP_END |
| 120 | + |
| 121 | + // Tests for 'tdig_reset' step. |
| 122 | + // REMOVE_START |
| 123 | + Assert.True(res12); |
| 124 | + // REMOVE_END |
| 125 | + |
| 126 | + |
| 127 | + // HIDE_START |
| 128 | + } |
| 129 | +} |
| 130 | +// HIDE_END |
| 131 | + |
0 commit comments