2008-11-15 18:38:06 +01:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2008 Google Inc.
|
|
|
|
*
|
|
|
|
* Licensed 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.
|
|
|
|
*/
|
|
|
|
|
2008-10-11 01:22:27 +02:00
|
|
|
package com.google.gson.metrics;
|
|
|
|
|
|
|
|
import com.google.gson.Gson;
|
|
|
|
import com.google.gson.JsonParseException;
|
|
|
|
|
|
|
|
import junit.framework.TestCase;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Tests to measure performance for Gson. All tests in this file will be disabled in code. To run
|
|
|
|
* them remove disabled_ prefix from the tests and run them.
|
|
|
|
*
|
|
|
|
* @author Inderjeet Singh
|
|
|
|
* @author Joel Leitch
|
|
|
|
*/
|
|
|
|
public class PerformanceTest extends TestCase {
|
|
|
|
private Gson gson;
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void setUp() throws Exception {
|
|
|
|
super.setUp();
|
|
|
|
gson = new Gson();
|
|
|
|
}
|
|
|
|
|
|
|
|
public void testDummy() {
|
|
|
|
// This is here to prevent Junit for complaining when we disable all tests.
|
|
|
|
}
|
|
|
|
|
|
|
|
public void disabled_testStringDeserializationPerformance() {
|
|
|
|
StringBuilder sb = new StringBuilder(8096);
|
|
|
|
sb.append("Error Yippie");
|
|
|
|
|
|
|
|
while (true) {
|
|
|
|
try {
|
|
|
|
String stackTrace = sb.toString();
|
|
|
|
sb.append(stackTrace);
|
|
|
|
String json = "{\"message\":\"Error message.\"," + "\"stackTrace\":\"" + stackTrace + "\"}";
|
|
|
|
parseLongJson(json);
|
|
|
|
System.out.println("Gson could handle a string of size: " + stackTrace.length());
|
|
|
|
} catch (JsonParseException expected) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void parseLongJson(String json) throws JsonParseException {
|
|
|
|
ExceptionHolder target = gson.fromJson(json, ExceptionHolder.class);
|
|
|
|
assertTrue(target.message.contains("Error"));
|
|
|
|
assertTrue(target.stackTrace.contains("Yippie"));
|
|
|
|
}
|
|
|
|
|
|
|
|
private static class ExceptionHolder {
|
|
|
|
public final String message;
|
|
|
|
public final String stackTrace;
|
|
|
|
public ExceptionHolder() {
|
|
|
|
this("", "");
|
|
|
|
}
|
|
|
|
public ExceptionHolder(String message, String stackTrace) {
|
|
|
|
this.message = message;
|
|
|
|
this.stackTrace = stackTrace;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|