# Manticore 2.7.5 与 Sphinx 3.1.1

[这里](https://manticoresearch.com/blog/sphinx-3-vs-manticore-performance-benchmark/) 我们对 Sphinx 3.0.2 与 Manticore 2.6.2 进行了基准测试。这已经是 8 个月前的事了，Manticore 和 Sphinx 自那时起都有所变化。正如 [Sphinx 3.0.3 宣布](http://sphinxsearch.com/blog/2018/03/31/sphinx-3-0-3-released/) 中所说，Sphinx 3.0.3 相比 3.0.2 最多快了 2 倍，因此再次进行基准测试很有趣。这次让我们在真实数据集上进行测试 - Hacker News 评论。

基准测试是在以下条件下进行的：
- [Hacker News 精选评论数据集](https://zenodo.org/record/45901/export/xd#.XEqiuc8zaU0) 2016 年的 CSV 格式
- 操作系统：16.04.4 LTS (Xenial Xerus)，内核：4.15.0-30-generic
- CPU：Intel(R) Xeon(R) CPU E3-1275 v5 @ 3.60GHz，8 核
- 64G 内存
- 硬盘
- Docker 版本 17.05.0-ce，构建 89658be
- 索引和 searchd 的基础镜像 - alpine:3.6
- Manticore Search 在那里构建，Sphinx 二进制文件从网站下载，因为没有开源代码可供构建
- [压力测试工具](http://github.com/Ivinco/stress-tester) 用于基准测试

Manticore 和 Sphinx 的配置相同：

```ini
source full
{
  type = csvpipe
  csvpipe_command = cat /root/hacker_news_comments.prepared.csv|grep -v line_number
  csvpipe_attr_uint = story_id
  csvpipe_attr_timestamp = story_time
  csvpipe_field = story_text
  csvpipe_field = story_author
  csvpipe_attr_uint = comment_id
  csvpipe_field = comment_text
  csvpipe_field = comment_author
  csvpipe_attr_uint = comment_ranking
  csvpipe_attr_uint = author_comment_count
  csvpipe_attr_uint = story_comment_count
}

index full
{
  path = /root/idx_full
  source = full
  html_strip = 1
  mlock = 1
}

searchd
{
  listen = 9306:mysql41
  query_log = /root/query.log
  log = /root/searchd.log
  pid_file = /root/searchd.pid
  binlog_path =
  qcache_max_bytes = 0
}

```


### 索引

索引耗时 **Manticore 682 秒** 和 **Sphinx 645 秒**：

Manticore:

```bash
indexing index 'full'...
collected 11654429 docs, 6198.6 MB
sorted 1115.7 Mhits, 100.0% done
total 11654429 docs, 6198580642 bytes
total <b>681.732 sec</b>, 9092389 bytes/sec, 17095.30 docs/sec
total 11676 reads, 1.811 sec, 452.6 kb/call avg, 0.1 msec/call avg
total 9431 writes, 4.878 sec, 1065.3 kb/call avg, 0.5 msec/call avg

```
Sphinx:

```bash
using config file '/root/manticore.conf'...
indexing index 'full'...
collected 11654429 docs, 6198.6 MB
sorted 1115.7 Mhits, 100.0% done
total 11654429 docs, 6.199 Gb
total <b>645.7 sec</b>, 9.600 Mb/sec, 18049 docs/sec

```
因此在这个数据集和索引模式下 **Manticore 的索引速度比 Sphinx 慢 5.6%。**  

## 性能测试

---

在测试之前，两个实例都进行了预热。索引大小相同，且完全在操作系统缓存中：

Manticore:

```bash
snikolaev@dev:~$ sudo ls -lah /var/lib/docker/volumes/4702617c7b0d970ba660514053706125fd88f2a4b7fce7222aab1f53fba2b56d/_data/
total 4.6G
drwx------ 2 root root 4.0K Jan 25 04:50 .
drwxr-xr-x 3 root root 4.0K Jan 25 04:43 ..
-rw-r--r-- 1 root root 362M Jan 25 03:51 idx_full.spa
-rw-r--r-- 1 root root 3.1G Jan 25 03:56 idx_full.spd
-rw-r--r-- 1 root root 27M Jan 25 03:56 idx_full.spe
-rw-r--r-- 1 root root 601 Jan 25 03:56 idx_full.sph
-rw-r--r-- 1 root root 6.3M Jan 25 03:56 idx_full.spi
-rw-r--r-- 1 root root 0 Jan 25 03:51 idx_full.spk
-rw------- 1 root root 0 Jan 25 04:50 idx_full.spl
-rw-r--r-- 1 root root 0 Jan 25 03:51 idx_full.spm
-rw-r--r-- 1 root root 1.1G Jan 25 03:56 idx_full.spp
-rw-r--r-- 1 root root 1 Jan 25 03:56 idx_full.sps
-rw-rw-r-- 1 root root 750 Jan 25 03:27 manticore.conf
lrwxrwxrwx 1 root root 11 Jan 25 03:57 query.log -> /dev/stdout
lrwxrwxrwx 1 root root 11 Jan 25 03:57 searchd.log -> /dev/stdout
-rw------- 1 root root 2 Jan 25 04:50 searchd.pid
snikolaev@dev:~$ sudo vmtouch /var/lib/docker/volumes/4702617c7b0d970ba660514053706125fd88f2a4b7fce7222aab1f53fba2b56d/_data/
vmtouch: WARNING: not following symbolic link /var/lib/docker/volumes/4702617c7b0d970ba660514053706125fd88f2a4b7fce7222aab1f53fba2b56d/_data/searchd.log
vmtouch: WARNING: not following symbolic link /var/lib/docker/volumes/4702617c7b0d970ba660514053706125fd88f2a4b7fce7222aab1f53fba2b56d/_data/query.log
Files: 12
Directories: 1
Resident Pages: 1190485/1190682 4G/4G 100%
Elapsed: 0.070337 seconds
```

Sphinx:

```bash
snikolaev@dev:~$ sudo ls -lah /var/lib/docker/volumes/0aa7c89baeaa4c1bd2c52d2fbffe6f05fde5f43243534d5725482a702a90fd5b/_data
total 4.6G
drwx------ 3 root root 4.0K Jan 25 04:50 .
drwxr-xr-x 3 root root 4.0K Jan 25 04:43 ..
-rw-r--r-- 1 root root 362M Jan 25 03:37 idx_full.spa
-rw-r--r-- 1 root root 3.1G Jan 25 03:41 idx_full.spd
-rw-r--r-- 1 root root 27M Jan 25 03:41 idx_full.spe
-rw-r--r-- 1 root root 648 Jan 25 03:41 idx_full.sph
-rw-r--r-- 1 root root 6.3M Jan 25 03:41 idx_full.spi
-rw-r--r-- 1 root root 8 Jan 25 03:37 idx_full.spj
-rw-r--r-- 1 root root 1.4M Jan 25 03:37 idx_full.spk
-rw------- 1 root root 0 Jan 25 04:50 idx_full.spl
-rw-r--r-- 1 root root 1.1G Jan 25 03:41 idx_full.spp
-rw-rw-r-- 1 root root 750 Jan 25 03:27 manticore.conf
lrwxrwxrwx 1 root root 11 Jan 25 03:42 query.log -> /dev/stdout
lrwxrwxrwx 1 root root 11 Jan 25 03:42 searchd.log -> /dev/stdout
-rw------- 1 root root 2 Jan 25 04:50 searchd.pid
drwxr-sr-x 8 root root 4.0K Jan 23 08:01 sphinx-3.1.1
snikolaev@dev:~$ sudo vmtouch /var/lib/docker/volumes/0aa7c89baeaa4c1bd2c52d2fbffe6f05fde5f43243534d5725482a702a90fd5b/_data
vmtouch: WARNING: not following symbolic link /var/lib/docker/volumes/0aa7c89baeaa4c1bd2c52d2fbffe6f05fde5f43243534d5725482a702a90fd5b/_data/searchd.log
vmtouch: WARNING: not following symbolic link /var/lib/docker/volumes/0aa7c89baeaa4c1bd2c52d2fbffe6f05fde5f43243534d5725482a702a90fd5b/_data/query.log
Files: 156
Directories: 18
Resident Pages: 1195648/1210212 4G/4G 98.8%
Elapsed: 0.075835 seconds
```

### 测试 1 - 处理集合中前 1000 个术语所需时间

首先让我们运行一个简单的测试 - 查找包含集合中前 1000 个术语的文档需要多长时间：

```bash
for n in `head -1000 hn_top.txt|awk '{print $1}'`; do
mysql -P9306 -hhn_$engine -e "select * from full where match('@(comment_text,story_text,comment_author,story_author) $n') limit 10 option max_matches=1000" > /dev/null
done

```
结果是：**Sphinx 32.9 秒** 和 **Manticore 28.1 秒**。因此在这个测试中 **Manticore Search 比 Sphinx Search 快 16.7%。**  

### 测试 2 - 前 1000 个常见术语按组分解（前 1-50，前 50-100 等）

现在让我们看看 Sphinx 和 Manticore 在处理前 1000 个常见术语的子组时有何不同：

![](./manticore-2-7-5-vs-sphinx-3-1-1/test2_tp.png)
![](./manticore-2-7-5-vs-sphinx-3-1-1/test2_lat.png)

**Manticore 在 95p 延迟方面比 Sphinx 快 24%，在吞吐量方面快 21%。**  

### 测试 3 - 前 1000 个常见术语按组分解 + 1 个来自 1-100 组的术语

通过向查询中添加一个更频繁的术语来测试文档集的交集性能：

![](./manticore-2-7-5-vs-sphinx-3-1-1/test3_tp.png)
![](./manticore-2-7-5-vs-sphinx-3-1-1/test3_lat.png)

同样，Manticore 在吞吐量方面比 Sphinx 快 21%，在 95p 延迟方面平均快 13%。尽管对于 900-950 和 950-1000 组，Sphinx 显示出略好一点的 95p 延迟 - 分别为 1.3% 和 4.6%（但吞吐量仍明显较低 - 分别比 Manticore Search 低 15% 和 14%）。

### 测试 4 - 前 1000 个常见术语按组分解 + 1 个来自 1-100 组的术语，两个术语都用引号括起来以形成短语

![](./manticore-2-7-5-vs-sphinx-3-1-1/test4_tp.png)
![](./manticore-2-7-5-vs-sphinx-3-1-1/test4_lat.png)

平均而言，Manticore 再次更快：吞吐量快 20%，95p 延迟快 11%。与之前的测试一样，Sphinx 在 850-900、900-950 和 950-1000 组的 95p 延迟方面表现略好 - 分别快 1.4%、4.9% 和 4.9%，但吞吐量仍然明显较低 - 分别低 14-15%。

### 测试 5 - 从 600-750 组中各取 2 个术语，不同并发量

此测试旨在展示在不同查询并发量下的吞吐量差异。我们得到的结果如下：

![](./manticore-2-7-5-vs-sphinx-3-1-1/test5_tp.png)
![](./manticore-2-7-5-vs-sphinx-3-1-1/test5_lat.png)

因此，在所有并发量下，Manticore 的平均速度都快 14%，在并发量为 8 时快 17%，此时吞吐量达到最大，95p 延迟也低 17%。


# 结论

Sphinx 在索引性能方面略好一些。

在搜索性能方面，Manticore 2.7.5 在所有测试中都表现出更高的吞吐量，在绝大多数测试中延迟也显著更低。

该测试完全容器化，并且 [在我们的 GitHub 上开源](https://github.com/manticoresoftware/benchmarks/tree/master/hn_sphinx_manticore)。详细结果可以在 [这里](https://docs.google.com/spreadsheets/d/17RuSzkkloMwMoqrOdo0FjdI5Z-lELSLWffW-mkS5RNU/edit#gid=794362147) 找到。如果您在自己的硬件上运行相同的测试或添加不同的测试并告知我们结果，我们将不胜感激。

感谢阅读！
