summaryrefslogtreecommitdiff
path: root/ambari-metrics/ambari-metrics-common/src/test/java/org/apache/hadoop/metrics2/sink/timeline/cache/PostProcessingUtilTest.java
blob: d8387d0304440ceb6d309efedc52fd4796755412 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
/**
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.apache.hadoop.metrics2.sink.timeline.cache;

import junit.framework.Assert;
import org.apache.hadoop.metrics2.sink.timeline.PostProcessingUtil;
import org.apache.hadoop.metrics2.sink.timeline.SingleValuedTimelineMetric;
import org.junit.Test;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

public class PostProcessingUtilTest {

  @Test
  public void testInterpolateMissinData() throws Exception {

    Map<Long, Double> metricValues = new TreeMap<Long, Double>();
    long interval = 60*1000;

    long currentTime = System.currentTimeMillis();

    for(int i = 10; i>=1;i--) {
      if (i%4 != 0 && i != 5) { //Skip time points 4,5,8
        metricValues.put(currentTime - i*interval, (double)i);
      }
    }
    metricValues = PostProcessingUtil.interpolateMissingData(metricValues, interval);
    Assert.assertTrue(metricValues.size() == 10);

    Iterator it = metricValues.entrySet().iterator();
    double sum = 0;
    while (it.hasNext()) {
      Map.Entry entry = (Map.Entry)it.next();
      sum+= (double)entry.getValue();
    }
    Assert.assertEquals(sum, 55.0);
  }

  @Test
  public void testInterpolate() throws Exception {

    long t2 = System.currentTimeMillis();
    long t1 = t2 - 60000;
    double interpolatedValue;

    //Test Equal Values
    interpolatedValue = PostProcessingUtil.interpolate((t1 + 30000), t1, 10.0, t2, 10.0);
    Assert.assertEquals(interpolatedValue, 10.0);

    //Test Linear increase Values
    interpolatedValue = PostProcessingUtil.interpolate((t1 + 30000), t1, 10.0, t2, 20.0);
    Assert.assertEquals(interpolatedValue, 15.0);

    //Test Linear decrease Values
    interpolatedValue = PostProcessingUtil.interpolate((t1 + 30000), t1, 20.0, t2, 10.0);
    Assert.assertEquals(interpolatedValue, 15.0);

    //Test interpolation with non mid point time
    interpolatedValue = PostProcessingUtil.interpolate((t1 + 20000), t1, 15.0, t2, 30.0); // 1:2 ratio
    Assert.assertEquals(interpolatedValue, 20.0);

    //Test interpolation with past time
    interpolatedValue = PostProcessingUtil.interpolate((t1 - 60000), t1, 20.0, t2, 30.0);
    Assert.assertEquals(interpolatedValue, 10.0);

  }

  @Test
  public void testLinearInterpolate() throws Exception {

    long t2 = System.currentTimeMillis();

    Map<Long, Double> valuesMap = new TreeMap<>();

    valuesMap.put(t2 - 4 * 3000, 4.0);
    valuesMap.put(t2 - 2 * 3000, 2.0);
    valuesMap.put(t2 - 1 * 3000, 1.0);

    List<Long> requiredTs = new ArrayList<Long>();
    requiredTs.add(t2 - 5*3000);
    requiredTs.add(t2 - 3*3000);
    requiredTs.add(t2);

    Map result = PostProcessingUtil.interpolate(valuesMap, requiredTs);

    Assert.assertNotNull(result);
    Assert.assertEquals(result.get(t2 - 5*3000), 5.0);
    Assert.assertEquals(result.get(t2 - 3*3000), 3.0);
    Assert.assertEquals(result.get(t2), 0.0);
    System.out.println(result.toString());

  }

  }