Code: Select all
namespace java com.myproject.mybenchmark.thrift
struct StudentThrift {
1: required double sid
2: required double grade
3: required i32 age
4: required i32 year
}
struct StudentThriftList {
1: list students
}
< /code>
Serialisierungsmethode: < /p>
private static void serializeToFile(ArrayList studentList, String outputFile) {
StudentThriftList thriftStudentList = new StudentThriftList();
for(Student stu:studentList) {
StudentThrift thriftStudent = new StudentThrift();
thriftStudent.setSid(stu.getSid());
thriftStudent.setGrade(stu.getGrade());
thriftStudent.setAge(stu.getAge());
thriftStudent.setYear(stu.getYear());
thriftStudentList.addToStudents(thriftStudent);
}
// Serializing to disk.
try (FileOutputStream fos = new FileOutputStream(outputFile);
TTransport transport = new TIOStreamTransport(fos)) {
TCompactProtocol protocol = new TCompactProtocol(transport);
thriftStudentList.write(protocol);
} catch (Exception e) {
e.printStackTrace();
}
}
< /code>
Deserialisierungsmethode: < /p>
public static StudentThriftList deserializeFromFile(String dataFile) {
StudentThriftList thriftList = new StudentThriftList();
try (FileInputStream fis = new FileInputStream(dataFile);
TTransport transport = new TIOStreamTransport(fis)) {
TCompactProtocol protocol = new TCompactProtocol(transport);
thriftList.read(protocol);
} catch (Exception e) {
e.printStackTrace();
}
return thriftList;
}
< /code>
Hauptkala: < /p>
public static void main(String[] args) {
ArrayList studentList = new ArrayList();
long serStartTime = 0L;
long serEndTime = 0L;
int serCount = 0;
long serTotalTime = 0L;
long desStartTime = 0L;
long desEndTime = 0L;
int desCount = 0;
long desTotalTime = 0L;
// Create a list with 10000 objects:
for (int i = 1; i
--------------REPORT---------------
Serializetion count: 1000
Serializetion avg time (ms): 408
Deserializetion count: 1000
Deserializetion avg time (ms): 448