Skip to content

Instantly share code, notes, and snippets.

@pierre
Created March 19, 2012 02:12
Show Gist options
  • Save pierre/2090655 to your computer and use it in GitHub Desktop.
Save pierre/2090655 to your computer and use it in GitHub Desktop.
Arecibo LZF
/*
* Copyright 2010-2012 Ning, Inc.
*
* Ning 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 com.ning.arecibo.util.timeline;
import com.ning.compress.lzf.LZFEncoder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException;
import java.util.Random;
public class LZFCompressor
{
private static final Logger log = LoggerFactory.getLogger(LZFCompressor.class);
public static void main(final String[] args) throws IOException
{
final int hostId = 123;
final int sampleKindId = 456;
final int timelineTimesId = 789;
final TimelineChunkAccumulator accumulator = new TimelineChunkAccumulator(hostId, sampleKindId);
final Random randomGenerator = new Random();
// 1 day timeline with 2 samples per minute
for (int i = 0; i < 2880; i++) {
// sin test with 10% noise: Original payload: 25920 bytes, compressed payload: 16781 bytes (64.74%)
final Double value = Math.sin(i * 1.0 / 4 * Math.PI) * (100 - randomGenerator.nextInt(10)) / 100;
// constant test: Original payload: 253 bytes, compressed payload: 26 bytes (10.3%)
//final Double value = 10.0;
// constant test with 10% noise: Original payload: 23868 bytes, compressed payload: 9859 byes (41.4%)
//final Double value = 10.0 * (100 - randomGenerator.nextInt(10))/100;
accumulator.addSample(new ScalarSample<Double>(SampleOpcode.DOUBLE, value));
}
final TimelineChunk chunk = accumulator.extractTimelineChunkAndReset(timelineTimesId);
final byte[] bytes = chunk.getSamples();
final byte[] compressedBytes = LZFEncoder.encode(bytes);
log.info("Original payload: {} bytes, compressed payload: {} bytes ({}%)",
new Object[]{bytes.length, compressedBytes.length, 100.0 * compressedBytes.length / bytes.length});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment