Refactored indexing functions of the Diesel models.

This commit is contained in:
Jordan Petridis
2017-11-14 17:56:28 +02:00
parent 6996759db5
commit a286014a62
5 changed files with 84 additions and 71 deletions
+49 -39
View File
@@ -15,15 +15,29 @@ use rand::Rng;
use test::Bencher;
use hammond_data::run_migration_on;
use hammond_data::index_feed::{complete_index, insert_return_source};
use hammond_data::index_feed::{complete_index, insert_return_source, Database};
use std::io::BufReader;
// use std::io::BufRead;
use std::path::PathBuf;
use std::sync::{Arc, Mutex};
use std::fs;
struct TempDB(tempdir::TempDir, PathBuf, SqliteConnection);
// Big rss feed
const PCPER: &[u8] = include_bytes!("feeds/pcpermp3.xml");
const UNPLUGGED: &[u8] = include_bytes!("feeds/linuxunplugged.xml");
const RADIO: &[u8] = include_bytes!("feeds/coderradiomp3.xml");
const SNAP: &[u8] = include_bytes!("feeds/techsnapmp3.xml");
const LAS: &[u8] = include_bytes!("feeds/TheLinuxActionShow.xml");
static URLS: &[(&[u8], &str)] = &[
(PCPER, "https://www.pcper.com/rss/podcasts-mp3.rss"),
(UNPLUGGED, "http://feeds.feedburner.com/linuxunplugged"),
(RADIO, "https://feeds.feedburner.com/coderradiomp3"),
(SNAP, "https://feeds.feedburner.com/techsnapmp3"),
(LAS, "https://feeds2.feedburner.com/TheLinuxActionShow"),
];
/// Create and return a Temporary DB.
/// Will be destroed once the returned variable(s) is dropped.
fn get_temp_db() -> TempDB {
@@ -40,46 +54,42 @@ fn get_temp_db() -> TempDB {
TempDB(tmp_dir, db_path, db)
}
#[bench]
fn bench_index_test_files(b: &mut Bencher) {
let TempDB(_tmp_dir, _db_path, db) = get_temp_db();
// complete_index runs in parallel so it requires a mutex as argument.
let m = Arc::new(Mutex::new(db));
fn index_urls(m: &Database) {
URLS.par_iter().for_each(|&(buff, url)| {
// Create and insert a Source into db
let s = {
let temp = m.lock().unwrap();
insert_return_source(&temp, url).unwrap()
};
// parse it into a channel
let chan = rss::Channel::read_from(buff).unwrap();
// include them in the binary to avoid loading from disk making file open syscalls.
let pcper = include_bytes!("feeds/pcpermp3.xml");
let unplugged = include_bytes!("feeds/linuxunplugged.xml");
let radio = include_bytes!("feeds/coderradiomp3.xml");
let snap = include_bytes!("feeds/techsnapmp3.xml");
let las = include_bytes!("feeds/TheLinuxActionShow.xml");
// Index the channel
complete_index(m, &chan, &s).unwrap();
});
}
// vec of (&vec<u8>, url) tuples.
let urls = vec![
(pcper.as_ref(), "https://www.pcper.com/rss/podcasts-mp3.rss"),
(
unplugged.as_ref(),
"http://feeds.feedburner.com/linuxunplugged",
),
(radio.as_ref(), "https://feeds.feedburner.com/coderradiomp3"),
(snap.as_ref(), "https://feeds.feedburner.com/techsnapmp3"),
(
las.as_ref(),
"https://feeds2.feedburner.com/TheLinuxActionShow",
),
];
#[bench]
fn bench_index_feeds(b: &mut Bencher) {
let TempDB(_tmp_dir, _db_path, db) = get_temp_db();
let m = Arc::new(Mutex::new(db));
b.iter(|| {
urls.par_iter().for_each(|&(buff, url)| {
// Create and insert a Source into db
let s = {
let temp = m.lock().unwrap();
insert_return_source(&temp, url).unwrap()
};
// parse it into a channel
let chan = rss::Channel::read_from(buff).unwrap();
index_urls(&Arc::clone(&m));
});
}
// Index the channel
complete_index(&m, &chan, &s).unwrap();
});
#[bench]
fn bench_index_unchanged_feeds(b: &mut Bencher) {
let TempDB(_tmp_dir, _db_path, db) = get_temp_db();
let m = Arc::new(Mutex::new(db));
// Index first so it will only bench the comparison test case.
index_urls(&Arc::clone(&m));
b.iter(|| {
for _ in 0..10 {
index_urls(&Arc::clone(&m));
}
});
}