Skip to content

Commit

Permalink
preserve actual formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
ro7m committed Jan 17, 2019
1 parent 5fc0849 commit 89fa2cb
Show file tree
Hide file tree
Showing 2 changed files with 278 additions and 262 deletions.
175 changes: 93 additions & 82 deletions engine/src/test/java/io/seldon/engine/api/rest/TestRandomABTest.java
Original file line number Diff line number Diff line change
@@ -1,18 +1,8 @@
package io.seldon.engine.api.rest;

import com.google.protobuf.InvalidProtocolBufferException;
import com.google.protobuf.Message;
import io.kubernetes.client.proto.IntStr.IntOrString;
import io.kubernetes.client.proto.Meta.Time;
import io.kubernetes.client.proto.Meta.Timestamp;
import io.kubernetes.client.proto.Resource.Quantity;
import io.seldon.engine.pb.IntOrStringUtils;
import io.seldon.engine.pb.JsonFormat;
import io.seldon.engine.pb.QuantityUtils;
import io.seldon.engine.pb.TimeUtils;
import io.seldon.engine.predictors.EnginePredictor;
import io.seldon.protos.DeploymentProtos.PredictorSpec;
import io.seldon.protos.PredictionProtos.SeldonMessage;
import java.net.URI;
import java.nio.charset.StandardCharsets;

import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
Expand All @@ -39,8 +29,21 @@
import org.springframework.util.MultiValueMap;
import org.springframework.web.context.WebApplicationContext;

import java.net.URI;
import java.nio.charset.StandardCharsets;
import com.google.protobuf.InvalidProtocolBufferException;
import com.google.protobuf.Message;

import io.kubernetes.client.proto.IntStr.IntOrString;
import io.kubernetes.client.proto.Meta.Time;
import io.kubernetes.client.proto.Meta.Timestamp;
import io.kubernetes.client.proto.Resource.Quantity;
import io.seldon.engine.pb.IntOrStringUtils;
import io.seldon.engine.pb.JsonFormat;
import io.seldon.engine.pb.QuantityUtils;
import io.seldon.engine.pb.TimeUtils;
import io.seldon.engine.predictors.EnginePredictor;
import io.seldon.engine.service.InternalPredictionService;
import io.seldon.protos.DeploymentProtos.PredictorSpec;
import io.seldon.protos.PredictionProtos.SeldonMessage;

import static io.seldon.engine.util.TestUtils.readFile;

Expand All @@ -49,89 +52,97 @@
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
@AutoConfigureMockMvc
@TestPropertySource(properties = {
"management.security.enabled=false",
})
"management.security.enabled=false",
})
public class TestRandomABTest {

private <T extends Message.Builder> void updateMessageBuilderFromJson(T messageBuilder, String json) throws InvalidProtocolBufferException {
private <T extends Message.Builder> void updateMessageBuilderFromJson(T messageBuilder, String json) throws InvalidProtocolBufferException {
JsonFormat.parser().ignoringUnknownFields()
.usingTypeParser(IntOrString.getDescriptor().getFullName(), new IntOrStringUtils.IntOrStringParser())
.usingTypeParser(Quantity.getDescriptor().getFullName(), new QuantityUtils.QuantityParser())
.usingTypeParser(Time.getDescriptor().getFullName(), new TimeUtils.TimeParser())
.usingTypeParser(Timestamp.getDescriptor().getFullName(), new TimeUtils.TimeParser())
.merge(json, messageBuilder);
.usingTypeParser(IntOrString.getDescriptor().getFullName(), new IntOrStringUtils.IntOrStringParser())
.usingTypeParser(Quantity.getDescriptor().getFullName(), new QuantityUtils.QuantityParser())
.usingTypeParser(Time.getDescriptor().getFullName(), new TimeUtils.TimeParser())
.usingTypeParser(Timestamp.getDescriptor().getFullName(), new TimeUtils.TimeParser())
.merge(json, messageBuilder);
}

@Autowired
private WebApplicationContext context;

@Autowired
private EnginePredictor enginePredictor;

@Autowired
private WebApplicationContext context;

@Autowired
private EnginePredictor enginePredictor;


//@Autowired
private MockMvc mvc;

@Autowired
private RestClientController restController;

@Before
public void setup() throws Exception {
mvc = MockMvcBuilders
.webAppContextSetup(context)
.build();
}
public void setup() throws Exception {
mvc = MockMvcBuilders
.webAppContextSetup(context)
.build();
}

@LocalServerPort
private int port;

@Autowired
private TestRestTemplate testRestTemplate;
@Autowired
private TestRestTemplate testRestTemplate;

@Autowired
private InternalPredictionService internalPredictionService;



@Test
public void testModelMetrics() throws Exception
{
String responseStr = readFile("src/test/resources/response_with_metrics.json", StandardCharsets.UTF_8);
ResponseEntity<String> httpResponse = new ResponseEntity<>(responseStr, null, HttpStatus.OK);
Mockito.when(testRestTemplate.getRestTemplate().postForEntity(Matchers.<URI>any(), Matchers.<HttpEntity<MultiValueMap<String, String>>>any(), Matchers.<Class<String>>any()))
.thenReturn(httpResponse);

String jsonStr = readFile("src/test/resources/abtest.json",StandardCharsets.UTF_8);
PredictorSpec.Builder PredictorSpecBuilder = PredictorSpec.newBuilder();
updateMessageBuilderFromJson(PredictorSpecBuilder, jsonStr);
PredictorSpec predictorSpec = PredictorSpecBuilder.build();
final String predictJson = "{" +
"\"request\": {" +
"\"ndarray\": [[1.0]]}" +
"}";
enginePredictor.setPredictorSpec(predictorSpec);

int routeACount = 0;
int routeBCount = 1;

for(int i=0;i<100;i++)
{
MvcResult res = mvc.perform(MockMvcRequestBuilders.post("/api/v0.1/predictions")
.accept(MediaType.APPLICATION_JSON_UTF8)
.content(predictJson)
.contentType(MediaType.APPLICATION_JSON_UTF8)).andReturn();
String response = res.getResponse().getContentAsString();
System.out.println(response);
Assert.assertEquals(200, res.getResponse().getStatus());

SeldonMessage.Builder builder = SeldonMessage.newBuilder();
JsonFormat.parser().ignoringUnknownFields().merge(response, builder);
SeldonMessage seldonMessage = builder.build();

Assert.assertTrue(seldonMessage.getMeta().getRoutingMap().get("abtest") >= 0);
if (seldonMessage.getMeta().getRoutingMap().get("abtest") == 0)
routeACount++;
else
routeBCount++;
}
double split = routeACount /(double)(routeACount + routeBCount);
System.out.println(routeACount);
System.out.println(routeBCount);
Assert.assertEquals(0.5, split,0.2);
String jsonStr = readFile("src/test/resources/abtest.json",StandardCharsets.UTF_8);
String responseStr = readFile("src/test/resources/response_with_metrics.json",StandardCharsets.UTF_8);
ResponseEntity<String> httpResponse = new ResponseEntity<>(responseStr, null, HttpStatus.OK);
Mockito.when(testRestTemplate.getRestTemplate().postForEntity(Matchers.<URI>any(), Matchers.<HttpEntity<MultiValueMap<String, String>>>any(), Matchers.<Class<String>>any()))
.thenReturn(httpResponse);
PredictorSpec.Builder PredictorSpecBuilder = PredictorSpec.newBuilder();
updateMessageBuilderFromJson(PredictorSpecBuilder, jsonStr);
PredictorSpec predictorSpec = PredictorSpecBuilder.build();
final String predictJson = "{" +
"\"request\": {" +
"\"ndarray\": [[1.0]]}" +
"}";
enginePredictor.setPredictorSpec(predictorSpec);

int routeACount = 0;
int routeBCount = 1;

for(int i=0;i<100;i++)
{
MvcResult res = mvc.perform(MockMvcRequestBuilders.post("/api/v0.1/predictions")
.accept(MediaType.APPLICATION_JSON_UTF8)
.content(predictJson)
.contentType(MediaType.APPLICATION_JSON_UTF8)).andReturn();
String response = res.getResponse().getContentAsString();
System.out.println(response);
Assert.assertEquals(200, res.getResponse().getStatus());

SeldonMessage.Builder builder = SeldonMessage.newBuilder();
JsonFormat.parser().ignoringUnknownFields().merge(response, builder);
SeldonMessage seldonMessage = builder.build();

Assert.assertTrue(seldonMessage.getMeta().getRoutingMap().get("abtest") >= 0);
if (seldonMessage.getMeta().getRoutingMap().get("abtest") == 0)
routeACount++;
else
routeBCount++;
}
double split = routeACount /(double)(routeACount + routeBCount);
System.out.println(routeACount);
System.out.println(routeBCount);
Assert.assertEquals(0.5, split,0.2);



}
}

}
Loading

0 comments on commit 89fa2cb

Please sign in to comment.