[ 
https://issues.apache.org/jira/browse/BEAM-3545?focusedWorklogId=83701&page=com.atlassian.jira.plugin.system.issuetabpanels:worklog-tabpanel#worklog-83701
 ]

ASF GitHub Bot logged work on BEAM-3545:
----------------------------------------

                Author: ASF GitHub Bot
            Created on: 23/Mar/18 17:27
            Start Date: 23/Mar/18 17:27
    Worklog Time Spent: 10m 
      Work Description: lostluck commented on a change in pull request #4899: 
[BEAM-3545] Go SDK UserCounters
URL: https://github.com/apache/beam/pull/4899#discussion_r176808734
 
 

 ##########
 File path: sdks/go/pkg/beam/metrics_test.go
 ##########
 @@ -0,0 +1,272 @@
+package beam
+
+import (
+       "context"
+       "fmt"
+       "testing"
+       "time"
+
+       "github.com/apache/beam/sdks/go/pkg/beam/core/util/ptransform"
+)
+
+func ctxWithPTransformKey(k string) context.Context {
+       return ptransform.SetID(context.Background(), k)
+}
+
+func TestCounter_Inc(t *testing.T) {
+       tests := []struct {
+               ns, n, key string // Counter name and PTransform context
+               inc        int64
+               count, sum int64 // Internal variables to check
+       }{
+               {ns: "inc1", n: "count", key: "A", inc: 1, count: 1, sum: 1},
+               {ns: "inc1", n: "count", key: "A", inc: 1, count: 2, sum: 2},
+               {ns: "inc1", n: "ticker", key: "A", inc: 1, count: 1, sum: 1},
+               {ns: "inc1", n: "ticker", key: "A", inc: 2, count: 2, sum: 3},
+               {ns: "inc1", n: "count", key: "B", inc: 1, count: 1, sum: 1},
+               {ns: "inc1", n: "count", key: "B", inc: 1, count: 2, sum: 2},
+               {ns: "inc1", n: "ticker", key: "B", inc: 1, count: 1, sum: 1},
+               {ns: "inc1", n: "ticker", key: "B", inc: 2, count: 2, sum: 3},
+               {ns: "inc2", n: "count", key: "A", inc: 1, count: 1, sum: 1},
+               {ns: "inc2", n: "count", key: "A", inc: 1, count: 2, sum: 2},
+               {ns: "inc2", n: "ticker", key: "A", inc: 1, count: 1, sum: 1},
+               {ns: "inc2", n: "ticker", key: "A", inc: 2, count: 2, sum: 3},
+               {ns: "inc2", n: "count", key: "B", inc: 1, count: 1, sum: 1},
+               {ns: "inc2", n: "count", key: "B", inc: 1, count: 2, sum: 2},
+               {ns: "inc2", n: "ticker", key: "B", inc: 1, count: 1, sum: 1},
+               {ns: "inc2", n: "ticker", key: "B", inc: 2, count: 2, sum: 3},
+       }
+
+       for _, test := range tests {
+               t.Run(fmt.Sprintf("add %d to %s.%s[%q] count: %d sum: %d", 
test.inc, test.ns, test.n, test.key, test.count, test.sum),
+                       func(t *testing.T) {
+                               m := GetCounter(test.ns, test.n)
+                               ctx := ctxWithPTransformKey(test.key)
+                               m.Inc(ctx, test.inc)
+
+                               d := m.storage[test.key]
+                               if got, want := d.t, counterType; got != want {
+                                       t.Errorf("GetCounter(%q,%q) type is %s, 
want %s", test.ns, test.n, got, want)
+                               }
+                               if got, want := d.count, test.count; got != 
want {
+                                       t.Errorf("GetCounter(%q,%q).Inc(%s, %d) 
d.count got %v, want %v", test.ns, test.n, test.key, test.inc, got, want)
+                               }
+                               if got, want := d.sum, test.sum; got != want {
+                                       t.Errorf("GetCounter(%q,%q).Inc(%s, %d) 
d.sum got %v, want %v", test.ns, test.n, test.key, test.inc, got, want)
+                               }
+                       })
+       }
+}
+
+func TestCounter_Dec(t *testing.T) {
+       tests := []struct {
+               ns, n, key string // Counter name and PTransform context
+               dec        int64
+               count, sum int64 // Internal variables to check
+       }{
+               {ns: "dec1", n: "count", key: "A", dec: 1, count: 1, sum: -1},
+               {ns: "dec1", n: "count", key: "A", dec: 1, count: 2, sum: -2},
+               {ns: "dec1", n: "ticker", key: "A", dec: 1, count: 1, sum: -1},
+               {ns: "dec1", n: "ticker", key: "A", dec: 2, count: 2, sum: -3},
+               {ns: "dec1", n: "count", key: "B", dec: 1, count: 1, sum: -1},
+               {ns: "dec1", n: "count", key: "B", dec: 1, count: 2, sum: -2},
+               {ns: "dec1", n: "ticker", key: "B", dec: 1, count: 1, sum: -1},
+               {ns: "dec1", n: "ticker", key: "B", dec: 2, count: 2, sum: -3},
+               {ns: "dec2", n: "count", key: "A", dec: 1, count: 1, sum: -1},
+               {ns: "dec2", n: "count", key: "A", dec: 1, count: 2, sum: -2},
+               {ns: "dec2", n: "ticker", key: "A", dec: 1, count: 1, sum: -1},
+               {ns: "dec2", n: "ticker", key: "A", dec: 2, count: 2, sum: -3},
+               {ns: "dec2", n: "count", key: "B", dec: 1, count: 1, sum: -1},
+               {ns: "dec2", n: "count", key: "B", dec: 1, count: 2, sum: -2},
+               {ns: "dec2", n: "ticker", key: "B", dec: 1, count: 1, sum: -1},
+               {ns: "dec2", n: "ticker", key: "B", dec: 2, count: 2, sum: -3},
+       }
+
+       for _, test := range tests {
+               t.Run(fmt.Sprintf("subtract %d to %s.%s[%q] count: %d sum: %d", 
test.dec, test.ns, test.n, test.key, test.count, test.sum),
+                       func(t *testing.T) {
+                               m := GetCounter(test.ns, test.n)
+                               ctx := ctxWithPTransformKey(test.key)
+                               m.Dec(ctx, test.dec)
+
+                               d := m.storage[test.key]
+                               if got, want := d.t, counterType; got != want {
+                                       t.Errorf("GetCounter(%q,%q) type is %s, 
want %s", test.ns, test.n, got, want)
+                               }
+                               if got, want := d.count, test.count; got != 
want {
+                                       t.Errorf("GetCounter(%q,%q).Dec(%s, %d) 
d.count got %v, want %v", test.ns, test.n, test.key, test.dec, got, want)
+                               }
+                               if got, want := d.sum, test.sum; got != want {
+                                       t.Errorf("GetCounter(%q,%q).Dec(%s, %d) 
d.sum got %v, want %v", test.ns, test.n, test.key, test.dec, got, want)
+                               }
+                       })
+       }
+}
+
+func TestDistribution_Update(t *testing.T) {
+       tests := []struct {
+               ns, n, key           string // Counter name and PTransform 
context
+               v                    int64
+               count, sum, min, max int64 // Internal variables to check
+       }{
+               {ns: "update1", n: "latency", key: "A", v: 1, count: 1, sum: 1, 
min: 1, max: 1},
+               {ns: "update1", n: "latency", key: "A", v: 1, count: 2, sum: 2, 
min: 1, max: 1},
+               {ns: "update1", n: "latency", key: "A", v: 1, count: 3, sum: 3, 
min: 1, max: 1},
+               {ns: "update1", n: "size", key: "A", v: 1, count: 1, sum: 1, 
min: 1, max: 1},
+               {ns: "update1", n: "size", key: "A", v: 2, count: 2, sum: 3, 
min: 1, max: 2},
+               {ns: "update1", n: "size", key: "A", v: 3, count: 3, sum: 6, 
min: 1, max: 3},
+               {ns: "update1", n: "size", key: "A", v: -4, count: 4, sum: 2, 
min: -4, max: 3},
+               {ns: "update1", n: "size", key: "A", v: 1, count: 5, sum: 3, 
min: -4, max: 3},
+               {ns: "update1", n: "latency", key: "B", v: 1, count: 1, sum: 1, 
min: 1, max: 1},
+               {ns: "update1", n: "latency", key: "B", v: 1, count: 2, sum: 2, 
min: 1, max: 1},
+               {ns: "update1", n: "size", key: "B", v: 1, count: 1, sum: 1, 
min: 1, max: 1},
+               {ns: "update1", n: "size", key: "B", v: 2, count: 2, sum: 3, 
min: 1, max: 2},
+               {ns: "update2", n: "latency", key: "A", v: 1, count: 1, sum: 1, 
min: 1, max: 1},
+               {ns: "update2", n: "latency", key: "A", v: 1, count: 2, sum: 2, 
min: 1, max: 1},
+               {ns: "update2", n: "size", key: "A", v: 1, count: 1, sum: 1, 
min: 1, max: 1},
+               {ns: "update2", n: "size", key: "A", v: 2, count: 2, sum: 3, 
min: 1, max: 2},
+               {ns: "update2", n: "latency", key: "B", v: 1, count: 1, sum: 1, 
min: 1, max: 1},
+               {ns: "update2", n: "latency", key: "B", v: 1, count: 2, sum: 2, 
min: 1, max: 1},
+               {ns: "update2", n: "size", key: "B", v: 1, count: 1, sum: 1, 
min: 1, max: 1},
+               {ns: "update2", n: "size", key: "B", v: 2, count: 2, sum: 3, 
min: 1, max: 2},
+               {ns: "update1", n: "size", key: "A", v: 1, count: 6, sum: 4, 
min: -4, max: 3},
+       }
+
+       for _, test := range tests {
+               t.Run(fmt.Sprintf("add %d to %s.%s[%q] count: %d sum: %d", 
test.v, test.ns, test.n, test.key, test.count, test.sum),
+                       func(t *testing.T) {
+                               m := GetDistribution(test.ns, test.n)
+                               ctx := ctxWithPTransformKey(test.key)
+                               m.Update(ctx, test.v)
+
+                               d := m.storage[test.key]
+                               if got, want := d.t, distributionType; got != 
want {
+                                       t.Errorf("GetDistribution(%q,%q) type 
is %s, want %s", test.ns, test.n, got, want)
+                               }
+                               if got, want := d.count, test.count; got != 
want {
+                                       
t.Errorf("GetDistribution(%q,%q).Update(%s, %d) d.count got %v, want %v", 
test.ns, test.n, test.key, test.v, got, want)
+                               }
+                               if got, want := d.sum, test.sum; got != want {
+                                       
t.Errorf("GetDistribution(%q,%q).Update(%s, %d) d.sum got %v, want %v", 
test.ns, test.n, test.key, test.v, got, want)
+                               }
+                               if got, want := d.min, test.min; got != want {
+                                       
t.Errorf("GetDistribution(%q,%q).Update(%s, %d) d.min got %v, want %v", 
test.ns, test.n, test.key, test.v, got, want)
+                               }
+                               if got, want := d.max, test.max; got != want {
+                                       
t.Errorf("GetDistribution(%q,%q).Update(%s, %d) d.max got %v, want %v", 
test.ns, test.n, test.key, test.v, got, want)
+                               }
+                       })
+       }
+}
+
+type testclock time.Time
+
+func (t testclock) Now() time.Time {
+       return time.Time(t)
+}
+
+func TestGuage_Set(t *testing.T) {
 
 Review comment:
   Done. Thanks for the catch!

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
[email protected]


Issue Time Tracking
-------------------

    Worklog Id:     (was: 83701)
    Time Spent: 2h 20m  (was: 2h 10m)

> Fn API metrics in Go SDK harness
> --------------------------------
>
>                 Key: BEAM-3545
>                 URL: https://issues.apache.org/jira/browse/BEAM-3545
>             Project: Beam
>          Issue Type: Sub-task
>          Components: sdk-go
>            Reporter: Kenneth Knowles
>            Assignee: Robert Burke
>            Priority: Major
>              Labels: portability
>          Time Spent: 2h 20m
>  Remaining Estimate: 0h
>




--
This message was sent by Atlassian JIRA
(v7.6.3#76005)

Reply via email to